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");
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 MessageConsumer5.Start Connection
6.
Receive message7.Close Session and Connection