12.07.2015 Views

Beginning Java EE 6 with GlassFish 3, Second Edition

Beginning Java EE 6 with GlassFish 3, Second Edition

Beginning Java EE 6 with GlassFish 3, Second Edition

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 13 ■ SENDING MESSAGESThe code in Listing 13-1 represents a Sender class that has a main() method only. In this method, thefirst thing that occurs is that a JNDI context is instantiated and used to obtain a ConnectionFactory and aQueue. Connection factories and destinations (queues and topics) are called administered objects; theyhave to be created and declared in the message provider (in this case, OpenMQ). They both have a JNDIname (e.g., the queue is called jms/javaee6/Queue) and need to be looked up in the JNDI tree.When the two administered objects are obtained, the Sender class uses the ConnectionFactory tocreate a Connection from which a Session is obtained. With this session, a MessageProducer and amessage are created on the destination queue (session.createProducer(queue)). The producer thensends this message (of type text). The code is quite verbose, and in the example I’ve purposely omittedexception handling (catching JNDI and JMS exceptions).Fortunately, once you’ve written this code to send a message, the code to receive the message looksalmost the same. In fact, the first lines of the Receiver class in Listing 13-2 are exactly the same: create aJNDI context, lookup for the connection factory and the queue, and then connect. The only differencesare that a MessageConsumer is used instead of a MessageProducer, and that the receiver enters an infiniteloop to listen to the queue (you’ll later see that this loop can be avoided by using the more standardmessage listener). When the message arrives, it is consumed and the content displayed.Listing 13-2. The Receiver Class Consumes a Message from a Queuepublic class Receiver {public static void main(String[] args) {// Gets the JNDI contextContext jndiContext = new InitialContext();// Looks up the administered objectsConnectionFactory connectionFactory = (ConnectionFactory) jndiContext.lookup("jms/javaee6/ConnectionFactory");Queue queue = (Queue) jndiContext.lookup("jms/javaee6/Queue");// Creates the needed artifacts to connect to the queueConnection connection = connectionFactory.createConnection();Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);MessageConsumer consumer = session.createConsumer(queue);connection.start();}}// Infinite loop to receive the messageswhile (true) {TextMessage message = (TextMessage) consumer.receive();System.out.println("Message received: " + message.getText());}Next, it’s time to focus on the JMS API, define all the administered objects and classes that are used,and see how this translates to an MDB.381

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!