ActiveMQ
今天初学了ActiveMQ,MQ的好处又很多、有解决耦合问题、流量削峰、减低延迟等好处多的也就不介绍了。
这是我所画的图,是spring和ActiveMQ整合的流程
那么接下来就直接进入整合的步骤
首先找到Active官网下载可以查看MQ消息的页面(百度也可以下载到)
下载完成后压缩文件打开解压好的MQ/bin/win64或者win32/运行activemq.bat
然后去网页输入http://127.0.0.1:8161 进入MQ的查询消息页面,官方给的账号密码都是admin进行登录
接下来就是简单的demo环节了
pom.xml文件
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
<version>5.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.9.0</version>
</dependency>
</dependencies>
配置spring容器
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 -->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://localhost:61616" />
</bean>
<!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
<bean id="connectionFactory"
class="org.springframework.jms.connection.SingleConnectionFactory">
<!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
<property name="targetConnectionFactory" ref="targetConnectionFactory" />
</bean>
<!-- 配置生产者 -->
<!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->
<property name="connectionFactory" ref="connectionFactory" />
</bean>
<!--这个是队列目的地,点对点的如果没有监听者会对数据进行缓存 -->
<bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg>
<value>spring-And-queue</value>
</constructor-arg>
</bean>
<!--这个是主题目的地,一对多的使用,但是如果没有监听者不会对数据进行缓存,也可以去配置 -->
<bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg value="topic" />
</bean>
</beans>
编写消费者代码
public class TestActive {
/**
* 测试生产者
*/
@Test
public void demoProduce(){
//初始化容器
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
//从容器得到jmsTemplate对象
JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class);
//从容器获得Destination对象
Destination destination = (Destination) context.getBean("topicDestination");
//发送消息 ,参数2是一个创建消息的接口
jmsTemplate.send(destination, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
//创建消息
TextMessage textMessage = session.createTextMessage();
textMessage.setText("你好啊,我是生产消息者,很高兴认识你" );
return textMessage;
}
});
}
}
编写消费者代码(实现消息监听器)
public class CustomerMessage implements MessageListener{
/**
* 消费者
*/
public void onMessage(Message msg) {
try {
//TextMessage继承Message
TextMessage textMessage = (TextMessage) msg;
//接收消息
String text = textMessage.getText();
System.out.println("消费者监听到的消息 : "+text);
} catch (JMSException e) {
e.printStackTrace();
}
}
}
关键的配置消费者的spring容器(和上面的是连着的)
-------------------------------和上面的spring容器是一体的---------------------------------
<!-- 配置监听者 -->
<bean id="customerMessage" class="com.hbsi.demo.CustomerMessage"></bean>
<!-- 消息监听容器 -->
<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<!-- destination监听是一对一的还是多对多的,监听那个就获取那个的消息 -->
<property name="destination" ref="topicDestination" /> //注意此处,和上面的是配套的,填写queue就用一对一的,配置topic的就用一对多的,自选
<property name="messageListener" ref="customerMessage" />
</bean>
测试
public class TestCustomerMessage {
/**
* 接收监听到的消息
* @throws IOException
*/
@SuppressWarnings("resource")
@Test
public void customerMessage() throws IOException{
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
System.in.read();
}
}
测试一对一(需要把生产者的getBean的id和配置监听者下面的ref对应,一对一是queueDestination)
测试一对多(需要生产者的getbean和spring配置文件的对应 topicDestination)
因为前面我有测试了几次所以变成了7
总结
对于MQ的学习可能我只是刚刚入门,此篇文章也只是怕将来自己忘记所留,真正的学习也才刚刚开始,也希望自己能够找到理想的工作吧,加油!2019.2.28日留..