[jboss-user] [JBoss Messaging] - Re: Lookup for JmsXA returning null
mskonda
do-not-reply at jboss.com
Thu Jun 21 05:59:04 EDT 2007
Hello Tim, this is what I did and it works!!
| /**
| * Method that receives the messages on the specific destination and persist
| * them into a database table all in one transaction.
| *
| * If the transction is committed, the message is consumed from the
| * destination and acknowledged and a row appears in the database.
| *
| * If the transaciton is rolled back, the message still apears on the
| * destination and the row does not get persisted in the database
| *
| * @param destination
| * @param isDurable
| * @throws Exception
| */
| public void receiveAndPersistCommit() throws Exception
| {
|
| /*
| * Create XAConnection and XASession objects
| */
| XAConnection c = jbcf.createXAConnection();
| XASession xaSession = c.createXASession();
|
| /*
| * Obtain a Messaging (JMS) XA Resource
| */
| MessagingXAResource msgRes =
| (MessagingXAResource) xaSession.getXAResource();
|
| /*
| * Create a sybase resource
| */
| SybaseXAResource sybaseRes = new SybaseXAResource();
| javax.sql.XAConnection sybaseXaConn = sybaseRes.getXAConnection();
|
| /*
| * Obtain a sybase XA Resource
| */
| XAResource sybaseXaResource = sybaseXaConn.getXAResource();
|
| /*
| * Strat the transaction manager
| */
| tm.begin();
|
| /*
| * Obtain a reference to the transaction object
| */
| Transaction tx = tm.getTransaction();
|
| logger.info("Enlisting resources");
|
| /*
| * Enlist the resources
| */
| tx.enlistResource(msgRes);
| tx.enlistResource(sybaseXaResource);
|
| /*
| * Start the connection. Unless you start, you won't see messages
| * being consumedd (usual culprit)
| */
| c.start();
|
| logger.info("Connection started");
|
| /*
| * Create a message consumer
| */
| MessageConsumer mc = xaSession.createConsumer(XATEST_QUEUE_1_DEST);
|
| logger.info("Consumer started ");
|
| Message msg = mc.receiveNoWait();
|
| if (msg != null)
| {
| logger.info("Message received: " + ((TextMessage) msg).getText());
| }
| else
| {
| logger.info("No message");
| }
|
| java.sql.Connection sybaseConn = sybaseXaConn.getConnection();
| java.sql.Statement stmt = sybaseConn.createStatement();
|
| logger.info("Inserting values into table");
| /*
| * Execute the insert statement
| */
| int i = stmt.executeUpdate("INSERT INTO XATEST VALUES('test-"
| + System.currentTimeMillis() / 1000 + "')");
| logger.info("Rows affected: " + i);
|
| /*
| * Unless you commit, the changes are not going to be affected
| */
| tx.commit();
| logger.info("Transaction committed");
|
| logger.info("Closing connections");
| /*
| * Close all connections
| */
| closeConnection(c);
| sybaseConn.close();
| }
|
Important to note is - I'm looking a ConnectionFactory but casting it to JBossConnectionFactory to obtain a XAConnection/XASession and hence MessagingResource. Also, I'm using a static method to create a transaction manager as shown below in setUp method:
| public void setUp()
| {
| try
| {
| initialContext = new InitialContext();
| /*
| * Lookup for ConnectionFactory
| */
| jbcf = (JBossConnectionFactory) initialContext
| .lookup("ConnectionFactory");
| logger.info("found JBconnection factory: " + jbcf);
|
| /*
| * Create a Transaction Manager
| */
|
| tm = com.arjuna.ats.jta.TransactionManager.transactionManager();
|
| /*
| * Deploy required destinations for this test
| */
|
| deployDestination(XATEST_QUEUE_1);
|
| logger.info("Deployed " + XATEST_QUEUE_1);
|
| XATEST_QUEUE_1_DEST =
| (Destination) initialContext.lookup(XATEST_QUEUE_1);
| deployDestination(XATEST_TOPIC_1);
|
| logger.info("Deployed " + XATEST_TOPIC_1);
| XATEST_TOPIC_1_DEST =
| (Destination) initialContext.lookup(XATEST_TOPIC_1);
| }
| catch (NamingException e)
| {
| logger.error("NamingException while looking up cf/tm: "
| + e.getMessage());
| }
| catch (Exception e)
| {
| logger.error("Exception in setup: " + e.getMessage());
| }
| }
|
The sendAndPerssitCommit works too.
Thanks for all your input Tim. Let me know if there's any obvious pitfalls.
Thanks
Madhu
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4056366#4056366
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4056366
More information about the jboss-user
mailing list