Java tutorial
/** * @(#)SimpleMessageSubscriber.java 2013-1-30 * * Copyright (c) 2004-2013 Lakala, Inc. * zhongjiang Road, building 22, Lane 879, shanghai, china * All Rights Reserved. * * This software is the confidential and proprietary information of Lakala, Inc. * You shall not disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into * with Lakala. */ package org.okj.commons.broker; import javax.jms.ConnectionFactory; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.ObjectMessage; import javax.jms.Session; import javax.jms.Topic; import javax.jms.TopicConnection; import javax.jms.TopicConnectionFactory; import javax.jms.TopicSession; import javax.jms.TopicSubscriber; import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; import org.okj.commons.logger.LogUtils; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; /** * ??? * @author Administrator * @version $Id: SimpleMessageSubscriber.java, v 0.1 2013-1-30 ?11:13:16 Administrator Exp $ */ public class SimpleMessageSubscriber implements MessageListener, InitializingBean, DisposableBean { /* logger */ private static final Logger LOGGER = Logger.getLogger(SimpleMessageSubscriber.class); /* ? */ private TaskExecutor executor; /* JMS */ private ConnectionFactory connectionFactory; /* */ private TopicConnection connection; /* ? */ private TopicSession session; /* ? */ private Topic topic; /* JMS?? */ private String topicName; /* ? */ private String selector; /** * @see org.springframework.beans.factory.DisposableBean#destroy() */ @Override public void destroy() throws Exception { if (this.connection != null) { try { this.connection.close(); LogUtils.info(LOGGER, "??topicName={0}", topicName); } catch (JMSException ex) { LogUtils.error(LOGGER, "??", ex); } } } /** * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() */ @Override public void afterPropertiesSet() throws Exception { //bean?????? try { if (connectionFactory != null) { //1. ? this.connection = ((TopicConnectionFactory) connectionFactory).createTopicConnection(); //2. ?? this.session = this.connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); //3. this.topic = session.createTopic(topicName); //4. ?? TopicSubscriber subscriber = null; if (StringUtils.isNotBlank(selector)) { subscriber = session.createSubscriber(topic, selector, false); //? } else { subscriber = session.createSubscriber(topic); } subscriber.setMessageListener(this); //2. ?? this.connection.start(); LogUtils.info(LOGGER, "???topicName={0}", topicName); } } catch (JMSException ex) { LogUtils.error(LOGGER, "??", ex); } } /** * @see javax.jms.MessageListener#onMessage(javax.jms.Message) */ @Override public void onMessage(Message message) { MessageEvent event = null; try { //? if (message instanceof ObjectMessage) { ObjectMessage msg = (ObjectMessage) message; if (msg.getObject() instanceof MessageEvent) { event = (MessageEvent) msg.getObject(); } } //?? if (event == null) { LogUtils.warn(LOGGER, "????, message={0}", message); return; } if (this.executor != null) { // this.executor.execute(event); } } catch (JMSException ex) { LogUtils.error(LOGGER, "???", ex); } catch (Exception ex) { LogUtils.error(LOGGER, "?", ex); } } /** * Setter method for property <tt>executor</tt>. * * @param executor value to be assigned to property executor */ public void setExecutor(TaskExecutor executor) { this.executor = executor; } /** * Setter method for property <tt>connectionFactory</tt>. * * @param connectionFactory value to be assigned to property connectionFactory */ public void setConnectionFactory(ConnectionFactory connectionFactory) { this.connectionFactory = connectionFactory; } /** * Setter method for property <tt>topicName</tt>. * * @param topicName value to be assigned to property topicName */ public void setTopicName(String topicName) { this.topicName = topicName; } /** * Setter method for property <tt>selector</tt>. * * @param selector value to be assigned to property selector */ public void setSelector(String selector) { this.selector = selector; } }