[jboss-user] [Messaging, JMS & JBossMQ] - Re: MDB and connection to a remote queue with transaction

hoigh do-not-reply at jboss.com
Wed Jan 24 04:30:10 EST 2007


Thanks for the answer. I don't know if the article actually helps. However, I have found my own solution. If you want to send a message in a container managed transaction to a remote destination, the nuts and bolts is to care about the XAConnection, i.e., the usage of XA classes is mandatory.

This is some code exctracted from my application that works fine now:


  | public void sendMessage(Message message, String providerUrl, String queueName, String user, String password) {
  |         Context context = null;
  |         
  |         XAQueueConnectionFactory remoteQueueFactoryXA = null;
  |         XAQueueConnection connectionXA = null;
  |         
  |         Queue queue = null;
  |         QueueSession session = null;
  |         QueueSender sender = null;
  | 
  |         try {
  |             Properties env = new Properties();
  |             env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
  |             env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
  |             env.put(Context.PROVIDER_URL, providerUrl);
  |             context = new InitialContext(env);
  |                  
  |             remoteQueueFactoryXA = (XAQueueConnectionFactory)context.lookup("XAConnectionFactory");
  |             connectionXA = remoteQueueFactoryXA.createXAQueueConnection(user, password);
  |             session = connectionXA.createQueueSession(true, -1);
  |             queue = (Queue)context.lookup(queueName);
  |             sender = session.createSender(queue);
  |             sender.send(queue, message);
  |         }
  |         catch(Exception excep) {
  | 	   ... 
  |         }
  |         finally {
  |             if(sender != null) {
  |                 try {
  |                     sender.close();
  |                 } catch(Exception excep) {
  | 		   ...
  |                 }
  |             }
  |             if(session != null) {
  |                 try {
  |                     session.close();
  |                 } catch(Exception excep) {
  |                     ...
  |                 }
  |             }
  |             if(connectionXA != null) {
  |                 try {
  |                     connectionXA.close();
  |                 } catch(Exception excep) {
  |                     ...
  |                 }
  |             }
  |         }
  | }
  | 

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4005708#4005708

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4005708



More information about the jboss-user mailing list