本章节主要内容:

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

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

接下来我们学习第二小节:spring boot整合redis开启缓存

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


本节主要内容:


1:spring boot整合active mq方案一

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

一:active mq相关

1:active mq下载:


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


2:启动


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


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


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


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

启动如下图:


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


二:spring boot中应用active mq

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


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


<!-- 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配置类


spring boot框架学习12-spring boot整合active mq方法1_web开发_07


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

setBrokerURL是添加url的


完整代码如下:

package ;

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:生产者配置:


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


这里记录日志时候使用的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:消费者:消费者进行消费处理。


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


2.5:在service层使用:


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


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


使用步骤:

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框架学习12-spring boot整合active mq方法1_redis_12


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