Add property preferJndiDestinationLookup so that users can override the default order as needed and have the connector first go to JNDI before trying the JMS create method.

git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@1388326 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Timothy A. Bish 2012-09-21 05:11:57 +00:00
parent de7617e9c7
commit c176fa8f33
3 changed files with 78 additions and 22 deletions

View File

@ -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.
*/

View File

@ -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;
}
}

View File

@ -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;