当前位置:首页> 正文
SpringBoot整合RabbitMQ五种常用模型
1.SpringBoot整合RabbitMQ使用
1.引入依赖:
<!--引入与rabbitmq集成的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
2.做配置 :application.yml
spring:
application:
name: rabbitmq-springboot
rabbitmq:
host: 192.168.231.141
port: 5672
username: ems
password: 123456
virtual-host: /ems
3.hello world
@SpringBootTest(classes = RabbitmqSpringbootApplication.class)
@RunWith(SpringRunner.class)
@ComponentScan("com.liziyi")
// @EnableRabbit启用@RabbitListener
@EnableRabbit
public class TestRabbitMQ {
// 注入rabbitTemplate
@Autowired
private RabbitTemplate rabbitTemplate;
// hello world
@Test
public void test() {
rabbitTemplate.convertAndSend("hello","hello world");
}
}
// 被工厂扫描道
@Component
// 消费者(声明队列)
@RabbitListener(queuesToDeclare = @Queue("hello"))
public class HelloCustomer {
// 代表通过下面方法取消息
@RabbitHandler
public void receivel(String message) {
System.out.println("message = " + message);
}
}
但是最后执行了。在网页上没有见到新建的队列
4.work
@SpringBootTest(classes = RabbitmqSpringbootApplication.class)
@RunWith(SpringRunner.class)
@ComponentScan("com.liziyi")
// @EnableRabbit启用@RabbitListener
@EnableRabbit
public class TestRabbitMQ {
// 注入rabbitTemplate
@Autowired
private RabbitTemplate rabbitTemplate;
// work
@Test
public void testWork() {
for (int i = 0; i < 10; i++) {
rabbitTemplate.convertAndSend("work","work模型"+i);
}
}
}
@Component
public class WorkCustomer {
@RabbitListener(queuesToDeclare = @Queue("work"))
public void receivel(String message) {
System.out.println("message1 =" +message);
}
@RabbitListener(queuesToDeclare = @Queue("work"))
public void receivel2(String message) {
System.out.println("message2 =" +message);
}
}
5.fanout
@SpringBootTest(classes = RabbitmqSpringbootApplication.class)
@RunWith(SpringRunner.class)
@ComponentScan("com.liziyi")
// @EnableRabbit启用@RabbitListener
@EnableRabbit
public class TestRabbitMQ {
// 注入rabbitTemplate
@Resource
private RabbitTemplate rabbitTemplate;
// fanout 广播
@Test
public void testFanout() {
rabbitTemplate.convertAndSend("logs","","fanout模型");
}
|
展开全文阅读
相关内容
-
电脑载图快捷键|电脑快捷键大全常用图片
电脑载图快捷键|电脑快捷键大全常用图片,,1. 电脑快捷键大全常...
-
基于kibana的Dev Tools控制板上es常用查询语句
基于kibana的Dev Tools控制板上es常用查询语句,索引,模板,1、...
-
cad布局常用快捷键|cad布局常用快捷键命令
cad布局常用快捷键|cad布局常用快捷键命令,,1. cad布局常用快...
-
cad平面设备快捷键|cad机械设计常用快捷键命令
cad平面设备快捷键|cad机械设计常用快捷键命令,,1. cad机械设...
-
lol常用设置|lol有用的设置
lol常用设置|lol有用的设置,,lol常用设置如何设置英雄联盟手游...
-
excel常用函数都有哪些?
excel常用函数都有哪些?,函数,哪些,常用,1、SUM函数:SUM函数的作...
-
什么牌子的笔记本好用|常用的笔记本电脑品牌
什么牌子的笔记本好用|常用的笔记本电脑品牌,,常用的笔记本电...
-
fcpx重做快捷键|fcpx常用快捷键
fcpx重做快捷键|fcpx常用快捷键,,1. fcpx常用快捷键command+T,f...
-
ps各种快捷键|PS快捷键大全常用
ps各种快捷键|PS快捷键大全常用,,PS快捷键大全常用工具类快捷...
-
postgresql的ALTER常用操作
postgresql的ALTER常用操作,字段,数据类型,postgresql版本:psql...
-
做模型电脑|电脑上做模型的软件
做模型电脑|电脑上做模型的软件,,电脑上做模型的软件建模软件 ...
-
电脑桌规格型号|常用电脑桌尺寸
电脑桌规格型号|常用电脑桌尺寸,,常用电脑桌尺寸问:台式电脑桌...
-
日本的“薙刀”是怎样一种武器,为何会成为女性和
日本的“薙刀”是怎样一种武器,为何会成为女性和僧兵的常用武...
-
计算机初学者教程:常用英语缩略语的意义分析
计算机初学者教程:常用英语缩略语的意义分析,,在使用电脑过程中...
-
常用cad快捷键|常用cad快捷键口诀表2015
常用cad快捷键|常用cad快捷键口诀表2015,,常用cad快捷键口诀表...