spring boot 2.0+ integration RabbitMq

1, add packages

1 <dependency>
2       <groupId>org.springframework.boot</groupId>
3      <artifactId>spring-boot-starter-amqp</artifactId>
4 </dependency>

2, add a link string

#rabbitmq配置
spring.rabbitmq.addresses=xxxxx
spring.rabbitmq.username=xxxxx
spring.rabbitmq.password=xxxxxxx
spring.rabbitmq.virtual-host=xx

3, the producers, wherein the queue name MSGRABBITQUERENAME

 1 @Component
 2 public class RabbitProducer {
 3 
 4     @Autowired
 5     AmqpTemplate rabbitTemplate;
 6 
 7     static final String MSGRABBITQUERENAME="chat.send.msg";
 8 
 9     public void sendMessages(String str) {
10         this.rabbitTemplate.convertAndSend(MSGRABBITQUERENAME,str);
11     }
12 }

4, consumer, I am here to confirm the manual mode to ensure data validity.

1 @Component
2 @org.springframework.amqp.rabbit.annotation.RabbitListener(queues = "chat.send.msg")
3 public class RabbitListener {
4 
5     @RabbitHandler
6     public void recieved(Message message, Channel channel) {
7         channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
8     }
9 }

Then call on it

Guess you like

Origin www.cnblogs.com/rolayblog/p/11248508.html