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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 13 ■ SENDING MESSAGESFor clarity, the code for exception handling is omitted. Note that the @Resource annotation appearson private attributes. Injection of resources occurs <strong>with</strong> different visibilities (private, protected, package,or public).ConnectionThe javax.jms.Connection object, which you create using the createConnection() method of theconnection factory, encapsulates a connection to the JMS provider. Connections are thread-safe anddesigned to be shareable, as opening a new connection is resource intensive. However, a session(javax.jms.Session) provides a single-threaded context for sending and receiving messages, using aconnection to create one or more sessions.Like connection factories, connections come in three forms: the generic Connection interface, theQueueConnection interface, and the TopicConnection interface that extends it. Depending on theconnection factory object that you have, you can use it to create a connection:Connection connection = connFactory.createConnection();QueueConnection connection = queueConnFactory.createQueueConnection();TopicConnection connection = topicConnFactory.createTopicConnection();In Listing 13-4, before the receiver can consume messages, it must call the start() method. If youneed to stop receiving messages temporarily <strong>with</strong>out closing the connection, you can call the stop()method.connection.start();connection.stop();When the application completes, you need to close any connections created. Closing a connectionalso closes its sessions and its producers or consumers.connection.close();SessionYou create a session from the connection using the createSession() method. A session provides atransactional context in which a set of messages to be sent or received is grouped in an atomic unit ofwork, meaning that, if you send several messages during the same session, JMS will ensure that theyeither all arrive in the order they’ve been sent at the destination or none at all. This behavior is set at thecreation of the session:Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);The first parameter of the method specifies whether or not the session is transactional. In thepreceding code, the parameter is set to true, meaning that the request for sending messages won’t berealized until either the session’s commit() method is called or the session is closed. If the parameter wasset to false, the session would not be transactional, and messages would be sent as soon as the send()method is invoked. The second parameter means that the session automatically acknowledges messageswhen they have been received successfully.389

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

Saved successfully!

Ooh no, something went wrong!