springboot+rabbitmq例子
demo目录

贴代码
1.ProducerConfig.java
package com.test.config; import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitMessagingTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; /**
* Created by admin on 2017/6/1 13:23.
*/
@Configuration
public class ProducerConfig {
@Bean
public RabbitMessagingTemplate msgMessageTemplate(ConnectionFactory connectionFactory) {
RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
//参数列表分别是:1.交换器名称(default.topic 为默认值),2.是否长期有效,3.如果服务器在不再使用时自动删除交换器
TopicExchange exchange = new TopicExchange("default.topic", true, false);
rabbitAdmin.declareExchange(exchange);
//1.队列名称,2.声明一个持久队列,3.声明一个独立队列,4.如果服务器在不再使用时自动删除队列
Queue queue = new Queue("test.demo.send", true, false, false);
rabbitAdmin.declareQueue(queue);
//1.queue:绑定的队列,2.exchange:绑定到那个交换器,3.test2.send:绑定的路由名称
rabbitAdmin.declareBinding(BindingBuilder.bind(queue).to(exchange).with("test2.send"));
return RabbitUtil.simpleMessageTemplate(connectionFactory);
}
}
2.RabbitMQConfig.java
package com.test.config; import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; /**
* Created by admin on 2017/6/1 11:26.
*/
@Configuration
public class RabbitMQConfig {
/**
* 注入配置文件属性
*/
@Value("${spring.rabbitmq.addresses}")
String addresses;//MQ地址
@Value("${spring.rabbitmq.username}")
String username;//MQ登录名
@Value("${spring.rabbitmq.password}")
String password;//MQ登录密码
@Value("${spring.rabbitmq.virtual-host}")
String vHost;//MQ的虚拟主机名 /**
* 创建 ConnectionFactory
*
* @return
* @throws Exception
*/
@Bean
public ConnectionFactory connectionFactory() throws Exception {
return RabbitUtil.connectionFactory(addresses, username, password, vHost);
} /**
* 创建 RabbitAdmin
*
* @param connectionFactory
* @return
* @throws Exception
*/
@Bean
public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) throws Exception {
RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
return rabbitAdmin;
} }
3.RabbitUtil.java
package com.test.config; import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitMessagingTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.messaging.converter.GenericMessageConverter; /**
* RabbitMQ 公共类
* Created by admin on 2017/6/1 11:25.
*/
public class RabbitUtil { /**
* 初始化 ConnectionFactory
*
* @param addresses
* @param username
* @param password
* @param vHost
* @return
* @throws Exception
*/
public static ConnectionFactory connectionFactory(String addresses, String username, String password, String vHost) throws Exception {
CachingConnectionFactory factoryBean = new CachingConnectionFactory();
factoryBean.setVirtualHost(vHost);
factoryBean.setAddresses(addresses);
factoryBean.setUsername(username);
factoryBean.setPassword(password);
return factoryBean;
} /**
* 初始化 RabbitMessagingTemplate
*
* @param connectionFactory
* @return
*/
public static RabbitMessagingTemplate simpleMessageTemplate(ConnectionFactory connectionFactory) {
RabbitTemplate template = new RabbitTemplate(connectionFactory);
RabbitMessagingTemplate rabbitMessagingTemplate = new RabbitMessagingTemplate();
rabbitMessagingTemplate.setMessageConverter(new GenericMessageConverter());
rabbitMessagingTemplate.setRabbitTemplate(template);
return rabbitMessagingTemplate;
}
}
4.Student.java
package com.test.model; import java.io.Serializable; /**
* Created by admin on 2017/6/1 13:36.
*/
public class Student implements Serializable {
private String name;
private Integer age;
private String address; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
}
}
5.Consumers.java
package com.test.task; import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service; /**
* Created by admin on 2017/6/1 13:29.
*/
@Service
public class Consumers { @RabbitListener(
//1.rabbitAdmin:RabbitAdmin名称
admin = "rabbitAdmin",
bindings = @QueueBinding(
//1.test.demo.send:队列名,2.true:是否长期有效,3.false:是否自动删除
value = @Queue(value = "test.demo.send", durable = "true", autoDelete = "false"),
//1.default.topic交换器名称(默认值),2.true:是否长期有效,3.topic:类型是topic
exchange = @Exchange(value = "default.topic", durable = "true", type = "topic"),
//test2.send:路由的名称,ProducerConfig 里面 绑定的路由名称(xxxx.to(exchange).with("test2.send")))
key = "test2.send")
)
public void test(Object obj) {
System.out.println("receive....");
System.out.println("obj:" + obj.toString());
}
}
6.Producers.java
package com.test.task; import com.test.model.Student;
import org.springframework.amqp.rabbit.core.RabbitMessagingTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; /**
* Created by admin on 2017/6/1 13:35.
*/
@Service
public class Producers { @Autowired
RabbitMessagingTemplate rabbitSendTemplate; public void send(Student student) {
System.out.println("send start.....");
rabbitSendTemplate.convertAndSend("default.topic", "test2.send", student);
}
}
7.TestController.java
package com.test.test; import com.test.model.Student;
import com.test.task.Producers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody; /**
* Created by admin on 2017/6/1 13:38.
*/
@Controller
@RequestMapping(value = "/test")
public class TestController { @Autowired
Producers producers; @RequestMapping(value = "/send", method = RequestMethod.GET)
@ResponseBody
public void test() {
Student s = new Student();
s.setName("zhangsan");
s.setAddress("wuhan");
s.setAge(20);
producers.send(s);
} }
8.MainApplication.java
package com.test; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; /**
* Created by admin on 2017/6/1 11:19.
*/
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
System.getProperties().put("test", "test");
SpringApplication.run(MainApplication.class, args); }
}
9.application.yml
server:
address: 192.168.200.117 #自己主机的IP地址
port: 8000 #端口
spring:
rabbitmq:
addresses: 192.168.200.119:5672 #MQ IP 和 端口
username: admin #MQ登录名
password: 123456 #MQ登录密码
virtual-host: test #MQ的虚拟主机名称
10.build.gradle
group 'rabbitmqtest'
version '1.0-SNAPSHOT' apply plugin: 'java' sourceCompatibility = 1.8 repositories {
mavenCentral()
} dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
testCompile("org.springframework.boot:spring-boot-starter-test:1.3.5.RELEASE")
compile("org.springframework.boot:spring-boot-starter-web:1.3.5.RELEASE")
compile(group: 'org.springframework.amqp', name: 'spring-rabbit', version: "1.6.1.RELEASE")
}
11.settings.gradle
rootProject.name = 'rabbitmqtest'
页面访问 192.168.200.117:8000/test/send 可以看到控制台有日志输出,发送的消息立即消费掉了

