send 一个消息
@Component
public class Send {
//springboot自动注入
@Autowired
private AmqpTemplate amp;
public void Send(){
this.amp.convertAndSend("hello","helloword");
}
}
recevie 一个消息
@Component
@RabbitListener(queues = "hello")
public class Receive {
//绑定queue
@RabbitHandler
public void getMsg(String massge){
System.out.println(massge);
System.out.println("这里阻塞式监听");
}
//这里是阻塞式监听
}
apllication 配置
spring:
rabbitmq:
port: 5672
username: guest
password: guest
host: localhost
测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class Test {
@Autowired
private Send send;
@org.junit.Test
public void setmsg(){
send.Send();
}
}