一、本文章包含的内容
1、列举了ActiveMQ中监听器的使用
2、spring+activemq方式
|
1
2
3
|
<!-- 消息监听容器(Queue),配置连接工厂,监听的队列是queue3,监听器是上面定义的监听器 --><!--加载spring配置后,studentInfoHandler的onMessage方法自动运行并接收队列-->
|
二、配置文件(spring-jms.xml)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
<?xml version="1.0" encoding="UTF-8"?>
xsi:schemaLocation="
<!-- 启用spring mvc 注解 -->
<context:component-scan base-package="org.soa.test.activemq"/>
<!-- 配置JMS连接工厂 -->
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<!--解决接收消息抛出异常:javax.jms.JMSException: Failed to build body from content. Serializable class not available to broke-->
<property name="trustAllPackages" value="true"/>
<!-- 是否异步发送 -->
<property name="useAsyncSend" value="true" />
</bean>
<!--============================监听模块==============-->
<bean id="queueDestination3" class="org.apache.activemq.command.ActiveMQQueue">
<!-- 设置消息队列的名字 -->
<constructor-arg>
<value>queue3</value>
</constructor-arg>
</bean>
<bean id="studentInfoHandler" class="org.soa.test.activemq.listeners.StudentInfoHandler" />
<!-- 消息监听容器(Queue),配置连接工厂,监听的队列是queue3,监听器是上面定义的监听器 -->
<!--加载spring配置后,studentInfoHandler的onMessage方法自动运行并接收队列-->
<bean id="jmsContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="queueDestination3" />
<property name="messageListener" ref="studentInfoHandler" />
</bean>
</beans>
|
三、监听代码
1、监听代码
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
package org.soa.test.activemq.listeners;
import org.soa.test.activemq.StudentInfo;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
/** * Created by JamesC on 16-9-22.
*/
public class StudentInfoHandler implements MessageListener {
//加载spring配置后,studentInfoHandler的onMessage方法自动运行并接收队列
@Override
public void onMessage(Message message) {
TextMessage tm = (TextMessage) message;
try {
System.out.println("ConsumerMessageListener收到了文本消息:\t"+ tm.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
} |
2、测试代码
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
package org.soa.test.activemq.listeners;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.soa.test.activemq.queues.ProduceMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.jms.Destination;
/** * Created by JamesC on 16-9-22.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/spring-jms.xml")
public class ListenerTest {
@Autowired
@Qualifier("queueDestination3")//配置文件中只配置了接收queueDestination3的消息
private Destination destination;
@Autowired
private ProduceMsg produceMsg;
@Test
public void sendMsg(){
produceMsg.sendMessage(destination,"----这里测试使用配置实现监听器测试消息接收");
}
} |