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 int nextId;
private static final Logger LOG = LoggerFactory.getLogger(JmsConnector.class); private static final Logger LOG = LoggerFactory.getLogger(JmsConnector.class);
protected boolean preferJndiDestinationLookup = false;
protected JndiTemplate jndiLocalTemplate; protected JndiTemplate jndiLocalTemplate;
protected JndiTemplate jndiOutboundTemplate; protected JndiTemplate jndiOutboundTemplate;
protected JmsMesageConvertor inboundMessageConvertor; protected JmsMesageConvertor inboundMessageConvertor;
@ -374,6 +375,25 @@ public abstract class JmsConnector implements Service {
this.policy = policy; 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. * @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 { protected Queue createForeignQueue(QueueSession session, String queueName) throws JMSException {
Queue result = null; Queue result = null;
try {
result = session.createQueue(queueName); if (preferJndiDestinationLookup) {
} catch (JMSException e) {
// look-up the Queue
try { try {
// look-up the Queue
result = (Queue)jndiOutboundTemplate.lookup(queueName, Queue.class); result = (Queue)jndiOutboundTemplate.lookup(queueName, Queue.class);
} catch (NamingException e1) { } catch (NamingException e) {
String errStr = "Failed to look-up Queue for name: " + queueName; try {
LOG.error(errStr, e); result = session.createQueue(queueName);
JMSException jmsEx = new JMSException(errStr); } catch (JMSException e1) {
jmsEx.setLinkedException(e1); String errStr = "Failed to look-up or create Queue for name: " + queueName;
throw jmsEx; 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; return result;
} }
} }

View File

@ -20,11 +20,11 @@ import javax.jms.Connection;
import javax.jms.Destination; import javax.jms.Destination;
import javax.jms.ExceptionListener; import javax.jms.ExceptionListener;
import javax.jms.JMSException; import javax.jms.JMSException;
import javax.jms.Session;
import javax.jms.Topic; import javax.jms.Topic;
import javax.jms.TopicConnection; import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory; import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSession; import javax.jms.TopicSession;
import javax.jms.Session;
import javax.naming.NamingException; import javax.naming.NamingException;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -426,18 +426,36 @@ public class JmsTopicConnector extends JmsConnector {
protected Topic createForeignTopic(TopicSession session, String topicName) throws JMSException { protected Topic createForeignTopic(TopicSession session, String topicName) throws JMSException {
Topic result = null; Topic result = null;
try {
result = session.createTopic(topicName); if (preferJndiDestinationLookup) {
} catch (JMSException e) {
// look-up the Topic
try { try {
// look-up the Queue
result = (Topic)jndiOutboundTemplate.lookup(topicName, Topic.class); result = (Topic)jndiOutboundTemplate.lookup(topicName, Topic.class);
} catch (NamingException e1) { } catch (NamingException e) {
String errStr = "Failed to look-up Topic for name: " + topicName; try {
LOG.error(errStr, e); result = session.createTopic(topicName);
JMSException jmsEx = new JMSException(errStr); } catch (JMSException e1) {
jmsEx.setLinkedException(e1); String errStr = "Failed to look-up or create Topic for name: " + topicName;
throw jmsEx; 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; return result;