diff --git a/activemq-core/src/main/java/org/apache/activemq/network/jms/JmsConnector.java b/activemq-core/src/main/java/org/apache/activemq/network/jms/JmsConnector.java index 63aef74fbe..9b5fdd3178 100755 --- a/activemq-core/src/main/java/org/apache/activemq/network/jms/JmsConnector.java +++ b/activemq-core/src/main/java/org/apache/activemq/network/jms/JmsConnector.java @@ -50,6 +50,7 @@ public abstract class JmsConnector implements Service { private static int nextId; private static final Logger LOG = LoggerFactory.getLogger(JmsConnector.class); + protected boolean preferJndiDestinationLookup = false; protected JndiTemplate jndiLocalTemplate; protected JndiTemplate jndiOutboundTemplate; protected JmsMesageConvertor inboundMessageConvertor; @@ -374,6 +375,25 @@ public abstract class JmsConnector implements Service { this.policy = policy; } + /** + * @return the preferJndiDestinationLookup + */ + public boolean isPreferJndiDestinationLookup() { + return preferJndiDestinationLookup; + } + + /** + * Sets whether the connector should prefer to first try to find a destination in JNDI before + * using JMS semantics to create a Destination. By default the connector will first use JMS + * semantics and then fall-back to JNDI lookup, setting this value to true will reverse that + * ordering. + * + * @param preferJndiDestinationLookup the preferJndiDestinationLookup to set + */ + public void setPreferJndiDestinationLookup(boolean preferJndiDestinationLookup) { + this.preferJndiDestinationLookup = preferJndiDestinationLookup; + } + /** * @return returns true if the {@link JmsConnector} is connected to both brokers. */ diff --git a/activemq-core/src/main/java/org/apache/activemq/network/jms/JmsQueueConnector.java b/activemq-core/src/main/java/org/apache/activemq/network/jms/JmsQueueConnector.java index 61d3de42e0..930831dec6 100755 --- a/activemq-core/src/main/java/org/apache/activemq/network/jms/JmsQueueConnector.java +++ b/activemq-core/src/main/java/org/apache/activemq/network/jms/JmsQueueConnector.java @@ -429,21 +429,39 @@ public class JmsQueueConnector extends JmsConnector { protected Queue createForeignQueue(QueueSession session, String queueName) throws JMSException { Queue result = null; - try { - result = session.createQueue(queueName); - } catch (JMSException e) { - // look-up the Queue + + if (preferJndiDestinationLookup) { try { + // look-up the Queue result = (Queue)jndiOutboundTemplate.lookup(queueName, Queue.class); - } catch (NamingException e1) { - String errStr = "Failed to look-up Queue for name: " + queueName; - LOG.error(errStr, e); - JMSException jmsEx = new JMSException(errStr); - jmsEx.setLinkedException(e1); - throw jmsEx; + } catch (NamingException e) { + try { + result = session.createQueue(queueName); + } catch (JMSException e1) { + String errStr = "Failed to look-up or create Queue for name: " + queueName; + LOG.error(errStr, e); + JMSException jmsEx = new JMSException(errStr); + jmsEx.setLinkedException(e1); + throw jmsEx; + } + } + } else { + try { + result = session.createQueue(queueName); + } catch (JMSException e) { + // look-up the Queue + try { + result = (Queue)jndiOutboundTemplate.lookup(queueName, Queue.class); + } catch (NamingException e1) { + String errStr = "Failed to look-up Queue for name: " + queueName; + LOG.error(errStr, e); + JMSException jmsEx = new JMSException(errStr); + jmsEx.setLinkedException(e1); + throw jmsEx; + } } } + return result; } - } diff --git a/activemq-core/src/main/java/org/apache/activemq/network/jms/JmsTopicConnector.java b/activemq-core/src/main/java/org/apache/activemq/network/jms/JmsTopicConnector.java index 2d5389104b..db49539945 100755 --- a/activemq-core/src/main/java/org/apache/activemq/network/jms/JmsTopicConnector.java +++ b/activemq-core/src/main/java/org/apache/activemq/network/jms/JmsTopicConnector.java @@ -20,11 +20,11 @@ import javax.jms.Connection; import javax.jms.Destination; import javax.jms.ExceptionListener; import javax.jms.JMSException; +import javax.jms.Session; import javax.jms.Topic; import javax.jms.TopicConnection; import javax.jms.TopicConnectionFactory; import javax.jms.TopicSession; -import javax.jms.Session; import javax.naming.NamingException; import org.slf4j.Logger; @@ -426,18 +426,36 @@ public class JmsTopicConnector extends JmsConnector { protected Topic createForeignTopic(TopicSession session, String topicName) throws JMSException { Topic result = null; - try { - result = session.createTopic(topicName); - } catch (JMSException e) { - // look-up the Topic + + if (preferJndiDestinationLookup) { try { + // look-up the Queue result = (Topic)jndiOutboundTemplate.lookup(topicName, Topic.class); - } catch (NamingException e1) { - String errStr = "Failed to look-up Topic for name: " + topicName; - LOG.error(errStr, e); - JMSException jmsEx = new JMSException(errStr); - jmsEx.setLinkedException(e1); - throw jmsEx; + } catch (NamingException e) { + try { + result = session.createTopic(topicName); + } catch (JMSException e1) { + String errStr = "Failed to look-up or create Topic for name: " + topicName; + LOG.error(errStr, e); + JMSException jmsEx = new JMSException(errStr); + jmsEx.setLinkedException(e1); + throw jmsEx; + } + } + } else { + try { + result = session.createTopic(topicName); + } catch (JMSException e) { + // look-up the Topic + try { + result = (Topic)jndiOutboundTemplate.lookup(topicName, Topic.class); + } catch (NamingException e1) { + String errStr = "Failed to look-up Topic for name: " + topicName; + LOG.error(errStr, e); + JMSException jmsEx = new JMSException(errStr); + jmsEx.setLinkedException(e1); + throw jmsEx; + } } } return result;