依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
配置
spring.rabbitmq.host=ip
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.virtual-host=/
消费者
@Component
@RabbitListener(bindings = @QueueBinding(
value=@Queue(value="queue_name",durable="true"),
exchange=@Exchange(name="exchange_name",durable="true",type="topic"),
key="routing_key"))
public class MyReceiver {
@RabbitHandler
public void receiver(String msg) {
System.out.println("MyReceiver :" + msg);
}
}
生产者
@Autowired
private AmqpTemplate amqpTemplate;
@Test
public void send5() {
for (int i=0;i<5;i++){
String message = "sms mail inform to user"+i;
amqpTemplate.convertAndSend("exchange_name","routing_key",message);
System.out.println("Send Message is:'" + message + "'");
}
}