1-新建service包BookService类
package com.example.springbootamqp.service;
import com.example.springbootamqp.bean.Book;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;
@Service
public class BookService {
@RabbitListener(queues = "atguigu.news")
public void receive(Book book){
System.out.println("收到消息:"+book);
}
}
2-启动类上添加注解@EnableRabbit
package com.example.springbootamqp;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableRabbit //开启基于注解的RabbitMQ模式
@SpringBootApplication
public class SpringbootRabbitmqAmqpApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootRabbitmqAmqpApplication.class, args);
}
}
3-运行主程序类
Book类添加toString()方法
发送消息时,监听到消息
4-BookService中添加监听方法
import org.springframework.amqp.core.Message;
@RabbitListener(queues = "atguigu")
public void receive02(Message message){
System.out.println(message.getBody()); //打印消息体
System.out.println(message.getMessageProperties()); //打印消息头
}
标签:springboot,RabbitListener,springframework,EnableRabbit,org,import,public
From: https://blog.51cto.com/u_12528551/5900181