git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@1475798 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gary Tully 2013-04-25 14:33:54 +00:00
parent 66e8011e9e
commit f2b01a7f01
2 changed files with 25 additions and 16 deletions

View File

@ -34,6 +34,7 @@ import javax.transaction.xa.XAResource;
import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory; import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.RedeliveryPolicy; import org.apache.activemq.RedeliveryPolicy;
import org.apache.activemq.TransactionContext;
import org.apache.activemq.broker.BrokerFactory; import org.apache.activemq.broker.BrokerFactory;
import org.apache.activemq.broker.BrokerService; import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.util.ServiceSupport; import org.apache.activemq.util.ServiceSupport;
@ -233,26 +234,21 @@ public class ActiveMQResourceAdapter extends ActiveMQConnectionSupport implement
* @see javax.resource.spi.ResourceAdapter#getXAResources(javax.resource.spi.ActivationSpec[]) * @see javax.resource.spi.ResourceAdapter#getXAResources(javax.resource.spi.ActivationSpec[])
*/ */
public XAResource[] getXAResources(ActivationSpec[] activationSpecs) throws ResourceException { public XAResource[] getXAResources(ActivationSpec[] activationSpecs) throws ResourceException {
Connection connection = null;
try { try {
connection = makeConnection(); final ActiveMQConnection connection = makeConnection();
if (connection instanceof XAConnection) { return new XAResource[]{new LocalAndXATransaction(new TransactionContext(connection)) {
XASession session = ((XAConnection)connection).createXASession(); public void finalize() throws Throwable {
XAResource xaResource = session.getXAResource();
return new XAResource[] {
xaResource
};
}
return new XAResource[] {};
} catch (JMSException e) {
throw new ResourceException(e);
} finally {
try { try {
connection.close(); connection.close();
} catch (Throwable ignore) { } catch (Throwable ignore) {
// } finally {
super.finalize();
} }
} }
}};
} catch (JMSException e) {
throw new ResourceException(e);
}
} }
// /////////////////////////////////////////////////////////////////////// // ///////////////////////////////////////////////////////////////////////

View File

@ -30,6 +30,7 @@ import javax.resource.spi.UnavailableException;
import javax.resource.spi.XATerminator; import javax.resource.spi.XATerminator;
import javax.resource.spi.work.WorkManager; import javax.resource.spi.work.WorkManager;
import javax.transaction.xa.XAResource;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQTopicSubscriber; import org.apache.activemq.ActiveMQTopicSubscriber;
@ -105,4 +106,16 @@ public class ActiveMQConnectionFactoryTest extends TestCase {
assertEquals(0, ((ActiveMQTopicSubscriber)sub).getPrefetchNumber()); assertEquals(0, ((ActiveMQTopicSubscriber)sub).getPrefetchNumber());
} }
public void testGetXAResource() throws Exception {
ActiveMQResourceAdapter ra = new ActiveMQResourceAdapter();
ra.setServerUrl(url);
ra.setUserName(user);
ra.setPassword(pwd);
XAResource[] resoruces = ra.getXAResources(null);
assertEquals("one resource", 1, resoruces.length);
assertEquals("no pending transactions", 0, resoruces[0].recover(100).length);
}
} }