Sun River
Topics about Java SE, Servlet/JSP, JDBC, MultiThread, UML, Design Pattern, CSS, JavaScript, Maven, JBoss, Tomcat, ...
posts - 78,comments - 0,trackbacks - 0

1). Steps for Building a JMS Sender Application

1.Get ConnectionFactory and Destination object (Topic or Queue) through JNDI         

// Get JNDI InitialContext object

Context jndiContext = new InitialContext();

// Locate ConnectionFactory object via JNDI

TopicConnectionFactory factory =
      (TopicConnectionFactory) jndiContext.lookup("MyTopicConnectionFactory");

// Locate Destination object (Topic or Queue) through JNDI

Topic weatherTopic = (Topic) jndiContext.lookup("WeatherData");

2.Create a Connection                              
                                 
                    
// Create a Connection object from ConnectionFactory object
                                 TopicConnection topicConnection = factory.createTopicConnection();

3.Create a Session to send/receive messages

         // Create a Session from Connection object.

            // 1st parameter controls transaction

            // 2nd parameter specifies acknowledgment type

         TopicSession session =

            topicConnection.createTopicSession (false, Session.CLIENT_ACKNOWLEDGE);

4.Create a MessageProducer (TopicPublisher or QueueSender)

                                          // Create MessageProducer from Session object
                              // TopicPublisher for Pub/Sub
                              // QueueSender for Point-to-Point
                              TopicPublisher publisher =session.createPublisher(weatherTopic);

5.Start Connection
                                                // Until Connection gets started, message flow
                                    // is inhibited: Connection must be started before
                                    // messages will be transmitted.
                                    topicConnection.start();

6.Send (publish) messages
                                    // Create a Message
                                     TextMessage message =session.createMessage();
                                    message.setText("text:35 degrees");
                                    // Publish the message
                                    publisher.publish(message);

7.Close Session and Connection

2). Steps for Building a JMS Receiver Application (non-blocking mode)

1.Get ConnectionFactory and Destination object

(Topic or Queue) through JNDI

2.Create a Connection

3.Create a Session to send/receive messages

4.Create a MessageConsumer (TopicSubscriber or QueueReceiver)

            // Create Subscriber from Session object
            TopicSubscriber subscriber =session.createSubscriber(weatherTopic);

5.
Register MessageListener for non-blocking mode
         WeatherListener myListener= new WeatherListener();
         // Register MessageListener with TopicSubscriber object
         subscriber.setMessageListener(myListener);

6.Start Connection

7.Close Session and Connection

3). Steps for Building a JMS Receiver Application for blocking mode)

1
.Get ConnectionFactory and Destination object (Topic or Queue) through JNDI

2.Create a Connection

3.Create a Session to send/receive messages

4.

Create a MessageConsumer

5.Start Connection

6.

Receive message

7.Close Session and Connection

posted on 2006-10-30 01:11 Sun River 阅读(353) 评论(0)  编辑  收藏 所属分类: JMS / MQ