标签:des style blog http java color os io
当从webservice接收到信息的时候,消息生产者producer立刻把收到的消息放入到jms里面,消费者cusomer这时要设置一个监听,当生产者发送消息时,只要消息被发出来,消费者就会接收到消息,然后进行相应的操作。
在tomcat里面,要进行配置。
首先在tomcat安装目录里面,对conf/context.xml进行配置,加入以下代码:
<Resource name="jms/FailoverConnectionFactory" auth="Container" type="org.apache.activemq.ActiveMQConnectionFactory" description="JMS Connection Factory" factory="org.apache.activemq.jndi.JNDIReferenceFactory" brokerURL="failover:(tcp://localhost:61616)?initialReconnectDelay=100&maxReconnectAttempts=5" brokerName="localhost" useEmbeddedBroker="false"/> <Resource name="jms/NormalConnectionFactory" auth="Container" type="org.apache.activemq.ActiveMQConnectionFactory" description="JMS Connection Factory" factory="org.apache.activemq.jndi.JNDIReferenceFactory" brokerURL="tcp://localhost:61616" brokerName="localhost" useEmbeddedBroker="false"/> <Resource name="jms/topic/MyTopic" auth="Container" type="org.apache.activemq.command.ActiveMQTopic" factory="org.apache.activemq.jndi.JNDIReferenceFactory" physicalName="MY.TEST.FOO"/> <Resource name="jms/queue/MyQueue" auth="Container" type="org.apache.activemq.command.ActiveMQQueue" factory="org.apache.activemq.jndi.JNDIReferenceFactory" physicalName="MY.TEST.FOO.QUEUE"/>
具体也不晓得这个是做什么的,看上去貌似是配置jndi的,我没用jndi的方式,加上这个,也正常运行。所以还是加上吧。
然后在web.xml里面,加入监听的类,这个类是自己写的。
<!-- 添加jms监听 --> <servlet> <servlet-name>jms-listener</servlet-name> <servlet-class> com.message.JMSListener </servlet-class> <load-on-startup>1</load-on-startup> </servlet>
然后就建立这个类:
package com.message; import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageConsumer; import javax.jms.MessageListener; import javax.jms.ObjectMessage; import javax.jms.Session; import javax.servlet.http.HttpServlet; import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; import org.apache.log4j.Logger;public class JMSListener extends HttpServlet implements MessageListener { /** * */ private static final long serialVersionUID = -8455543162505698971L; private Logger logger=Logger.getLogger(JMSListener.class); ConnectionFactory connectionFactory; Connection connection=null; Session session; Destination destination; MessageConsumer consumer; int i; public JMSListener(){ String url = "tcp://localhost:61616"; String queue ="NewNocPacket"; connectionFactory = new ActiveMQConnectionFactory( ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, url); try { connection = connectionFactory.createConnection(); session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE); destination = session.createQueue(queue); consumer = session.createConsumer(destination); consumer.setMessageListener(this); connection.start(); } catch (JMSException e) { // TODO Auto-generated catch block logger.error("===========cusumer create error"); e.printStackTrace(); } } @Override public void onMessage(Message message) { // TODO Auto-generated method stub ObjectMessage msg=(ObjectMessage) message;try { logger.info("=========hello ==========="); i++; } catch (JMSException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
原理大概是这样,上面代码不要照抄的。
然后就ok了,部署一下项目,建立一个消息的发送者,消息到达后,监听就会自动执行,打印出onmessage里面的话。
tomcat监听activemq jms配置,布布扣,bubuko.com
标签:des style blog http java color os io
原文地址:http://www.cnblogs.com/juepei/p/3867706.html