https://issues.apache.org/jira/browse/AMQ-4426 - allow objectfactory prop config via jndi, java:/amq/pcf config properties as strings in java:/amq/conf/pfc context

git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@1463790 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gary Tully 2013-04-02 23:19:42 +00:00
parent 059fd2f8c4
commit 07c3db5eeb
2 changed files with 84 additions and 46 deletions

View File

@ -23,26 +23,104 @@ import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.naming.Binding;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.Name;
import javax.naming.NamingEnumeration;
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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* A pooled connection factory that automatically enlists
* sessions in the current active XA transaction if any.
*/
public class XaPooledConnectionFactory extends PooledConnectionFactory implements ObjectFactory, Serializable, QueueConnectionFactory, TopicConnectionFactory
{
public class XaPooledConnectionFactory extends PooledConnectionFactory implements ObjectFactory,
Serializable, QueueConnectionFactory, TopicConnectionFactory {
private static final transient Logger LOG = LoggerFactory.getLogger(XaPooledConnectionFactory.class);
private TransactionManager transactionManager;
private boolean tmFromJndi = false;
private String tmJndiName = "java:/TransactionManager";
private String brokerUrl = null;
public XaPooledConnectionFactory() {
super();
}
public XaPooledConnectionFactory(ActiveMQConnectionFactory connectionFactory) {
super(connectionFactory);
}
public XaPooledConnectionFactory(String brokerURL) {
super(brokerURL);
}
public TransactionManager getTransactionManager() {
if (transactionManager == null && tmFromJndi) {
try {
transactionManager = (TransactionManager) new InitialContext().lookup(getTmJndiName());
} catch (Throwable ignored) {
if (LOG.isTraceEnabled()) {
LOG.trace("exception on tmFromJndi: " + getTmJndiName(), ignored);
}
}
}
return transactionManager;
}
public void setTransactionManager(TransactionManager transactionManager) {
this.transactionManager = transactionManager;
}
@Override
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);
configFromJndiConf(obj);
if (environment != null) {
IntrospectionSupport.setProperties(this, environment);
}
return this;
}
private void configFromJndiConf(Object rootContextName) {
if (rootContextName instanceof String) {
String name = (String) rootContextName;
name = name.substring(0, name.lastIndexOf('/')) + "/conf" + name.substring(name.lastIndexOf('/'));
try {
InitialContext ctx = new InitialContext();
NamingEnumeration bindings = ctx.listBindings(name);
while (bindings.hasMore()) {
Binding bd = (Binding)bindings.next();
IntrospectionSupport.setProperty(this, bd.getName(), bd.getObject());
}
} catch (Exception ignored) {
if (LOG.isTraceEnabled()) {
LOG.trace("exception on config from jndi: " + name, ignored);
}
}
}
}
public void setBrokerUrl(String url) {
if (brokerUrl == null || !brokerUrl.equals(url)) {
brokerUrl = url;
setConnectionFactory(new ActiveMQConnectionFactory(brokerUrl));
}
}
public String getTmJndiName() {
return tmJndiName;
@ -64,47 +142,6 @@ public class XaPooledConnectionFactory extends PooledConnectionFactory implement
this.tmFromJndi = tmFromJndi;
}
public XaPooledConnectionFactory() {
super();
}
public XaPooledConnectionFactory(ActiveMQConnectionFactory connectionFactory) {
super(connectionFactory);
}
public XaPooledConnectionFactory(String brokerURL) {
super(brokerURL);
}
public TransactionManager getTransactionManager() {
if (transactionManager == null && tmFromJndi) {
try {
transactionManager = (TransactionManager) new InitialContext().lookup(getTmJndiName());
} catch (Throwable ignored) {
ignored.printStackTrace();
}
}
return transactionManager;
}
public void setTransactionManager(TransactionManager transactionManager) {
this.transactionManager = transactionManager;
}
@Override
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();

View File

@ -40,6 +40,7 @@ import javax.transaction.SystemException;
import javax.transaction.Transaction;
import javax.transaction.TransactionManager;
import javax.transaction.xa.XAResource;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.ActiveMQXAConnectionFactory;
import org.apache.activemq.ActiveMQXASession;
import org.apache.activemq.command.ActiveMQTopic;
@ -157,14 +158,14 @@ public class XAConnectionPoolTest extends TestSupport {
public void testBindable() throws Exception {
XaPooledConnectionFactory pcf = new XaPooledConnectionFactory();
assertTrue(pcf instanceof ObjectFactory);
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);
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);
@ -173,7 +174,7 @@ public class XAConnectionPoolTest extends TestSupport {
public void testSenderAndPublisherDest() throws Exception {
XaPooledConnectionFactory pcf = new XaPooledConnectionFactory();
pcf.setConnectionFactory(new ActiveMQXAConnectionFactory("vm://test?broker.persistent=false"));
pcf.setConnectionFactory(new ActiveMQConnectionFactory("vm://test?broker.persistent=false"));
QueueConnection connection = pcf.createQueueConnection();
QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);