赞
踩
开启基于注解的RabbitMQ模式
- @EnableRabbit//开启基于注解的RabbitMQ模式
- @SpringBootApplication
- public class AmqptestApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(AmqptestApplication.class, args);
- }
-
- }
监听获取消息队列中的消息
- @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从入门到放弃","汤姆"));
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。