关于近段时间activemq使用总结
activemq队列管理器用于两个系统间的交互,属于异步交互。
由于涉及到两个系统,在使用监听类的时候,用到了两种不同的方法
座席系统使用正常的监听配置:(监听队列的配置)
<!-- 监听类--> <bean id="distinfoListener" class="com.topnet.ccc.jms.listener.DistinfoListener"> </bean> <bean id="distinfoConsumer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="pooledJmsFactory"/> <!-- 注册所监听的队列名字 --> <property name="destination" ref="distinfoQueue"/> <!-- 注册监听类--> <property name="messageListener" ref="distinfoListener"/> <property name="concurrentConsumers" value="1" /> <property name="maxConcurrentConsumers" value="1" /> <property name="sessionTransacted" value="true"></property> <property name="sessionAcknowledgeMode" value="2"></property> <property name="autoStartup" value="true" /> </bean> <!-- 监听队列名字--> <bean id="distinfoQueue" class="org.apache.activemq.command.ActiveMQQueue"> <constructor-arg value="topicis.distinfo"></constructor-arg> </bean>
这种配置属于目前流行配置。
而另一个系统由于spring版本较低,不支持该种配置,则需要在程序中支持
队列监听配置
<bean id="companyUpdateListener" class="com.topsoft.service.icis.tel2.listener.CompanyUpdateListenerImpl" destroy-method="stop"> <property name="url" value="${queue.url}"/> <property name="clientID" value="${companyUpdate.clientID}"/> <property name="dispatchservice" ref="DispatchService"/> </bean>
监听类代码:(监听topic)
public class CompanyUpdateListenerImpl implements CompanyUpdateListener { private static Log logger = LogFactory.getLog(CompanyUpdateListenerImpl.class); private ActiveMQConnectionFactory activeMQConnectionFactory; private Connection connection; private TopicSubscriber topicSubscriber; private Session session; private DispatchService dispatchservice; private String url; private String clientID; public void start() { logger.debug("初始化接收企业信息连接开始"); activeMQConnectionFactory = new ActiveMQConnectionFactory(url); try { connection = activeMQConnectionFactory.createConnection(); logger.debug("企业信息连接设置clientID:" + clientID); connection.setClientID(clientID); logger.debug("企业信息连接设置clientID:" + clientID + "完毕"); connection.start(); session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Topic t = session.createTopic(TransferInfo2Queue.QUEUE_CRMIS_ENTERPRISEINFO); topicSubscriber = session.createDurableSubscriber(t, clientID); topicSubscriber.setMessageListener(new MessageListener(){ public void onMessage(Message message) { try { if(message!=null){ System.out.println("----------------开始发送--------------------------"); dispatchservice.updateCompany4WJ(((ObjectMessage)message).getObject()); System.out.println("-----------------发送完毕-------------------------"); } } catch (JMSException e) { logger.error("获取消息出错"+message); e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } } }); logger.info("初始化接收企业信息连接成功,connection[" + connection + "],consume:[" + topicSubscriber + "]"); // start(); } catch (InvalidClientIDException inE) { logger.error("初始化接收企业信息第二遍加载时异常,处理异常"); try { if (connection != null) { connection.stop(); connection.close(); } } catch (JMSException e1) { e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } } catch (JMSException e) { logger.error("初始化接收企业信息出错 JMSException:", e); try { if (topicSubscriber != null) topicSubscriber.close(); if (session != null) session.close(); if (connection != null) { connection.stop(); connection.close(); } } catch (JMSException e1) { e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. throw new RuntimeException("初始化接收企业信息出错 JMSException" + e.getMessage()); } catch (RuntimeException exception) { logger.error("初始化接收企业信息出错 其他异常", exception); try { if (topicSubscriber != null) topicSubscriber.close(); if (session != null) session.close(); if (connection != null) { connection.stop(); connection.close(); } } catch (JMSException e1) { e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } throw new RuntimeException("初始化接收企业信息出错 其他异常" + exception.getMessage()); } } public void stop() { try { if (topicSubscriber != null) topicSubscriber.close(); if (session != null) session.close(); if (connection != null) { connection.stop(); connection.close(); } logger.info(" 接收企业信息停止监听成功" ); } catch (JMSException e1) { logger.error("接收企业信息停止监听失败 JMSException", e1); e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. throw new RuntimeException("接收企业信息停止监听失败 JMSException" + e1.getMessage()); } catch (RuntimeException exception) { logger.error("接收企业信息停止监听失败,其他异常", exception); exception.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. throw new RuntimeException("接收企业信息停止监听失败,其他异常" + exception.getMessage()); } } public DispatchService getDispatchservice() { return dispatchservice; } public void setDispatchservice(DispatchService dispatchservice) { this.dispatchservice = dispatchservice; } public void setUrl(String url) { this.url = url; } public String getClientID() { return clientID; } public void setClientID(String clientID) { this.clientID = clientID; } }
浙公网安备 33010602011771号