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 MESSAGES}sendPrintingMessage();}private void sendPrintingMessage() throws JMSException {Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);MessageProducer producer = session.createProducer(printingQueue);TextMessage message = session.createTextMessage();message.setText("This message has been received and sent again");producer.send(message);session.close();}This MDB uses most of the concepts introduced thus far. First, it uses the @MessageDrivenannotation to define the JNDI name of the topic it is listening to (mappedName = "jms/javaee6/Topic"). Inthis same annotation, it defines a set of properties, such as the acknowledge mode and a messageselector using an array of @ActivationConfigProperty annotations, and it implements MessageListenerand its onMessage() method.This MDB also needs to produce a message. Therefore, it gets injected <strong>with</strong> the two administeredobjects required: a connection factory and a destination (the queue named jms/javaee6/Queue). It canthen create and close a shared javax.jms.Connection instance using life-cycle callbacks; althoughcreating a connection is expensive, putting this code in the @PostConstruct and @PreDestroy annotatedmethods will ensure that it will be done only at creation and at destruction of the MDB.Finally, the business method that sends messages (the sendPrintingMessage() method) looks likewhat you’ve seen earlier: a JMS session gets created and used to create a text message and a producer,and the message is then sent. For better readability, exception handling has been omitted in the entireclass.TransactionMDBs are EJBs (see Chapter 9 for more information). MDBs can use BMTs or container-managedtransactions (CMTs); they can explicitly roll back a transaction by using theMessageDrivenContext.setRollbackOnly() method, etc. There is some specificity about MDBs that isworth explaining.When we talk about transactions, we always think of relational databases. However, other resourcesare also transactional, such as messaging systems. If two or more operations have to succeed or failtogether, they form a transaction. With messaging, if two or more messages are sent, they have tosucceed (commit) or fail (roll back) together. How does this work in practice? The answer is thatmessages are not released to consumers until the transaction commits. The container will start atransaction before the onMessage() method is invoked and will commit the transaction when the methodreturns (unless the transaction was marked for rollback <strong>with</strong> setRollbackOnly()).Even though MDBs are transactional, they cannot execute in the client’s transaction context, as theydon’t have a client. Nobody explicitly invokes methods on MDBs, they just listen to a destination andconsume messages. There is no context passed from a client to an MDB, and client transaction contextto the onMessage() method cannot be passed.Table 13-8 compares CMTs <strong>with</strong> session beans and MDBs.407

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

Saved successfully!

Ooh no, something went wrong!