一、@EnableRabbit的使用
开启基于注解的RabbitMQ模式
@EnableRabbit//开启基于注解的RabbitMQ模式
@SpringBootApplication
public class AmqptestApplication {
public static void main(String[] args) {
SpringApplication.run(AmqptestApplication.class, args);
}
}
二、@RabbitListene的使用
监听获取消息队列中的消息
@Service
public class BookService {
/**
* 只获取消息体
* @param book
*/
@RabbitListener(queues = "zhq.news")
public void receive(Book book){
System.out.println("收到消息:"+book);
}
/**
* 获取消息体和消息头
* @param message
*/
@RabbitListener(queues = "zhq")
public void receiveWithMessage(Message message){
System.out.println("Message:"+message);
System.out.println("Body:"+message.getBody());
System.out.println("MessageProperties:"+message.getMessageProperties());
}
}
三、编写单元测试
@Autowired
RabbitTemplate rabbitTemplate;
@Test
public void contextLoads() {
rabbitTemplate.convertAndSend("exchange.direct","zhq",new Book("Java从入门到放弃","汤姆"));
}