Spring boot 集成MQ
import lombok.extern.java.Log;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; /**
* @author 周志伟
* @projectname 项目名称: ${project_name}
* @classname: RabbitConfig
* @description:
* @date 2018/6/22:10:00
*/
@Configuration
@Log
public class RabbitConfig {
@Qualifier("firstConnectionFactory")
@Autowired
public ConnectionFactory connectionFactory; @Autowired
public MQProperties mqProperties; @Bean(name="firstRabbitTemplate")
public RabbitTemplate firstRabbitTemplate(){
log.info("########################初始化rabitTemplate################################");
RabbitTemplate firstRabbitTemplate = new RabbitTemplate(connectionFactory);
return firstRabbitTemplate;
} @Bean
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory() {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
factory.setMessageConverter(new Jackson2JsonMessageConverter());
return factory;
} @Bean
public TopicExchange defaultExchange() {
return new TopicExchange(mqProperties.getCar_exchange());
}
package com.car.config; import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; /**
* @author 周志伟
* @projectname 项目名称: ${project_name}
* @classname: ConnectionFactoryConfig
* @description:初始化数据源
* @date 2018/6/22:11:30
*/
@Configuration
public class ConnectionFactoryConfig {
@Bean(name="firstConnectionFactory")
public ConnectionFactory firstConnectionFactory(@Value("${spring.rabbitmq.car_host}") String host,
@Value("${spring.rabbitmq.car_port}") int port,
@Value("${spring.rabbitmq.car_username}") String username,
@Value("${spring.rabbitmq.car_password}") String password,
@Value("${spring.rabbitmq.car_vhost}") String virtualhost
){
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
connectionFactory.setHost(host);
connectionFactory.setPort(port);
connectionFactory.setUsername(username);
connectionFactory.setPassword(password);
connectionFactory.setVirtualHost(virtualhost);
return connectionFactory;
}
}
package com.car.config; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component; /**
* @author 周志伟
* @projectname 项目名称: ${project_name}
* @classname: RabbitMQ_Queue
* @description:创建队列
* @date 2018/6/22:13:09
*/
@Configuration
@Component
public class RabbitMQQueue extends RabbitConfig{
Logger logger = LoggerFactory.getLogger(RabbitMQQueue.class); public RabbitMQQueue(){
logger.info("############### RabbitMQ Queue 创建 ###############");
} @Bean
public Queue CreationCarQueue() {
logger.info("创建队列:{}", mqProperties.getCar_queuename());
return new Queue(mqProperties.getCar_queuename());
} @Bean
public Binding binding() {
logger.info("绑定队列,exchange:{},routingKey:{},queueName:{}", mqProperties.getCar_exchange(), mqProperties.getCar_routingkey(), mqProperties.getCar_queuename());
return BindingBuilder.bind(CreationCarQueue()).to(defaultExchange()).with(mqProperties.getCar_routingkey());
}
}
package com.car.modules.MqMsg.mq; import com.carloan.feign.info.InserttheorderServiceFeign;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component; import java.io.UnsupportedEncodingException; /**
* @author 周志伟
* @projectname 项目名称: ${project_name}
* @classname: ReqCarMqMsg
* @description:配置监听
* @date 2018/5/28:10:50
*/ @Component
public class ReqCarMqMsg{ private Logger logger = LoggerFactory.getLogger(ReqCarMqMsg.class); @RabbitListener(queues = "${spring.rabbitmq.car_queuename}", containerFactory = "rabbitListenerContainerFactory")
public void process(@Payload byte[] bytes) throws UnsupportedEncodingException {
String result = null;
try {
result = new String(bytes, "UTF8");
logger.info("监听到消息==========="+ result);
} catch (UnsupportedEncodingException e) {
logger.error("监听车贷门店Mq: insuranceTrial UnsupportedEncodingException error :{}",e);
} catch (Exception e){
logger.error("监听车贷门店Mq: insuranceTrial Exception error :{}",e);
}
}
}
package com.car.modules.MqMsg.mq; import com.car.config.MQProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component; import java.util.UUID; /**
* @author 周志伟
* @projectname 项目名称: ${project_name}
* @classname: PushMq
* @description:推送消息
* @date 2018/6/25:9:52
*/
@Component
public class PushMq {
Logger logger = LoggerFactory.getLogger(PushMq.class); @Qualifier("firstRabbitTemplate")
@Autowired
private RabbitTemplate rabbitTemplate;
@Autowired
public MQProperties mqProperties; public void pushmessage(String message){
try{
CorrelationData correlationId = new CorrelationData(UUID.randomUUID().toString());
rabbitTemplate.convertAndSend(mqProperties.getCar_exchange(), mqProperties.getCar_put_routingkey(), message,correlationId);
logger.info("推送消息:{}", message);
}catch (Exception e){
logger.error("推送异常:{}",e);
} }
}
Spring boot 集成MQ的更多相关文章
- Spring boot集成Rabbit MQ使用初体验
Spring boot集成Rabbit MQ使用初体验 1.rabbit mq基本特性 首先介绍一下rabbitMQ的几个特性 Asynchronous Messaging Supports mult ...
- Spring boot集成RabbitMQ(山东数漫江湖)
RabbitMQ简介 RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统 MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法.应用程序通过读写出 ...
- 【spring boot】【redis】spring boot 集成redis的发布订阅机制
一.简单介绍 1.redis的发布订阅功能,很简单. 消息发布者和消息订阅者互相不认得,也不关心对方有谁. 消息发布者,将消息发送给频道(channel). 然后是由 频道(channel)将消息发送 ...
- Spring Boot 集成 RabbitMQ 实战
Spring Boot 集成 RabbitMQ 实战 特别说明: 本文主要参考了程序员 DD 的博客文章<Spring Boot中使用RabbitMQ>,在此向原作者表示感谢. Mac 上 ...
- Spring Boot集成Jasypt安全框架
Jasypt安全框架提供了Spring的集成,主要是实现 PlaceholderConfigurerSupport类或者其子类. 在Sring 3.1之后,则推荐使用PropertySourcesPl ...
- Spring boot集成swagger2
一.Swagger2是什么? Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件. Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格 ...
- Spring Boot 集成 Swagger,生成接口文档就这么简单!
之前的文章介绍了<推荐一款接口 API 设计神器!>,今天栈长给大家介绍下如何与优秀的 Spring Boot 框架进行集成,简直不能太简单. 你所需具备的基础 告诉你,Spring Bo ...
- spring boot 集成 zookeeper 搭建微服务架构
PRC原理 RPC 远程过程调用(Remote Procedure Call) 一般用来实现部署在不同机器上的系统之间的方法调用,使得程序能够像访问本地系统资源一样,通过网络传输去访问远程系统资源,R ...
- Spring Boot 集成Swagger
Spring Boot 集成Swagger - 小单的博客专栏 - CSDN博客https://blog.csdn.net/catoop/article/details/50668896 Spring ...
随机推荐
- Python强大的日志模块logging
前言 日志是对于软件执行所发生的事件的一种追踪记录方式.日常使用过程中对代码执行的错误和问题会进行查看日志来分析定位问题所在.平常编写代码以及调试也经常用到.通常的新手的做法是直接print打印,但是 ...
- Netty tcnative boringssl windows 32-bit 编译
1 问题 在使用Netty SSL时,我们往往会采用netty-tcnative-boringssl组件.但是netty-tcnative-boringssl在Windows上仅有64位版本的,没有3 ...
- MySQL [ERROR] Table 'mysql.user' doesn't exist
问题描述: 在安装MYsql时,/etc/init.d/mysqld start时报错: [root@master data]# /etc/init.d/mysqld start Starting M ...
- vue踩坑记,持续更新中......
1.运行项目报错 you may use special comments to disable some waring. use //eslint-disable-next-line.....吧啦吧 ...
- sqlserver 清除表数据和拷贝表结构的操作
最近在做一个ERP系统需要导入数据,因此用到了sql的一些操作,在这里记录一下. 1.清除表数据: Delete from 表名称 where XXX 2.拷贝表结构,需求是新增一个和某个表数据格式一 ...
- WEB安全讨论-表单登录是先验证验证码还是密码
表单登录是先验证验证码还是密码? 肯定是验证码呀!!!这是毋庸置疑的.但是发现有人会验证密码,感觉先验证密码和先验证验证码是一个概念是一样的.但是其实是完全不一样的.下面我们来一起详细的剖析一下: 消 ...
- 经典项目管理 OR 敏捷项目管理,我该怎么选?
CODING 项目协同近期为支持传统项目管理推出了「经典项目管理」.至此,CODING 已全面支持敏捷项目管理以及传统项目管理.那么问题来了,「经典项目管理」和「敏捷项目管理」,我该怎么选呢?本文将从 ...
- Java 中 Executors.newSingleThreadExecutor() 与Executors.newFixedThreadPool(1)有什么区别
在研究Executors提供的线程池时自然会想到标题这个问题,既然已经有了newFixedThreadPool,为什么还要存在newSingleThreadExecutor这个方法.难道newFixe ...
- 【JavaWeb】JSTL 标签库
JSTL 标签库 简介 JSTL(JSP Standard Tag Library),即 JSP 标准标签库.标签库是为了替换代码脚本,使得整个 jsp 页面变得更加简洁. JSTL 有五个功能不同的 ...
- 了解一下ajax
AJAX:是一种无需重新加载页面的情况下能够更新部分(局部更新)网页的技术. 1. 概念:ASychronous JavaScript And XML 异步的JavaScript和XML 首先了解一下 ...