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 MESSAGESAt this point, the client program starts the connection and receives messages. The namejavaee6DurableSubscription is used as an identifier of the durable subscription. Each durable subscribermust have a unique ID, resulting in the declaration of a unique connection factory for each potential,durable subscriber.Setting PrioritiesYou can use message priority levels to instruct the JMS provider to deliver urgent messages first. JMSdefines ten priority values, <strong>with</strong> 0 as the lowest and 9 as the highest. You can specify the priority value byeither using the setPriority() method or passing it as a parameter to the send() method:MessageProducer producer = session.createProducer(topic);producer.setPriority(2);// orproducer.send(message, DeliveryMode.NON_PERSISTENT, 2, 1000);Message-Driven BeansThis chapter shows how asynchronous messaging provides loose coupling and increased flexibilitybetween systems, using the JMS API in a stand-alone environment <strong>with</strong> main classes, JNDI lookup, orresource injection using the ACC. MDBs provide this standard asynchronous messaging model forenterprise applications running in an EJB container.An MDB is an asynchronous consumer that is invoked by the container as a result of the arrival of amessage. To a message producer, an MDB is simply a message consumer, hidden behind a destinationto which it listens.MDBs are part of the EJB specification, and their model is close to stateless session beans as they donot have any state and run inside an EJB container. The container listens to a destination and delegatesthe call to the MDB upon message arrival. Like any other EJB, MDBs can access resources managed bythe container (other EJBs, JDBC connections, JMS resources, etc.).Why use MDBs when you can use stand-alone JMS clients, as you’ve seen previously? Because of thecontainer, which manages multithreading, security, and transactions, thereby greatly simplifying thecode of your JMS consumer. It also manages incoming messages among multiple instances of MDBs(available in a pool) that have no special multithreading code themselves. As soon as a new messagereaches the destination, an MDB instance is retrieved from the pool to handle the message.How to Write an MDBMDBs can be transactional, multithreaded, and naturally consume JMS messages. With the JMS API thusfar, you might expect factories, connections, sessions, consumers, user transactions, and so on.However, it is surprising to see that a simple consumer MDB can look like Listing 13-8.400

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

Saved successfully!

Ooh no, something went wrong!