前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >spring boot框架学习12-spring boot整合active mq方法1

spring boot框架学习12-spring boot整合active mq方法1

作者头像
凯哥Java
发布2019-06-30 19:46:49
6100
发布2019-06-30 19:46:49
举报
文章被收录于专栏:凯哥Java凯哥Java

本章节主要内容:

主要从以下几个方面讲解:

mybatis和spring boot整合、redis(单机版)和spring boot整合、redis(集群)和spring boot整合、httpclient和spring boot整合、rabbitMQ/active MQ和spring boot整合。

欢迎关注凯哥微信公众号:凯哥Java(kaigejava)

欢迎访问凯哥个人博客:www.kaigejava.com

本节主要内容:

1:spring boot整合active mq方案一

方案一是简单的,生产者和消费者都在同一个应用项目中。

一:active mq相关

1:active mq下载:

2:启动

在bin文件夹下有64和32位找到自己系统响应的文件夹打开。凯哥使用的是64位的

双击activemq.bat批处理就启动了。

启动如下图:

二:spring boot中应用active mq

2.1:在pom.xml文件中添加mq相关的依赖。

<!-- actionMQ相关的 start -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-activemq</artifactId>

</dependency>

<!-- 如果此处设置为true,需要加如下的依赖包,否则会自动配置失败,报JmsMessagingTemplate注入失败 -->

<dependency>

<groupId>org.apache.activemq</groupId>

<artifactId>activemq-pool</artifactId>

<!-- <version>5.7.0</version> -->

</dependency>

<!-- actionMQ相关的 end -->

2.2:MQ配置类

注:必须使用@Configuration 或者是spring 其他注解。这样该类才可以被spring管理。

setBrokerURL是添加url的

完整代码如下:

package com.kaigejava.springconfig.mq;

import org.apache.activemq.ActiveMQConnectionFactory;

import org.apache.activemq.pool.PooledConnectionFactory;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.jms.connection.SingleConnectionFactory;

import org.springframework.jms.core.JmsTemplate;

@Configuration

public class ActiveMQConfig {

@Bean

public ActiveMQConnectionFactory targetConnectionFactory(){

ActiveMQConnectionFactory targetConnectionFactory = new ActiveMQConnectionFactory();

targetConnectionFactory.setBrokerURL("tcp://localhost:61616");

return targetConnectionFactory;

}

@Bean

public SingleConnectionFactory connectionFactory( PooledConnectionFactory targetConnectionFactory){

SingleConnectionFactory connectionFactory = new SingleConnectionFactory();

connectionFactory.setTargetConnectionFactory(targetConnectionFactory);

return connectionFactory;

}

@Bean

public PooledConnectionFactory pooledConnectionFactory(ActiveMQConnectionFactory targetConnectionFactory){

PooledConnectionFactory pooledConnectionFactory = new PooledConnectionFactory();

pooledConnectionFactory.setConnectionFactory(targetConnectionFactory);

pooledConnectionFactory.setMaxConnections(10);

return pooledConnectionFactory;

}

@Bean

public JmsTemplate jmsTemplate(SingleConnectionFactory connectionFactory){

JmsTemplate jmsTemplate = new JmsTemplate();

jmsTemplate.setConnectionFactory(connectionFactory);

return jmsTemplate;

}

}

2.3:生产者配置:

这里记录日志时候使用的MQ生产者。

/**

*

* @ClassName: Producer

* @Description: 后台通过MQ记录日志的生成者

* @author 凯哥Java

*

*

*/

@Service("adminLogProducer")  

public class AdminLogProducer {  

   @Autowired // 也可以注入JmsTemplate,JmsMessagingTemplate对JmsTemplate进行了封装  

   private JmsMessagingTemplate jmsTemplate;  

   // 发送消息,destination是发送到的队列,message是待发送的消息  

   public void sendMessage(Destination destination, final String message){  

       jmsTemplate.convertAndSend(destination, message);  

   }  

}  

2.4:消费者:消费者进行消费处理。

2.5:在service层使用:

使用步骤:

2.5.1:注入mq生产者

2.5.2:准备准备发射的数据

2.5.3:创建一个目标并指定消费者

Destination destination = new ActiveMQQueue("adminLogConsumerQuenue.loggingClassName");

2.5.4:调用生产者的方法进行发送

adminLogProducer.sendMessage(destination,sendmsg);

2.6:生产者发送类:

至此spring boot整合active MQ方案一完成。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-12-08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档