spring boot使用rabbitmq传递实体类:java对象必须序列化
。
实现序列化有2种方式:
1)bean对象实现Serializable接口
发送者和接收者bean对象都必须序列化
2)借助SerializationUtils工具类
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8</version>
</dependency>
- 1
- 2
- 3
- 4
- 5
发送者:SerializationUtils.serialize()
接收者:SerializationUtils.deserialize()
缺点: 发送者和接收者的bean定义必须一模一样,包括bean路径
spring boot使用rabbitmq传递JSON对象
由于MQ直接传递JSON对象有弊端,所以建议使用JSON传递。
把对象使用ObjectMapper等JSON工具类把对象转换为JSON格式,然后发送。
@Autowired
private ObjectMapper objectMapper;
List<SystemConfiguration> configs = constructConfigs(protocolId);
String msgJson = objectMapper.writeValueAsString(configs);
Message message = MessageBuilder
.withBody(msgJson.getBytes())
.setContentType(MessageProperties.CONTENT_TYPE_JSON)
.build();
rabbitTemplate.convertAndSend(RabbitConfig.CONFIGURATION_DATA_QUEUE, message);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
但是在每一个发送消息的地方都这样写就会很繁琐。如果规定了消息的格式为JSON,可以使用org.springframework.amqp.support.converter.Jackson2JsonMessageConverter作为默认的消息转换器。
在RabbitConfig添加转换器:
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitConfig {
@Bean
public RabbitTemplate rabbitTemplate(final ConnectionFactory connectionFactory) {
final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
rabbitTemplate.setMessageConverter(producerJackson2MessageConverter());
return rabbitTemplate;
}
@Bean
public Jackson2JsonMessageConverter producerJackson2MessageConverter() {
return new Jackson2JsonMessageConverter();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
在RabbitMQ的的管理端查看,转换后的确实是json,headers里包含了对象的具体类型等
接收JSON消息
为了接收JSON数据,我们应该通过实现RabbitListenerConfigurer来定制RabbitMQ配置。
import org.springframework.amqp.rabbit.annotation.RabbitListenerConfigurer;
import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistrar;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.converter.MappingJackson2MessageConverter;
import org.springframework.messaging.handler.annotation.support.DefaultMessageHandlerMethodFactory;
import org.springframework.messaging.handler.annotation.support.MessageHandlerMethodFactory;
@Configuration
public class RabbitConfig implements RabbitListenerConfigurer {
@Override
public void configureRabbitListeners(RabbitListenerEndpointRegistrar registrar) {
registrar.setMessageHandlerMethodFactory(messageHandlerMethodFactory());
}
@Bean
MessageHandlerMethodFactory messageHandlerMethodFactory() {
DefaultMessageHandlerMethodFactory messageHandlerMethodFactory = new DefaultMessageHandlerMethodFactory();
messageHandlerMethodFactory.setMessageConverter(consumerJackson2MessageConverter());
return messageHandlerMethodFactory;
}
@Bean
public MappingJackson2MessageConverter consumerJackson2MessageConverter() {
return new MappingJackson2MessageConverter();
}
}
来源:oschina
链接:https://my.oschina.net/xiaominmin/blog/4484499