12.11.2014 Views

web server - Borland Technical Publications

web server - Borland Technical Publications

web server - Borland Technical Publications

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

JMS and Transactions<br />

For instance:<br />

import javax.jms.*;<br />

QueueConnectionFactory nonXAQCF;<br />

Queue<br />

myQueue;<br />

try {<br />

javax.naming.Context ctx = (javax.naming.Context) new<br />

javax.naming.InitialContext();<br />

nonXAQCF = (QueueConnectionFactory) ctx.lookup("java:comp/env/jms/<br />

MyJMSQueueConnectionFactory");<br />

myQueue = (Queue) ctx.lookup("java:comp/env/jms/MyJMSQueue");<br />

}<br />

catch (javax.naming.NamingException exp) {<br />

exp.printStackTrace();<br />

}<br />

// Note: A global transaction context is currently active when the Session is<br />

being created<br />

QueueSession qSession = conn.createQueueSession(false,<br />

Session.AUTO_ACKNOWLEDGE);<br />

QueueSender = qSession.createSender(myQueue);<br />

TextMessage msg = qSession.createTextMessage("A Message ");<br />

sender.send(msg);<br />

Here, the TextMessage msg is queued regardless of the outcome of the active global<br />

transaction. This is in line with test cases in the J2EE Compatibility Test Suite (CTS<br />

1.3.1). It seems useful to have this capability in a global transaction, whereby a log<br />

message needs to be sent irrespective of the enclosing global transaction's completion<br />

result.<br />

Multiple resource access within a single global transaction is supported in BES. This<br />

provides the capability to do a unit of work which is composed of sending/receiving<br />

JMS messages along with some other type of resource manager access. That is, it is<br />

desirable to write code (in an EJB for example) which does some work against a non<br />

JMS resource such as a database and also send a message to a queue with the<br />

Container providing transactional completion for all work performed. Upon completion<br />

of the transaction, either the work performed against the database is committed AND<br />

the message is queued, or should something fail during the transaction database work<br />

is rolled back AND the message is not delivered to the queue.<br />

In application code, an EJB method such as doSomeWork() shown below is supported in<br />

BES:<br />

// Business method in a session bean, the EJB container marks the transaction<br />

void doSomeWork()<br />

{<br />

// Establish a database connection<br />

java.sql.Connection dbConn = datasource.getConnection();<br />

// Execute SQL<br />

...<br />

}<br />

// Call a remote EJB in the same transaction<br />

ejbRemote.doWork();<br />

// Send a JMS message to a queue<br />

jmsSender.send(msg);<br />

218 BES Developer’s Guide

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

Saved successfully!

Ooh no, something went wrong!