JMS Apache ActiveMQ使用(3)
2013-09-27 15:43
309 查看
还有工作就是如何接受JMS Message并进行处理了,spring中也有现成的框架进行处理MessageListenerAdapter和DefaultMessageListenerContainer,我们只要写一个普通类编写处理消息的逻辑,具体如何接收消息都由Spring完成。
spring-listener-jms.xml
Xml代码
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<import resource="spring-jms.xml"/>
<bean id="messageListener" class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
<constructor-arg>
<bean class="com.viva.server.message.jms.UserRegisterListener"/>
</constructor-arg>
<property name="defaultListenerMethod" value="onRegister"/>
<property name="messageConverter" ref="messageConvert"/>
</bean>
<bean id="queueListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="jmsFactory"/>
<property name="destination" ref="queueDestination"/>
<property name="messageListener" ref="messageListener"/>
</bean>
<bean id="topicListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="jmsFactory"/>
<property name="destination" ref="topicDestination"/>
<property name="messageListener" ref="messageListener"/>
</bean>
</beans>
[xml] view
plaincopyprint?
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<import resource="spring-jms.xml"/>
<bean id="messageListener" class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
<constructor-arg>
<bean class="com.viva.server.message.jms.UserRegisterListener"/>
</constructor-arg>
<property name="defaultListenerMethod" value="onRegister"/>
<property name="messageConverter" ref="messageConvert"/>
</bean>
<bean id="queueListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="jmsFactory"/>
<property name="destination" ref="queueDestination"/>
<property name="messageListener" ref="messageListener"/>
</bean>
<bean id="topicListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="jmsFactory"/>
<property name="destination" ref="topicDestination"/>
<property name="messageListener" ref="messageListener"/>
</bean>
</beans>
![](https://oscdn.geek-share.com/Uploads/Images/Content/201910/25/805aaecd1e7cc30334d42033690f333d.gif)
从配置文件可以看出配置了一个Topic监听器和Queue监听器,实际的监听类就是一个UserRegisterListener,由它处理用户注册事件。
UserRegisterListener.java代码
Java代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/202009/29/554a559af05764c9753782e3a7682aeb.gif)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202009/20/57e87902e0ab436c8b510791c9b65a39.png)
public class UserRegisterListener {
public void onRegister(VivaUserRegisterMessage registerMessage){
System.out.println("User Register,tel="+ registerMessage.getTel()+ ",time="+registerMessage.getRegisterTime());
}
}
[java] view
plaincopyprint?
public class UserRegisterListener {
public void onRegister(VivaUserRegisterMessage registerMessage){
System.out.println("User Register,tel="+ registerMessage.getTel()+ ",time="+registerMessage.getRegisterTime());
}
}
![](https://oscdn.geek-share.com/Uploads/Images/Content/201910/25/805aaecd1e7cc30334d42033690f333d.gif)
最后写一个JUnit测试用户来测试一下代码
JMSTestor.java
Java代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/202009/29/554a559af05764c9753782e3a7682aeb.gif)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202009/20/57e87902e0ab436c8b510791c9b65a39.png)
public class JMSTestor extends TestCase {
private ApplicationContext context;
public void testQueueSend(){
context = new ClassPathXmlApplicationContext("spring-jms.xml");
JmsTemplate jmsTemplate = (JmsTemplate) context.getBean("queueTemplate");
VivaUserRegisterMessage msg = new VivaUserRegisterMessage();
msg.setRegisterTime(new Date());
msg.setTel("1385199XXXX");
msg.setUserId(1);
jmsTemplate.convertAndSend(msg);
}
public void testTopicSend(){
context = new ClassPathXmlApplicationContext("spring-jms.xml");
JmsTemplate jmsTemplate = (JmsTemplate) context.getBean("topicTemplate");
VivaUserRegisterMessage msg = new VivaUserRegisterMessage();
msg.setRegisterTime(new Date());
msg.setTel("1385199XXXX");
msg.setUserId(1);
jmsTemplate.convertAndSend(msg);
}
public void testListener()throws Exception{
context = new ClassPathXmlApplicationContext("spring-listener-jms.xml");
Thread.sleep(1000*60*30);
}
}
[java] view
plaincopyprint?
public class JMSTestor extends TestCase {
private ApplicationContext context;
public void testQueueSend(){
context = new ClassPathXmlApplicationContext("spring-jms.xml");
JmsTemplate jmsTemplate = (JmsTemplate) context.getBean("queueTemplate");
VivaUserRegisterMessage msg = new VivaUserRegisterMessage();
msg.setRegisterTime(new Date());
msg.setTel("1385199XXXX");
msg.setUserId(1);
jmsTemplate.convertAndSend(msg);
}
public void testTopicSend(){
context = new ClassPathXmlApplicationContext("spring-jms.xml");
JmsTemplate jmsTemplate = (JmsTemplate) context.getBean("topicTemplate");
VivaUserRegisterMessage msg = new VivaUserRegisterMessage();
msg.setRegisterTime(new Date());
msg.setTel("1385199XXXX");
msg.setUserId(1);
jmsTemplate.convertAndSend(msg);
}
public void testListener()throws Exception{
context = new ClassPathXmlApplicationContext("spring-listener-jms.xml");
Thread.sleep(1000*60*30);
}
}
执行testTopicSend发送JMS Message到Topic
执行testQueueSend发送JMS Message到Queue
执行testListener启动Topic和Queue监听器处理JMS Message
![](https://oscdn.geek-share.com/Uploads/Images/Content/201910/25/805aaecd1e7cc30334d42033690f333d.gif)
完成!另外ActiveMQ集成了Jetty服务,可以访问地址http://localhost:8161/admin/查看具体信息
spring-listener-jms.xml
Xml代码
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<import resource="spring-jms.xml"/>
<bean id="messageListener" class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
<constructor-arg>
<bean class="com.viva.server.message.jms.UserRegisterListener"/>
</constructor-arg>
<property name="defaultListenerMethod" value="onRegister"/>
<property name="messageConverter" ref="messageConvert"/>
</bean>
<bean id="queueListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="jmsFactory"/>
<property name="destination" ref="queueDestination"/>
<property name="messageListener" ref="messageListener"/>
</bean>
<bean id="topicListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="jmsFactory"/>
<property name="destination" ref="topicDestination"/>
<property name="messageListener" ref="messageListener"/>
</bean>
</beans>
[xml] view
plaincopyprint?
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<import resource="spring-jms.xml"/>
<bean id="messageListener" class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
<constructor-arg>
<bean class="com.viva.server.message.jms.UserRegisterListener"/>
</constructor-arg>
<property name="defaultListenerMethod" value="onRegister"/>
<property name="messageConverter" ref="messageConvert"/>
</bean>
<bean id="queueListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="jmsFactory"/>
<property name="destination" ref="queueDestination"/>
<property name="messageListener" ref="messageListener"/>
</bean>
<bean id="topicListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="jmsFactory"/>
<property name="destination" ref="topicDestination"/>
<property name="messageListener" ref="messageListener"/>
</bean>
</beans>
![](https://oscdn.geek-share.com/Uploads/Images/Content/201910/25/805aaecd1e7cc30334d42033690f333d.gif)
从配置文件可以看出配置了一个Topic监听器和Queue监听器,实际的监听类就是一个UserRegisterListener,由它处理用户注册事件。
UserRegisterListener.java代码
Java代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/202009/29/554a559af05764c9753782e3a7682aeb.gif)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202009/20/57e87902e0ab436c8b510791c9b65a39.png)
public class UserRegisterListener {
public void onRegister(VivaUserRegisterMessage registerMessage){
System.out.println("User Register,tel="+ registerMessage.getTel()+ ",time="+registerMessage.getRegisterTime());
}
}
[java] view
plaincopyprint?
public class UserRegisterListener {
public void onRegister(VivaUserRegisterMessage registerMessage){
System.out.println("User Register,tel="+ registerMessage.getTel()+ ",time="+registerMessage.getRegisterTime());
}
}
![](https://oscdn.geek-share.com/Uploads/Images/Content/201910/25/805aaecd1e7cc30334d42033690f333d.gif)
最后写一个JUnit测试用户来测试一下代码
JMSTestor.java
Java代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/202009/29/554a559af05764c9753782e3a7682aeb.gif)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202009/20/57e87902e0ab436c8b510791c9b65a39.png)
public class JMSTestor extends TestCase {
private ApplicationContext context;
public void testQueueSend(){
context = new ClassPathXmlApplicationContext("spring-jms.xml");
JmsTemplate jmsTemplate = (JmsTemplate) context.getBean("queueTemplate");
VivaUserRegisterMessage msg = new VivaUserRegisterMessage();
msg.setRegisterTime(new Date());
msg.setTel("1385199XXXX");
msg.setUserId(1);
jmsTemplate.convertAndSend(msg);
}
public void testTopicSend(){
context = new ClassPathXmlApplicationContext("spring-jms.xml");
JmsTemplate jmsTemplate = (JmsTemplate) context.getBean("topicTemplate");
VivaUserRegisterMessage msg = new VivaUserRegisterMessage();
msg.setRegisterTime(new Date());
msg.setTel("1385199XXXX");
msg.setUserId(1);
jmsTemplate.convertAndSend(msg);
}
public void testListener()throws Exception{
context = new ClassPathXmlApplicationContext("spring-listener-jms.xml");
Thread.sleep(1000*60*30);
}
}
[java] view
plaincopyprint?
public class JMSTestor extends TestCase {
private ApplicationContext context;
public void testQueueSend(){
context = new ClassPathXmlApplicationContext("spring-jms.xml");
JmsTemplate jmsTemplate = (JmsTemplate) context.getBean("queueTemplate");
VivaUserRegisterMessage msg = new VivaUserRegisterMessage();
msg.setRegisterTime(new Date());
msg.setTel("1385199XXXX");
msg.setUserId(1);
jmsTemplate.convertAndSend(msg);
}
public void testTopicSend(){
context = new ClassPathXmlApplicationContext("spring-jms.xml");
JmsTemplate jmsTemplate = (JmsTemplate) context.getBean("topicTemplate");
VivaUserRegisterMessage msg = new VivaUserRegisterMessage();
msg.setRegisterTime(new Date());
msg.setTel("1385199XXXX");
msg.setUserId(1);
jmsTemplate.convertAndSend(msg);
}
public void testListener()throws Exception{
context = new ClassPathXmlApplicationContext("spring-listener-jms.xml");
Thread.sleep(1000*60*30);
}
}
执行testTopicSend发送JMS Message到Topic
执行testQueueSend发送JMS Message到Queue
执行testListener启动Topic和Queue监听器处理JMS Message
![](https://oscdn.geek-share.com/Uploads/Images/Content/201910/25/805aaecd1e7cc30334d42033690f333d.gif)
完成!另外ActiveMQ集成了Jetty服务,可以访问地址http://localhost:8161/admin/查看具体信息
相关文章推荐
- 使用apache的activemq集合JMS处理异步消息
- JMS Apache ActiveMQ使用(1)
- JMS Apache ActiveMQ使用(1)
- JMS Apache ActiveMQ使用(2)
- JMS Apache ActiveMQ(消息中间件)使用攻略
- Apache apusic---ActiveMQ中间件在JMS1.1、J2EE中的使用
- Java ActiveMQ 理解JMS 和 ActiveMQ基本使用(一)
- ActiveMQ使用spring JmsTemplate发送消息(一)
- 【Java消息中间件】Java消息中间件( 第4章 使用activemq - spring jms演示 )
- 使用Apache ActiveMQ
- ActiveMQ使用spring JmsTemplate发送消息(一)
- JMS_使用ActiveMQ实现消息的发送和接收
- 使用activeMQ实现jms
- Apache ActiveMQ消息中间件的基本使用
- JMS之ActiveMQ(含与Spring集成使用)
- Java ActiveMQ 讲解(一)理解JMS 和 ActiveMQ基本使用
- JMS - ActiveMQ的简单使用
- ActiveMQ使用spring JmsTemplate发送消息(一)
- 遇见JMS[1] —— activeMQ的简单使用
- Spring JMS 和 ActiveMQ 基本使用(二)