本文主要是介绍springAMQP(示例),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
配置:
消费者类(接收方):
spring:rabbitmq:host: RabbitMQ的IP地址 # 主机名port: 5672 # 发送消息和接收消息的端口号virtual-host: / # 虚拟主机username: 用户名 # 用户名password: 密码 # 密码#预取 : 每次将队列中的消息发给消费者一个消息,当消费者消费完之后在给学消费者消息。那么这样做谁消费快谁就会消费更多的消息listener:simple:prefetch: 1
消费者类(接收方):
spring:rabbitmq:host: RabbitMQ的IP地址 # 主机名port: 5672 # 发送消息和接收消息的端口号virtual-host: / # 虚拟主机username: 用户名 # 用户名password: 密码 # 密码
引入依赖(在父工程引入或者在两个工程中同时引入):
<!--AMQP依赖,包含RabbitMQ--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency>
简单消息队列:
消费者(SpringRabbitListener):
@Component
public class SpringRabbitListener {//定义监听简单消息队列simple.queue的消息/*1.RabbitListener 注解中的属性:String[] queues() default {}; 书写监听哪个队列的名字*/@RabbitListener(queues = {"simple.queue"})//@RabbitListener(queues = "simple.queue")public void listenSimpleMessage(String msg){System.out.println("消费者接收到了生产者发送的消息:"+msg);}}
生产者:
@Autowired
private RabbitTemplate rabbitTemplate;public void testSimpleQueue() {//1.定义变量保存队列名String queueName = "simple.queue";//2.定义变量保存发送的消息String message="hello,我是锁哥,发送的基本消息";//3.发送消息rabbitTemplate.convertAndSend(queueName,message);}
广播消息队列:
消费者:
@RabbitListener(queues = "fanout.queue1")public void listenFanout1Message(String msg){System.out.println("消费者1接收到了生产者发送的消息:"+msg);}@RabbitListener(queues = "fanout.queue2")public void listenFanout2Message(String msg){System.out.println("消费者2接收到了生产者发送的消息:"+msg);}
消费者配置类:
@Configuration
public class FanoutConfig {//1.定义方法声明交换机,将交换机对象放到SpringIOC容器中//@Bean修饰的方法返回值对象作为SpringIOC容器中的value,方法名作为key@Beanpublic FanoutExchange fanoutExchange(){//itcast.fanout表示交换机的名字return new FanoutExchange("itcast.fanout");}//2.定义方法声明队列1@Beanpublic Queue fanoutQueue1(){//fanout.queue1 表示队列名return new Queue("fanout.queue1");}//3.定义方法将队列1绑定到交换机itcast.fanout上//下面方法的形参FanoutExchange fanoutExchange的名字和上述 public FanoutExchange fanoutExchange(){}方法名一致@Beanpublic Binding bindingQueue1ToExchange(FanoutExchange fanoutExchange,Queue fanoutQueue1){/*public Binding to(FanoutExchange exchange){}*/return BindingBuilder.bind(fanoutQueue1).to(fanoutExchange);}//4.定义方法声明队列2@Beanpublic Queue fanoutQueue2(){//fanout.queue2 表示队列名return new Queue("fanout.queue2");}//5.定义方法将队列2绑定到交换机上@Beanpublic Binding bindingQueue2ToExchange(FanoutExchange fanoutExchange,Queue fanoutQueue2){/*public Binding to(FanoutExchange exchange){}*/return BindingBuilder.bind(fanoutQueue2).to(fanoutExchange);}}
生产者:
@Autowired
private RabbitTemplate rabbitTemplate;public void testFanoutExchange() {String exchangeName="itcast.fanout"; //1.定义变量保存交换机的名字String message="马上过年了,别慌,好好学习,找一份满意的工作,明年领媳妇回家过年"; //2.定义变量保存发送的消息//第二个参数是routingkey,这里实现的是广播交换机,不需要routingkey,因此这里是空字符串rabbitTemplate.convertAndSend(exchangeName,"",message);}
监听路由队列:
生产者:
@Autowired
private RabbitTemplate rabbitTemplate;public void testSendDirectExchange() {//1.定义变量保存交换机名String exchangeName = "交换机名x";//2.定义变量保存消息String message="乾坤未定,你我皆是屌丝程序员";//3.发送消息rabbitTemplate.convertAndSend(exchangeName,"路由值1",message);}
消费者:
@RabbitListener(bindings = {@QueueBinding(value=@Queue(name="队列名"),exchange = @Exchange(name="交换机名x",type = ExchangeTypes.DIRECT),key={"路由值1","路由值2"})})public void listenDirect1Message(String msg){System.out.println("消费者1接收到了生产者发送的消息:"+msg);}
消息转换器:
引入json(在父工程引入或者在两个工程中同时引入):
<!--json格式的消息转换器--><dependency><groupId>com.fasterxml.jackson.dataformat</groupId><artifactId>jackson-dataformat-xml</artifactId><version>2.9.10</version></dependency>
在消费者配置类中添加:
//将json的消息转换器对象放到IOC中@Beanpublic MessageConverter jsonMessageConverter(){return new Jackson2JsonMessageConverter();}
生产者配置类:
@Configuration
public class MessageConverterConfig {//将json的消息转换器对象放到IOC中@Beanpublic MessageConverter jsonMessageConverter(){return new Jackson2JsonMessageConverter();}
}
消费者:
@RabbitListener(queues = "object.queue")public void listenObjectMessage(HashMap<String,String> map){System.out.println("消费者接收到了生产者发送的消息:"+map);}
生产者:
@Autowired
private RabbitTemplate rabbitTemplate;public void testSendMap() throws InterruptedException {//1.定义变量保存队列名String queueName="object.queue";//2.创建Map集合对象HashMap<String, String> map = new HashMap<>();map.put("itcast001","张三");map.put("itcast002","柳岩");//3.发送消息rabbitTemplate.convertAndSend(queueName,map);}
这篇关于springAMQP(示例)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!