Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Master JMS Messaging with OpenJMS  : Page 2

Learn how to take advantage of Java's Messaging Service API (JMS) with the free OpenJMS implementation. You'll see how to send and receive messages using both point-to-point and publish/subscribe messaging models.


advertisement
Point-to-point Messaging with OpenJMS
You should recall that in the point-to-point messaging style, a sender sends messages to a queue. In this messaging model, messages are received by one—and only one—receiver.

Writing to a Queue
Writing to a queue consists of the following steps:

  1. Obtain the JNDI context.
  2. Contact the JMS provider, OpenJMS, and get a JMS connection from the queue connection factory.
  3. Create a queue connection using the connection factory.
  4. Establish a queue session for the newly established connection.
  5. Lookup the target queue destination.
  6. Create a queue sender that will be used to send messages and associate it to the queue session.
  7. Send a message using the queue sender.
The code for the QueueSend class shown below follows these steps. Note that it uses a queue named queue1 The OpenJMS install pre-configures this queue by default; however you can refer to the section Creating Your Own Topics and Queues in this article to see how to configure your own queues.

public class QueueSend { public static void main(String[] args) { try { // standard boilerplate code to establish a // JNDI context and connection // retrieve queue connection factory QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory)context.lookup( "JmsQueueConnectionFactory"); // create a queue connection QueueConnection queueConnection = queueConnectionFactory. createQueueConnection(); // create a queue session // set transactions to false and set auto // acknowledgement of receipt of messages QueueSession queueSession = queueConnection.createQueueSession( false,Session.AUTO_ACKNOWLEDGE); // retrieve queue Queue queue = (Queue)context.lookup("queue1"); // create a queue sender and associate to // the retrieved queue QueueSender queueSender = queueSession.createSender(queue); // send a message to the queue TextMessage message = queueSession.createTextMessage(); message.setText("Hello there."); queueSender.send(message); System.out.println( "Sample application writing message to " + "queue."); // clean up ... } catch (NamingException e) { e.printStackTrace(); } catch (JMSException e) { e.printStackTrace(); } } }

Now that you've seen how to write a message to the queue, you can retrieve the message from the queue either synchronously or asynchronously.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date