SpringBoot-RabbitMq 简单实例

来源:互联网 发布:签署淘宝图片空间协议 编辑:程序博客网 时间:2024/04/16 22:29

添加maven依赖:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency>

创建队列:

package com.example.demo;import org.springframework.amqp.core.Queue;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;//创建队列@Configurationpublic class RabbitConfig {    @Bean    public Queue queue(){        return new Queue("hello");    }}

消息发送:

package com.example.demo;import org.springframework.amqp.rabbit.core.RabbitTemplate;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class RabbitController {    @Autowired    RabbitTemplate rabbitTemplate;    @RequestMapping("/rm")    public void sendMsg(){        rabbitTemplate.convertAndSend("hello","你好你好hello");    }}
消息接收:

package com.example.demo;import org.springframework.amqp.rabbit.annotation.RabbitHandler;import org.springframework.stereotype.Component;//接收消息@Component@org.springframework.amqp.rabbit.annotation.RabbitListener(queues = "hello")public class RabbitListener {    @RabbitHandler    public void getMsg(String msg){        System.out.println("接收到的消息:"+msg);    }}

测试  (http://localhost:8080/rm):

在RabbitMq  Web管理端查看:

尝试一对多:

控制层:

  

package com.example.demo;import org.springframework.amqp.rabbit.core.RabbitTemplate;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class RabbitController {    @Autowired    RabbitTemplate rabbitTemplate;    @RequestMapping("/rm")    public void sendMsg(){        for(int i=0;i<50;i++){            rabbitTemplate.convertAndSend("queue1",""+i);        }    }}
消息接收:

package com.example.demo;import org.springframework.amqp.rabbit.annotation.RabbitListener;import org.springframework.stereotype.Component;//接收消息@Componentpublic class RabbitReceive {    @RabbitListener(queues="queue1")    public void listener1(String msg){        System.out.println("receiver1接收到的消息:"+msg);    }    @RabbitListener(queues = "queue1")    public void listener2(String msg){        System.out.println("receiver2接收到的消息:"+msg);    }}

测试:

receiver2接收到的消息:0receiver1接收到的消息:1receiver2接收到的消息:2receiver1接收到的消息:3receiver2接收到的消息:5receiver1接收到的消息:4receiver2接收到的消息:6receiver1接收到的消息:7receiver1接收到的消息:8receiver2接收到的消息:9receiver2接收到的消息:10receiver1接收到的消息:11receiver2接收到的消息:12receiver1接收到的消息:13receiver1接收到的消息:15receiver2接收到的消息:14receiver1接收到的消息:16receiver2接收到的消息:17receiver1接收到的消息:19receiver2接收到的消息:18receiver1接收到的消息:20receiver2接收到的消息:21receiver1接收到的消息:22receiver2接收到的消息:23receiver2接收到的消息:25receiver1接收到的消息:24receiver1接收到的消息:27
尝试多对多:

控制层:

package com.example.demo;import org.springframework.amqp.rabbit.core.RabbitTemplate;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class RabbitController {    @Autowired    RabbitTemplate rabbitTemplate;    @RequestMapping("/rm")    public void sendMsg(){        for(int i=0;i<20;i++){            rabbitTemplate.convertAndSend("queue1","1:"+i);            rabbitTemplate.convertAndSend("queue1","2:"+i);        }    }}
消息接收:

package com.example.demo;import org.springframework.amqp.rabbit.annotation.RabbitListener;import org.springframework.stereotype.Component;//接收消息@Componentpublic class RabbitReceive {    @RabbitListener(queues="queue1")    public void listener1(String msg){        System.out.println("receiver1接收到的消息:"+msg);    }    @RabbitListener(queues = "queue1")    public void listener2(String msg){        System.out.println("receiver2接收到的消息:"+msg);    }}
测试:
receiver1接收到的消息:2:0receiver2接收到的消息:1:0receiver1接收到的消息:1:1receiver2接收到的消息:2:1receiver1接收到的消息:1:2receiver2接收到的消息:2:2receiver2接收到的消息:2:3receiver1接收到的消息:1:3receiver2接收到的消息:1:4receiver1接收到的消息:2:4receiver2接收到的消息:1:5receiver1接收到的消息:2:5receiver2接收到的消息:1:6receiver1接收到的消息:2:6receiver2接收到的消息:1:7receiver1接收到的消息:2:7receiver2接收到的消息:1:8receiver1接收到的消息:2:8receiver2接收到的消息:1:9receiver1接收到的消息:2:9receiver2接收到的消息:1:10receiver1接收到的消息:2:10receiver2接收到的消息:1:11receiver1接收到的消息:2:11receiver2接收到的消息:1:12receiver1接收到的消息:2:12receiver2接收到的消息:1:13receiver1接收到的消息:2:13receiver2接收到的消息:1:14receiver1接收到的消息:2:14receiver1接收到的消息:2:15
在RabbitMq  Web管理端查看:







原创粉丝点击