jms P-T-P和Pub/Sub

如前所述,JMS分两大类:PTP和Pub/Sub

主要的几个对象:ConnectionFactory、Connection、Destination、Session、MessageProducer、MessageConsumer
其相互关系如下图:

1.PTP:
简单回顾7个对象:QueueConnectionFactory、QuequeConnection、Queue、QueueSession、QueueSender、QueueReceiver、QueueBrowser
    1) Sender
    try {
        //具体怎么得到就不写了,各个系统肯定都不一样,但终究都是通过jdni来获得
        Context jndiContext = new InitialContext();
        QueueConnectionFactory factory = jndiContext.lookup("**Factory");
        Queue queue = jndiContext.lookup("**Queue");
        QueueConnection connection = factory.createQueueConnection();
        boolean transaction = true;
        QueueSession session = connection.createQueueSession(transaction , Session.AUTO_ACKNOWLEDGE);
        Message objMessage = session.createObjectMessage();  //或session.createTextMessage("...");
        objMessage.setObject((Serializable)obj); //obj为要传输的对象
        QueueSender sender = session.createSender(queue);
        publisher.setTimeToLive(timeout); //long timeout = ...
        connection.start();
        sender.send(objMessage);
        //or
        /**
        sender = session.createSender(null);
        sender.send(queue, message);
        */
        //如果不用了,就收拾干净
        publisher.close();
        session.close();
        connection.close();
    } catch (JMSException e) {
        //TODO
    } finally {
        publisher = null;
        session = null;
        connection = null;
    }

    2) Receiver/Browser
        a.主动接收,synchronously
        try {
            QueueReceiver receiver = session.createReceiver(queue);
            receiver.receiveNoWait();
            /**
            QueueBrowser browser = session.createBrowser(queue); //QueueBrowser只会取消息,但不会取走消息
            Enumeration elements = browser.getEnumeration();
            while(elements.hasMoreElements) {
                Message message = elements.nextElement();
                ...
            }
            */
        } catch (JMSException e) {
            ...
        }

        b.消息侦听,也是最主要的应用,asynchronously
        try {
            MessageListener myListener = new MyListener();
            QueueReceiver receiver = session.createReceiver(queue);
            receiver.setMessageListener(myListener);
            connection.start();
        } catch (JMSException e) {
            ...    
        }

        public class MyListener impelments MessageListner {
            public void onMessage(Message message) {
                if(message instanceof TextMessage) {
                    ...
                } else if (message instanceof ObjectMessage) {
                    ...
                } else {
                    ...
                }    
            }
        }


2.Pub/Sub:
简单回顾6个对象:TopicConnectionFactory、TopicConnection、Topic、TopicSession、TopicPublisher、TopicSubscriber
    1) Publisher
    try {
        //具体怎么得到就不写了,各个系统肯定都不一样,但终究都是通过jdni来获得
        Context jndiContext = new InitialContext();
        TopicConnectionFactory factory = jndiContext.lookup("**Factory");
        Topic topic = jndiContext.lookup("**Topic");
        TopicConnection connection = factory.createTopicConnection();
        TopicSession session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);  //false表示不使用事务
        Message objMessage = session.createObjectMessage();
        objMessage.setObject((Serializable)obj); //obj为要传输的对象
        TopicPublisher publisher = session.createPublisher(topic);
        publisher.setTimeToLive(100000);
        connection.start();
        publisher.publish(message);
        //or
        /**
        publisher = session.createPublisher(null);
        publiser.publish(topic, message);
        */
        publisher.close();
        session.close();
        connection.close();
    } catch (JMSException e) {
        //TODO
    }


    2) Subscriber
        a.主动接收,比如上面发送完后,接收回应消息,相当于同步接收了
        try {
            TopicSubscriber subscriber = session.createSubScriber(topic);
            message = subscriber.receiver(timeout); //long timeout = ...
            subscriber.close();
        } catch (JMSException e) {
            ...    
        }


        b.消息侦听,asynchronously
        try {
            ...
            TopicSubscriber subscriber = session.createSubScriber(topic);
            MessageListener listener = new MyListener();
            subscriber.setMessageListener(listener); //关键在于这个listener
            connection.start();
        } catch (JMSException e) {
            ...
        }

        MyListener实现同上

你可能感兴趣的:(jms)