@Configuration
public class GlobalConfig {
//以下配置RabbitMQ消息服务
@Autowired
public ConnectionFactory connectionFactory;
@Bean
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory() {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
factory.setConcurrentConsumers(3);
factory.setMaxConcurrentConsumers(10);
return factory;
}
@Bean
public MessageConverter jsonMessageConverter() {
return new Jackson2JsonMessageConverter();
}
@Bean
public RabbitTemplate rabbitTemplate() {
RabbitTemplate template = new RabbitTemplate(connectionFactory);
template.setMessageConverter(jsonMessageConverter());
return template;
}
}
这样配置后,就可以通过rabbitTemplate.convertAndSend(createCustomerQueueName, requestWrapper);来自动序列化成json字符串了。
RequestWrapper requestWrapper = new RequestWrapper();
rabbitTemplate.convertAndSend("queue", requestWrapper);
消费的地方是
@RabbitListener(queues = "queue")
public void processCreateCustomer(@Payload RequestWrapper requestWrapper, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag, Channel channel) throws IOException {
// 处理成功
channel.basicAck(deliveryTag,false);
// 处理失败
channel.basicNack(deliveryTag, false, true);
}
版权声明:本文为gwj41原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。