MQ的队列里面也是空的

如果把消费者的代码注掉,再访问刚才的 url 地址 队列里面就会多一条


springboot+rabbitmq例子的更多相关文章
- springboot+rabbitmq整合示例程
关于什么是rabbitmq,请看另一篇文: http://www.cnblogs.com/boshen-hzb/p/6840064.html 一.新建maven工程:springboot-rabbit ...
- SpringBoot RabbitMQ 延迟队列代码实现
场景 用户下单后,如果30min未支付,则删除该订单,这时候就要可以用延迟队列 准备 利用rabbitmq_delayed_message_exchange插件: 首先下载该插件:https://ww ...
- springboot rabbitmq 死信队列应用场景和完整demo
何为死信队列? 死信队列实际上就是,当我们的业务队列处理失败(比如抛异常并且达到了retry的上限),就会将消息重新投递到另一个Exchange(Dead Letter Exchanges),该Exc ...
- springboot + rabbitmq 做智能家居,我也没想到会这么简单
本文收录在个人博客:www.chengxy-nds.top,共享技术资源,共同进步 前一段有幸参与到一个智能家居项目的开发,由于之前都没有过这方面的开发经验,所以对智能硬件的开发模式和技术栈都颇为好奇 ...
- springboot + rabbitmq 用了消息确认机制,感觉掉坑里了
本文收录在个人博客:www.chengxy-nds.top,技术资源共享,一起进步 最近部门号召大伙多组织一些技术分享会,说是要活跃公司的技术氛围,但早就看穿一切的我知道,这 T M 就是为了刷KPI ...
- 带着新人学springboot的应用06(springboot+RabbitMQ 中)
上一节说了这么多废话,看也看烦了,现在我们就来用鼠标点点点,来简单玩一下这个RabbitMQ. 注意:这一节还是不用敲什么代码,因为上一节我们设置了那个可视化工具,我们先用用可视化工具熟悉一下流程. ...
- springboot rabbitmq整合
这一篇我们来把消息中间件整合到springboot中 ===================================================================== 首先在 ...
- 刚体验完RabbitMQ?一文带你SpringBoot+RabbitMQ方式收发消息
人生终将是场单人旅途,孤独之前是迷茫,孤独过后是成长. 楔子 这篇是消息队列RabbitMQ的第二弹. 上一篇的结尾我也预告了本篇的内容:利用RabbitTemplate和注解进行收发消息,还有一个我 ...
- SpringBoot+RabbitMQ 方式收发消息
本篇会和SpringBoot做整合,采用自动配置的方式进行开发,我们只需要声明RabbitMQ地址就可以了,关于各种创建连接关闭连接的事都由Spring帮我们了~ 交给Spring帮我们管理连接可以让 ...
随机推荐
- PHP date()函数格式与用法汇总
在页面的最前页加上 date_default_timezone_set("PRC"); /*把时间调到北京时间,php5默认为格林威治标准时间*/ date () a: & ...
- 分享一篇vue项目规范
最近 Vue 用的比较多,而且因为公司里有实习生,当几个人写一个项目的时候,会出现很多问题,最麻烦的就是规范不统一,之前我有一篇文章是说, vue 是比较有规范的一种框架了,但是也会出现很多问题,所以 ...
- YOLO 算法框架的使用一(初级)
YOLO官方框架使用C写的,性能杠杠的,YOLO算法,我就不做过多介绍了.先简单介绍一下这个框架如何使用.这里默认是yolo2,yolo1接近过时.环境 推荐ubuntu 或者centos YOLO是 ...
- (译)学习JavaScript闭包
原文地址:https://medium.freecodecamp.org/lets-learn-javascript-closures-66feb44f6a44 闭包是JavaScript中一个基 ...
- C语言的scanf函数
一. 变量的内存分析 1. 字节和地址 1> 内存以“字节为单位”,Oxffc1,Oxffc2,Oxffc3,Oxffc4....都是字节 ,0x表示的是十六进制 2> 不同类型占用的字节 ...
- [Docker网络]模拟一台交换机的拓扑
[Docker网络]模拟一台交换机的拓扑 本例主要对Docker网络进行实际运用. 背景介绍 一台虚拟机如何模拟成一台多端口交换机分别连接多台虚拟机? bridge网桥技术 实验准备 docker d ...
- thinkphp使用foreach遍历的方法
我们在做一些需求的时候可能会对遍历的上限有一定的要求,这时候就需要对上限进行限定 首先使用foreach遍历的输出数组相比较于volist功能较少 volist标签主要用于在模板中循环输出数据集或者多 ...
- C++反汇编第二讲,不同作用域下的构造和析构的识别
C++反汇编第二讲,不同作用域下的构造和析构的识别 目录大纲: 1.全局(静态)对象的识别,(全局静态全局一样的,都是编译期间检查,所以当做全局对象看即可.) 1.1 探究本质,理解构造和析构的生成, ...
- TreeSet(一)--排序
TreeSet(一) 一.TreeSet定义: 与HashSet是基于HashMap实现一样,TreeSet同样是基于TreeMap实现的. 1)TreeSet类概述 ...
- WCF、WebAPI、WCFREST、WebService之间的区别和选择
转载翻译,原文:http://www.dotnet-tricks.com/Tutorial/webapi/JI2X050413-Difference-between-WCF-and-Web-API-a ...