org.okj.commons.broker.SimpleMessagePublisher.java Source code

Java tutorial

Introduction

Here is the source code for org.okj.commons.broker.SimpleMessagePublisher.java

Source

/**
 * @(#)SimpleMessagePublisher.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.ObjectMessage;
import javax.jms.Session;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;

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: SimpleMessagePublisher.java, v 0.1 2013-1-30 ?11:10:37 Administrator Exp $
 */
public class SimpleMessagePublisher implements MessagePublisher, InitializingBean, DisposableBean {
    /* logger */
    private static final Logger LOGGER = Logger.getLogger(MessagePublisher.class);

    /* JMS */
    private ConnectionFactory connectionFactory;

    /* JMS?? */
    private String topicName;

    /*  */
    private TopicConnection connection;

    /* ? */
    private TopicSession session;

    /* ? */
    private Topic topic;

    /** 
     * @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);

                //2. ??
                this.connection.start();

                LogUtils.info(LOGGER, "???topicName={0}", topicName);
            }
        } catch (JMSException ex) {
            LogUtils.error(LOGGER, "??", ex);
        }
    }

    /** 
     * @see org.storevm.commons.broker.MessagePublisher#publishMessage(org.storevm.commons.broker.MessageEvent)
     */
    @Override
    public void publishMessage(MessageEvent event) {
        try {
            //1. ??
            TopicPublisher publisher = this.session.createPublisher(this.topic);

            //2. ???
            ObjectMessage message = session.createObjectMessage(event);

            //3. ??
            message.setStringProperty(SERVER_ID, event.getAttribute(DEST_HOST));
            message.setStringProperty(EVENT_CODE, event.getEventCode());
            message.setStringProperty(TOPIC_NAME, topicName);

            //4. ???
            publisher.publish(message);

            LogUtils.info(LOGGER, "???event={0}", event);
        } catch (JMSException ex) {
            LogUtils.error(LOGGER, "???", ex);
        }
    }

    /**
     * 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;
    }
}