博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

JMS example

Posted on 2012-08-09 21:56  钟悍  阅读(625)  评论(0编辑  收藏  举报
package com.karl.learn;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.EJB;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.jms.DeliveryMode;
import javax.jms.JMSException;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.naming.InitialContext;

/**
 * EventReceiver
 
*/
@Stateless(mappedName = EventReporterRemote.JNDI_NAME)
@Remote(EventReporterRemote.class)
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public class EventHandler implements EventReporterRemote {
    private static String destinationName = "/queue/MessageQueue";
    private QueueConnection queueConnection = null;
    private QueueSession queueSession = null;
    private Queue queue = null;

    @Override
    public void notify(Event event) {
        TO to = event.getEventTO();
        if (to == null) {
            System.out.println("Unable to get data from this event "
                    + event.getEventType());
        }
        Message msg = null;
        switch (event.getEventType()) {
        case CREATE:
            msg = new Message(Message.Type.CREATE, to);
            break;
        case DELETE:
            msg = new Message(Message.Type.DELETE, pto);
            break;
        default:
            System.out.println("Wrong event type from this event "
                    + event.getEventType().toString());
        }
        if (msg != null) {
            sendMessage(msg);
        }
    }

    @PostConstruct
    private void init() {
        initQueue();
    }

    private void initQueue() {
        InitialContext ctx = null;
        try {
            ctx = new InitialContext();
            QueueConnectionFactory cf = (QueueConnectionFactory) ctx
                    .lookup("java:/ConnectionFactory");
            queue = (Queue) ctx.lookup(destinationName);
            try {
                if (queueConnection != null)
                    queueConnection.close();
                queueConnection = null;
            } catch (Exception ex) {
                System.out.println("Closing queueConnection error.");
            }
            queueConnection = cf.createQueueConnection();
            queueSession = queueConnection.createQueueSession(false,
                    Session.AUTO_ACKNOWLEDGE);
            queueConnection.start();
        } catch (Exception e) {
            System.out.println("Lookup ConnectionFactory error.", e);
            try {
                if (queueConnection != null)
                    queueConnection.close();
            } catch (JMSException ignored) {
                System.out.println("This Exception should be ignored.");
            }
            queueConnection = null;
        } finally {
            if (ctx != null) {
                try {
                    ctx.close();
                } catch (Exception e) {
                    System.out.println("Failed in closing context.", e);
                }
            }
        }
    }

    @PreDestroy
    private void closeConnection() {
        try {
            if (queueConnection != null) {
                queueConnection.close();
            }
        } catch (JMSException jmse) {
            System.out.println("Could not close connection.", jmse);
        }
    }

    private void sendMessage(Message message) {
        try {
            System.out.println("Send message:" + message.getMessageType());
            if (queueConnection == null || queueSession == null) {
                initQueue();
            }
            if (queueConnection != null || queueSession != null) {
                final ObjectMessage message = queueSession
                        .createObjectMessage(message);
                QueueSender publisher = queueSession.createSender(queue);
                publisher.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
                publisher.send(message);
            } else {
                System.out.println("No available message queue.");
            }
        } catch (Exception ex) {
            System.out.println("Failed to send message to queue." + ex);
        }
    }
}