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的更多相关文章

  1. Spring boot集成Rabbit MQ使用初体验

    Spring boot集成Rabbit MQ使用初体验 1.rabbit mq基本特性 首先介绍一下rabbitMQ的几个特性 Asynchronous Messaging Supports mult ...

  2. Spring boot集成RabbitMQ(山东数漫江湖)

    RabbitMQ简介 RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统 MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法.应用程序通过读写出 ...

  3. 【spring boot】【redis】spring boot 集成redis的发布订阅机制

    一.简单介绍 1.redis的发布订阅功能,很简单. 消息发布者和消息订阅者互相不认得,也不关心对方有谁. 消息发布者,将消息发送给频道(channel). 然后是由 频道(channel)将消息发送 ...

  4. Spring Boot 集成 RabbitMQ 实战

    Spring Boot 集成 RabbitMQ 实战 特别说明: 本文主要参考了程序员 DD 的博客文章<Spring Boot中使用RabbitMQ>,在此向原作者表示感谢. Mac 上 ...

  5. Spring Boot集成Jasypt安全框架

    Jasypt安全框架提供了Spring的集成,主要是实现 PlaceholderConfigurerSupport类或者其子类. 在Sring 3.1之后,则推荐使用PropertySourcesPl ...

  6. Spring boot集成swagger2

    一.Swagger2是什么? Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件. Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格 ...

  7. Spring Boot 集成 Swagger,生成接口文档就这么简单!

    之前的文章介绍了<推荐一款接口 API 设计神器!>,今天栈长给大家介绍下如何与优秀的 Spring Boot 框架进行集成,简直不能太简单. 你所需具备的基础 告诉你,Spring Bo ...

  8. spring boot 集成 zookeeper 搭建微服务架构

    PRC原理 RPC 远程过程调用(Remote Procedure Call) 一般用来实现部署在不同机器上的系统之间的方法调用,使得程序能够像访问本地系统资源一样,通过网络传输去访问远程系统资源,R ...

  9. Spring Boot 集成Swagger

    Spring Boot 集成Swagger - 小单的博客专栏 - CSDN博客https://blog.csdn.net/catoop/article/details/50668896 Spring ...

随机推荐

  1. java interface和class中的协变

    协变 Java中的协变是指,当发生继承时,子类中重写父类的方法时,可以返回父类方法返回类型的子类型.比如: class SuperClass{} class SubClass extends Supe ...

  2. 风炫安全WEB安全学习第三十八节课 越权漏洞演示与讲解

    风炫安全WEB安全学习第三十八节课 越权漏洞演示与讲解 越权漏洞 0x01 漏洞介绍 越权漏洞的危害与影响主要是与对应业务的重要性相关,比如说某一页面服务器端响应(不局限于页面返回的信息,有时信息在响 ...

  3. 使用python做一个IRC在线下载器

    使用python做一个IRC在线下载器 1.开发流程 2.软件流程 3.开始 3.0 准备工作 3.1寻找API接口 3.2 文件模块 3.2.1 选择文件弹窗 3.2.2 提取文件名 3.2.2.1 ...

  4. 剑指offer 面试题9:用两个栈实现队列

    题目描述 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 使用栈实现队列的下列操作:push(x) -- 将一个元素放入队列的尾部.pop() -- 从队列首部移 ...

  5. 拍摄、剪辑vlog的思路

    这篇文章是看了很多狂阿弥_ 的作品后 产生的一些小小总结.这些技法只是锦上添花,阿弥作品真正好的地方在于他细腻的情感,真实的对白,和打动人心的满分作文. 优秀的Vlog ,在于它和观众产生的情感共鸣. ...

  6. Mybatis 报错java.sql.SQLException: No suitable driver found for http://www.example.com

    运行项目报错 Error querying database. Cause: java.sql.SQLException: No suitable driver found for http://ww ...

  7. [Usaco2009 Feb]Bullcow 牡牛和牝牛

    原题链接https://www.lydsy.com/JudgeOnline/problem.php?id=3398 容易想到的一种\(dp\)就是:设\(dp[i][j]\)表示前\(i\)头牛里面有 ...

  8. 1.2V升压到3V和3.3V的升压芯片

    1.2V镍氢电池升压到3V和3.3V输出,1.2V升压3V,1.2V升压3.3V稳压输出供电的芯片. PW5100 是一款低静态电流.达效率. PFM 模式控制的同步升压变换器. PW5100 所需的 ...

  9. Java异常处理场景中不同位置的返回值详细解析

    Java 异常处理中的返回值在不同位置不同场景下是有一些差别的,这里需要格外注意 具体分以下两种场景: 1 finally语句块没有return语句,即当代码执行到try或者catch语句块中的ret ...

  10. 一种优化递归算法的方法(javascript)

    看书的时候看到了这个比较酷的方法,分享一下. 一.问题描述:代码如下,我们以计算阶乘(factorial)为例,当重复调用factorial(9),factorial(8),factorial(7)的 ...