本文为学习笔记,主要用于记录本人学习过程。内容为转载!!!!
ActiveMQ介绍
MQ是消息中间件,是一种在分布式系统中应用程序借以传递消息的媒介,常用的有ActiveMQ,RabbitMQ,kafka。ActiveMQ是Apache下的开源项目,完全支持JMS1.1和J2EE1.4规范的JMS Provider实现。
特点:
1、支持多种语言编写客户端
2、对spring的支持,很容易和spring整合
3、支持多种传输协议:TCP,SSL,NIO,UDP等
4、支持AJAX
消息形式:
1、点对点(queue)
2、一对多(topic)
ActiveMQ安装
我这里提供一个安装好的虚拟机:
服务器运行后,我们可以直接访问到activeMQ的界面:
然后点击queues可以看到现在没有一条消息:
ActiveMQ测试
编写一个测试类对ActiveMQ进行测试,首先得向pom文件中添加ActiveMQ相关的jar包:
<span style="color:#000000"><code> <span style="color:#006666"><<span style="color:#4f4f4f">dependency</span>></span>
<span style="color:#006666"><<span style="color:#4f4f4f">groupId</span>></span>org.apache.activemq<span style="color:#006666"></<span style="color:#4f4f4f">groupId</span>></span>
<span style="color:#006666"><<span style="color:#4f4f4f">artifactId</span>></span>activemq-all<span style="color:#006666"></<span style="color:#4f4f4f">artifactId</span>></span>
<span style="color:#006666"></<span style="color:#4f4f4f">dependency</span>></span> </code></span>
- queue的发送代码如下:
<span style="color:#000000"><code> <span style="color:#000088">public</span> <span style="color:#000088">void</span> <span style="color:#009900">testMQProducerQueue</span>() <span style="color:#000088">throws</span> Exception{
<span style="color:#880000">//1、创建工厂连接对象,需要制定ip和端口号</span>
ConnectionFactory connectionFactory = <span style="color:#000088">new</span> ActiveMQConnectionFactory(<span style="color:#009900">"tcp://192.168.156.44:61616"</span>);
<span style="color:#880000">//2、使用连接工厂创建一个连接对象</span>
Connection connection = connectionFactory.createConnection();
<span style="color:#880000">//3、开启连接</span>
connection.start();
<span style="color:#880000">//4、使用连接对象创建会话(session)对象</span>
Session session = connection.createSession(<span style="color:#000088">false</span>, Session.AUTO_ACKNOWLEDGE);
<span style="color:#880000">//5、使用会话对象创建目标对象,包含queue和topic(一对一和一对多)</span>
Queue queue = session.createQueue(<span style="color:#009900">"test-queue"</span>);
<span style="color:#880000">//6、使用会话对象创建生产者对象</span>
MessageProducer producer = session.createProducer(queue);
<span style="color:#880000">//7、使用会话对象创建一个消息对象</span>
TextMessage textMessage = session.createTextMessage(<span style="color:#009900">"hello!test-queue"</span>);
<span style="color:#880000">//8、发送消息</span>
producer.send(textMessage);
<span style="color:#880000">//9、关闭资源</span>
producer.close();
session.close();
connection.close();
}</code></span>
接收代码:
<span style="color:#000000"><code> <span style="color:#000088">public</span> <span style="color:#000088">void</span> <span style="color:#009900">TestMQConsumerQueue</span>() <span style="color:#000088">throws</span> Exception{
<span style="color:#880000">//1、创建工厂连接对象,需要制定ip和端口号</span>
ConnectionFactory connectionFactory = <span style="color:#000088">new</span> ActiveMQConnectionFactory(<span style="color:#009900">"tcp://192.168.156.44:61616"</span>);
<span style="color:#880000">//2、使用连接工厂创建一个连接对象</span>
Connection connection = connectionFactory.createConnection();
<span style="color:#880000">//3、开启连接</span>
connection.start();
<span style="color:#880000">//4、使用连接对象创建会话(session)对象</span>
Session session = connection.createSession(<span style="color:#000088">false</span>, Session.AUTO_ACKNOWLEDGE);
<span style="color:#880000">//5、使用会话对象创建目标对象,包含queue和topic(一对一和一对多)</span>
Queue queue = session.createQueue(<span style="color:#009900">"test-queue"</span>);
<span style="color:#880000">//6、使用会话对象创建生产者对象</span>
MessageConsumer consumer = session.createConsumer(queue);
<span style="color:#880000">//7、向consumer对象中设置一个messageListener对象,用来接收消息</span>
consumer.setMessageListener(<span style="color:#000088">new</span> MessageListener() {
<span style="color:#9b859d">@Override</span>
<span style="color:#000088">public</span> <span style="color:#000088">void</span> <span style="color:#009900">onMessage</span>(Message message) {
<span style="color:#880000">// TODO Auto-generated method stub</span>
<span style="color:#000088">if</span>(message <span style="color:#000088">instanceof</span> TextMessage){
TextMessage textMessage = (TextMessage)message;
<span style="color:#000088">try</span> {
System.out.println(textMessage.getText());
} <span style="color:#000088">catch</span> (JMSException e) {
<span style="color:#880000">// TODO Auto-generated catch block</span>
e.printStackTrace();
}
}
}
});
<span style="color:#880000">//8、程序等待接收用户消息</span>
System.in.read();
<span style="color:#880000">//9、关闭资源</span>
consumer.close();
session.close();
connection.close();
}</code></span>
- 然后当我们运行queue发送的时候可以看到队列里已经有一条消息了,但没有发送出去:
然后在运行queue 的接收端,可以看到消息已经发出了:
接着对topic进行测试,发送代码如下:
<span style="color:#000000"><code> <span style="color:#000088">public</span> <span style="color:#000088">void</span> <span style="color:#009900">TestTopicProducer</span>() <span style="color:#000088">throws</span> Exception{
<span style="color:#880000">//1、创建工厂连接对象,需要制定ip和端口号</span>
ConnectionFactory connectionFactory = <span style="color:#000088">new</span> ActiveMQConnectionFactory(<span style="color:#009900">"tcp://192.168.156.44:61616"</span>);
<span style="color:#880000">//2、使用连接工厂创建一个连接对象</span>
Connection connection = connectionFactory.createConnection();
<span style="color:#880000">//3、开启连接</span>
connection.start();
<span style="color:#880000">//4、使用连接对象创建会话(session)对象</span>
Session session = connection.createSession(<span style="color:#000088">false</span>, Session.AUTO_ACKNOWLEDGE);
<span style="color:#880000">//5、使用会话对象创建目标对象,包含queue和topic(一对一和一对多)</span>
Topic topic = session.createTopic(<span style="color:#009900">"test-topic"</span>);
<span style="color:#880000">//6、使用会话对象创建生产者对象</span>
MessageProducer producer = session.createProducer(topic);
<span style="color:#880000">//7、使用会话对象创建一个消息对象</span>
TextMessage textMessage = session.createTextMessage(<span style="color:#009900">"hello!test-topic"</span>);
<span style="color:#880000">//8、发送消息</span>
producer.send(textMessage);
<span style="color:#880000">//9、关闭资源</span>
producer.close();
session.close();
connection.close();
}</code></span>
- 接收代码:
<span style="color:#000000"><code> <span style="color:#000088">public</span> <span style="color:#000088">void</span> <span style="color:#009900">TestTopicConsumer</span>() <span style="color:#000088">throws</span> Exception{
<span style="color:#880000">//1、创建工厂连接对象,需要制定ip和端口号</span>
ConnectionFactory connectionFactory = <span style="color:#000088">new</span> ActiveMQConnectionFactory(<span style="color:#009900">"tcp://192.168.156.44:61616"</span>);
<span style="color:#880000">//2、使用连接工厂创建一个连接对象</span>
Connection connection = connectionFactory.createConnection();
<span style="color:#880000">//3、开启连接</span>
connection.start();
<span style="color:#880000">//4、使用连接对象创建会话(session)对象</span>
Session session = connection.createSession(<span style="color:#000088">false</span>, Session.AUTO_ACKNOWLEDGE);
<span style="color:#880000">//5、使用会话对象创建目标对象,包含queue和topic(一对一和一对多)</span>
Topic topic = session.createTopic(<span style="color:#009900">"test-topic"</span>);
<span style="color:#880000">//6、使用会话对象创建生产者对象</span>
MessageConsumer consumer = session.createConsumer(topic);
<span style="color:#880000">//7、向consumer对象中设置一个messageListener对象,用来接收消息</span>
consumer.setMessageListener(<span style="color:#000088">new</span> MessageListener() {
<span style="color:#9b859d">@Override</span>
<span style="color:#000088">public</span> <span style="color:#000088">void</span> <span style="color:#009900">onMessage</span>(Message message) {
<span style="color:#880000">// TODO Auto-generated method stub</span>
<span style="color:#000088">if</span>(message <span style="color:#000088">instanceof</span> TextMessage){
TextMessage textMessage = (TextMessage)message;
<span style="color:#000088">try</span> {
System.out.println(textMessage.getText());
} <span style="color:#000088">catch</span> (JMSException e) {
<span style="color:#880000">// TODO Auto-generated catch block</span>
e.printStackTrace();
}
}
}
});
<span style="color:#880000">//8、程序等待接收用户消息</span>
System.in.read();
<span style="color:#880000">//9、关闭资源</span>
consumer.close();
session.close();
connection.close();
}</code></span>
- 然后运行topic发送:
可以看到消息已经发送出去。再运行topic接收:
可以看到有了一个消费者,但是没有接收的消息,这是因为正常情况下我们的topic消息不会再服务器持久化,所以要先打开消费者,再打开生产者,这个时候我们再运行生产者发送一条消息看到消息已经接收到了:
ActiveMQ整合spring及项目中运用
activeMQ与spring看一整合到一起使用,除了添加ActiveMQ相关的jar包外,还需要添加spring的jar包:
<span style="color:#000000"><code> <span style="color:#006666"><<span style="color:#4f4f4f">dependency</span>></span>
<span style="color:#006666"><<span style="color:#4f4f4f">groupId</span>></span>org.springframework<span style="color:#006666"></<span style="color:#4f4f4f">groupId</span>></span>
<span style="color:#006666"><<span style="color:#4f4f4f">artifactId</span>></span>spring-context<span style="color:#006666"></<span style="color:#4f4f4f">artifactId</span>></span>
<span style="color:#006666"></<span style="color:#4f4f4f">dependency</span>></span> </code></span>
- 然后编写applicationContext-activemq.xml文件,
代码如下:
<span style="color:#000000"><code><span style="color:#006666"><?xml version="1.0" encoding="UTF-8"?></span>
<span style="color:#006666"><<span style="color:#4f4f4f">beans</span> <span style="color:#4f4f4f">xmlns</span>=<span style="color:#009900">"http://www.springframework.org/schema/beans"</span>
<span style="color:#4f4f4f">xmlns:context</span>=<span style="color:#009900">"http://www.springframework.org/schema/context"</span> <span style="color:#4f4f4f">xmlns:p</span>=<span style="color:#009900">"http://www.springframework.org/schema/p"</span>
<span style="color:#4f4f4f">xmlns:aop</span>=<span style="color:#009900">"http://www.springframework.org/schema/aop"</span> <span style="color:#4f4f4f">xmlns:tx</span>=<span style="color:#009900">"http://www.springframework.org/schema/tx"</span>
<span style="color:#4f4f4f">xmlns:dubbo</span>=<span style="color:#009900">"http://code.alibabatech.com/schema/dubbo"</span>
<span style="color:#4f4f4f">xmlns:xsi</span>=<span style="color:#009900">"http://www.w3.org/2001/XMLSchema-instance"</span>
<span style="color:#4f4f4f">xsi:schemaLocation</span>=<span style="color:#009900">"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://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd"</span>></span>
<span style="color:#880000"><!-- 配置能够产生connection的connectionfactory,由JMS对应的服务厂商提供 --></span>
<span style="color:#006666"><<span style="color:#4f4f4f">bean</span> <span style="color:#4f4f4f">id</span>=<span style="color:#009900">"tagertConnectionFactory"</span> <span style="color:#4f4f4f">class</span>=<span style="color:#009900">"org.apache.activemq.ActiveMQConnectionFactory"</span>></span>
<span style="color:#006666"><<span style="color:#4f4f4f">constructor-arg</span> <span style="color:#4f4f4f">name</span>=<span style="color:#009900">"brokerURL"</span> <span style="color:#4f4f4f">value</span>=<span style="color:#009900">"tcp://192.168.156.44:61616"</span>/></span>
<span style="color:#006666"></<span style="color:#4f4f4f">bean</span>></span>
<span style="color:#880000"><!-- 配置spring管理真正connectionfactory的connectionfactory,相当于spring对connectionfactory的一层封装 --></span>
<span style="color:#006666"><<span style="color:#4f4f4f">bean</span> <span style="color:#4f4f4f">id</span>=<span style="color:#009900">"connectionFactory"</span> <span style="color:#4f4f4f">class</span>=<span style="color:#009900">"org.springframework.jms.connection.SingleConnectionFactory"</span>></span>
<span style="color:#006666"><<span style="color:#4f4f4f">property</span> <span style="color:#4f4f4f">name</span>=<span style="color:#009900">"targetConnectionFactory"</span> <span style="color:#4f4f4f">ref</span>=<span style="color:#009900">"tagertConnectionFactory"</span>/></span>
<span style="color:#006666"></<span style="color:#4f4f4f">bean</span>></span>
<span style="color:#880000"><!-- 配置生产者 --></span>
<span style="color:#880000"><!-- Spring使用JMS工具类,可以用来发送和接收消息 --></span>
<span style="color:#006666"><<span style="color:#4f4f4f">bean</span> <span style="color:#4f4f4f">id</span>=<span style="color:#009900">"jmsTemplate"</span> <span style="color:#4f4f4f">class</span>=<span style="color:#009900">"org.springframework.jms.core.JmsTemplate"</span>></span>
<span style="color:#880000"><!-- 这里是配置的spring用来管理connectionfactory的connectionfactory --></span>
<span style="color:#006666"><<span style="color:#4f4f4f">property</span> <span style="color:#4f4f4f">name</span>=<span style="color:#009900">"connectionFactory"</span> <span style="color:#4f4f4f">ref</span>=<span style="color:#009900">"connectionFactory"</span>/></span>
<span style="color:#006666"></<span style="color:#4f4f4f">bean</span>></span>
<span style="color:#880000"><!-- 配置destination --></span>
<span style="color:#880000"><!-- 队列目的地 --></span>
<span style="color:#006666"><<span style="color:#4f4f4f">bean</span> <span style="color:#4f4f4f">id</span>=<span style="color:#009900">"queueDestination"</span> <span style="color:#4f4f4f">class</span>=<span style="color:#009900">"org.apache.activemq.command.ActiveMQQueue"</span>></span>
<span style="color:#006666"><<span style="color:#4f4f4f">constructor-arg</span> <span style="color:#4f4f4f">value</span>=<span style="color:#009900">"spring-queue"</span>/></span>
<span style="color:#006666"></<span style="color:#4f4f4f">bean</span>></span>
<span style="color:#880000"><!-- 话题目的地 --></span>
<span style="color:#006666"><<span style="color:#4f4f4f">bean</span> <span style="color:#4f4f4f">id</span>=<span style="color:#009900">"itemAddTopic"</span> <span style="color:#4f4f4f">class</span>=<span style="color:#009900">"org.apache.activemq.command.ActiveMQTopic"</span>></span>
<span style="color:#006666"><<span style="color:#4f4f4f">constructor-arg</span> <span style="color:#4f4f4f">value</span>=<span style="color:#009900">"item-add-topic"</span>/></span>
<span style="color:#006666"></<span style="color:#4f4f4f">bean</span>></span>
<span style="color:#006666"></<span style="color:#4f4f4f">beans</span>></span> </code></span>
- 然后在我们淘淘商城中,商品添加到数据库的时候,对应也要添加数据到我们的solr索引中,所以生产者应该在插入数据后创建:
当然,在xml文件中配置好的jmstemplate和destination也要注入进来:
<span style="color:#000000"><code> <span style="color:#9b859d">@Autowired</span>
<span style="color:#000088">private</span> JmsTemplate jmsTemplate;
<span style="color:#9b859d">@Resource</span>(name=<span style="color:#009900">"itemAddTopic"</span>)
<span style="color:#000088">private</span> Destination destination;</code></span>
然后消费者应该写在我们的搜索工程中,首先添加spring和activeMQ的jar包,然后配置xml文件,再编写一个监听器,当接收到消息时,就讲数据存入索引库,xml文件代码如下:
<span style="color:#000000"><code><span style="color:#006666"><?xml version="1.0" encoding="UTF-8"?></span>
<span style="color:#006666"><<span style="color:#4f4f4f">beans</span> <span style="color:#4f4f4f">xmlns</span>=<span style="color:#009900">"http://www.springframework.org/schema/beans"</span>
<span style="color:#4f4f4f">xmlns:context</span>=<span style="color:#009900">"http://www.springframework.org/schema/context"</span> <span style="color:#4f4f4f">xmlns:p</span>=<span style="color:#009900">"http://www.springframework.org/schema/p"</span>
<span style="color:#4f4f4f">xmlns:aop</span>=<span style="color:#009900">"http://www.springframework.org/schema/aop"</span> <span style="color:#4f4f4f">xmlns:tx</span>=<span style="color:#009900">"http://www.springframework.org/schema/tx"</span>
<span style="color:#4f4f4f">xmlns:dubbo</span>=<span style="color:#009900">"http://code.alibabatech.com/schema/dubbo"</span>
<span style="color:#4f4f4f">xmlns:xsi</span>=<span style="color:#009900">"http://www.w3.org/2001/XMLSchema-instance"</span>
<span style="color:#4f4f4f">xsi:schemaLocation</span>=<span style="color:#009900">"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://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd"</span>></span>
<span style="color:#880000"><!-- 配置能够产生connection的connectionfactory,由JMS对应的服务厂商提供 --></span>
<span style="color:#006666"><<span style="color:#4f4f4f">bean</span> <span style="color:#4f4f4f">id</span>=<span style="color:#009900">"tagertConnectionFactory"</span> <span style="color:#4f4f4f">class</span>=<span style="color:#009900">"org.apache.activemq.ActiveMQConnectionFactory"</span>></span>
<span style="color:#006666"><<span style="color:#4f4f4f">constructor-arg</span> <span style="color:#4f4f4f">name</span>=<span style="color:#009900">"brokerURL"</span> <span style="color:#4f4f4f">value</span>=<span style="color:#009900">"tcp://192.168.156.44:61616"</span>/></span>
<span style="color:#006666"></<span style="color:#4f4f4f">bean</span>></span>
<span style="color:#880000"><!-- 配置spring管理真正connectionfactory的connectionfactory,相当于spring对connectionfactory的一层封装 --></span>
<span style="color:#006666"><<span style="color:#4f4f4f">bean</span> <span style="color:#4f4f4f">id</span>=<span style="color:#009900">"connectionFactory"</span> <span style="color:#4f4f4f">class</span>=<span style="color:#009900">"org.springframework.jms.connection.SingleConnectionFactory"</span>></span>
<span style="color:#006666"><<span style="color:#4f4f4f">property</span> <span style="color:#4f4f4f">name</span>=<span style="color:#009900">"targetConnectionFactory"</span> <span style="color:#4f4f4f">ref</span>=<span style="color:#009900">"tagertConnectionFactory"</span>/></span>
<span style="color:#006666"></<span style="color:#4f4f4f">bean</span>></span>
<span style="color:#880000"><!-- 配置destination --></span>
<span style="color:#880000"><!-- 队列目的地 --></span>
<span style="color:#006666"><<span style="color:#4f4f4f">bean</span> <span style="color:#4f4f4f">id</span>=<span style="color:#009900">"queueDestination"</span> <span style="color:#4f4f4f">class</span>=<span style="color:#009900">"org.apache.activemq.command.ActiveMQQueue"</span>></span>
<span style="color:#006666"><<span style="color:#4f4f4f">constructor-arg</span> <span style="color:#4f4f4f">value</span>=<span style="color:#009900">"spring-queue"</span>/></span>
<span style="color:#006666"></<span style="color:#4f4f4f">bean</span>></span>
<span style="color:#880000"><!-- 话题目的地 --></span>
<span style="color:#006666"><<span style="color:#4f4f4f">bean</span> <span style="color:#4f4f4f">id</span>=<span style="color:#009900">"itemAddTopic"</span> <span style="color:#4f4f4f">class</span>=<span style="color:#009900">"org.apache.activemq.command.ActiveMQTopic"</span>></span>
<span style="color:#006666"><<span style="color:#4f4f4f">constructor-arg</span> <span style="color:#4f4f4f">value</span>=<span style="color:#009900">"item-add-topic"</span>/></span>
<span style="color:#006666"></<span style="color:#4f4f4f">bean</span>></span>
<span style="color:#880000"><!-- 配置监听器 --></span>
<span style="color:#006666"><<span style="color:#4f4f4f">bean</span> <span style="color:#4f4f4f">id</span>=<span style="color:#009900">"myListener"</span> <span style="color:#4f4f4f">class</span>=<span style="color:#009900">"com.taotao.search.listener.MyListener"</span>/></span>
<span style="color:#006666"><<span style="color:#4f4f4f">bean</span> <span style="color:#4f4f4f">id</span>=<span style="color:#009900">"itemAddListener"</span> <span style="color:#4f4f4f">class</span>=<span style="color:#009900">"com.taotao.search.listener.ItemAddListener"</span>/></span>
<span style="color:#880000"><!-- 系统监听器 --></span>
<span style="color:#880000"><!-- <bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="destination" ref="queueDestination"/>
<property name="messageListener" ref="myListener"/>
</bean> --></span>
<span style="color:#006666"><<span style="color:#4f4f4f">bean</span> <span style="color:#4f4f4f">class</span>=<span style="color:#009900">"org.springframework.jms.listener.DefaultMessageListenerContainer"</span>></span>
<span style="color:#006666"><<span style="color:#4f4f4f">property</span> <span style="color:#4f4f4f">name</span>=<span style="color:#009900">"connectionFactory"</span> <span style="color:#4f4f4f">ref</span>=<span style="color:#009900">"connectionFactory"</span>/></span>
<span style="color:#006666"><<span style="color:#4f4f4f">property</span> <span style="color:#4f4f4f">name</span>=<span style="color:#009900">"destination"</span> <span style="color:#4f4f4f">ref</span>=<span style="color:#009900">"itemAddTopic"</span>/></span>
<span style="color:#006666"><<span style="color:#4f4f4f">property</span> <span style="color:#4f4f4f">name</span>=<span style="color:#009900">"messageListener"</span> <span style="color:#4f4f4f">ref</span>=<span style="color:#009900">"itemAddListener"</span>/></span>
<span style="color:#006666"></<span style="color:#4f4f4f">bean</span>></span>
<span style="color:#006666"></<span style="color:#4f4f4f">beans</span>></span> </code></span>
- 接收消息代码:
最后同时打开测试即可。