git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@1463528 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gary Tully 2013-04-02 13:26:08 +00:00
parent 719f5e8946
commit 670d9d84a7
3 changed files with 100 additions and 1 deletions

View File

@ -63,6 +63,7 @@
javax.jms*,
javax.management*,
javax.transaction*,
javax.naming*;resolution:=optional,
org.apache.commons.pool*;resolution:=optional,
org.apache.commons.net*;resolution:=optional,
com.sun.syndication*;resolution:=optional,

View File

@ -16,18 +16,53 @@
*/
package org.apache.activemq.pool;
import java.io.Serializable;
import java.util.Hashtable;
import javax.jms.JMSException;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.Name;
import javax.naming.spi.ObjectFactory;
import javax.transaction.TransactionManager;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.util.IntrospectionSupport;
/**
* A pooled connection factory that automatically enlists
* sessions in the current active XA transaction if any.
*/
public class XaPooledConnectionFactory extends PooledConnectionFactory {
public class XaPooledConnectionFactory extends PooledConnectionFactory implements ObjectFactory, Serializable, QueueConnectionFactory, TopicConnectionFactory
{
private TransactionManager transactionManager;
private boolean tmFromJndi = false;
private String tmJndiName = "java:/TransactionManager";
public String getTmJndiName() {
return tmJndiName;
}
public void setTmJndiName(String tmJndiName) {
this.tmJndiName = tmJndiName;
}
public boolean isTmFromJndi() {
return tmFromJndi;
}
/**
* Allow transaction manager resolution from JNDI (ee deployment)
* @param tmFromJndi
*/
public void setTmFromJndi(boolean tmFromJndi) {
this.tmFromJndi = tmFromJndi;
}
public XaPooledConnectionFactory() {
super();
@ -42,6 +77,13 @@ public class XaPooledConnectionFactory extends PooledConnectionFactory {
}
public TransactionManager getTransactionManager() {
if (transactionManager == null && tmFromJndi) {
try {
transactionManager = (TransactionManager) new InitialContext().lookup(getTmJndiName());
} catch (Throwable ignored) {
ignored.printStackTrace();
}
}
return transactionManager;
}
@ -53,4 +95,33 @@ public class XaPooledConnectionFactory extends PooledConnectionFactory {
protected ConnectionPool createConnectionPool(ActiveMQConnection connection) {
return new XaConnectionPool(connection, getTransactionManager());
}
@Override
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
setTmFromJndi(true);
if (environment != null) {
IntrospectionSupport.setProperties(this, environment);
}
return this;
}
@Override
public QueueConnection createQueueConnection() throws JMSException {
return (QueueConnection) createConnection();
}
@Override
public QueueConnection createQueueConnection(String userName, String password) throws JMSException {
return (QueueConnection) createConnection(userName, password);
}
@Override
public TopicConnection createTopicConnection() throws JMSException {
return (TopicConnection) createConnection();
}
@Override
public TopicConnection createTopicConnection(String userName, String password) throws JMSException {
return (TopicConnection) createConnection(userName, password);
}
}

View File

@ -16,11 +16,15 @@
*/
package org.apache.activemq.pool;
import java.util.Hashtable;
import java.util.Vector;
import javax.jms.QueueConnectionFactory;
import javax.jms.Session;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;
import javax.naming.spi.ObjectFactory;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.InvalidTransactionException;
@ -140,4 +144,27 @@ public class XAConnectionPoolTest extends TestSupport {
}
connection.close();
}
public void testInstanceOf() throws Exception {
XaPooledConnectionFactory pcf = new XaPooledConnectionFactory();
assertTrue(pcf instanceof QueueConnectionFactory);
assertTrue(pcf instanceof TopicConnectionFactory);
}
public void testBindable() throws Exception {
XaPooledConnectionFactory pcf = new XaPooledConnectionFactory();
assertTrue(pcf instanceof ObjectFactory);
assertTrue(((ObjectFactory)pcf).getObjectInstance(null, null, null, null) instanceof XaPooledConnectionFactory);
assertTrue(pcf.isTmFromJndi());
}
public void testBindableEnvOverrides() throws Exception {
XaPooledConnectionFactory pcf = new XaPooledConnectionFactory();
assertTrue(pcf instanceof ObjectFactory);
Hashtable<String, String> environment = new Hashtable<String, String>();
environment.put("tmFromJndi", String.valueOf(Boolean.FALSE));
assertTrue(((ObjectFactory) pcf).getObjectInstance(null, null, null, environment) instanceof XaPooledConnectionFactory);
assertFalse(pcf.isTmFromJndi());
}
}