ARTEMIS-1164: NameNotFoundException when using java.naming.provider.url to set url via jndi
Support setting PROVIDER_URL on initial context to create default connection factories.
This commit is contained in:
parent
502f9c544c
commit
c6ac1d943c
|
@ -22,6 +22,7 @@ import javax.jms.Topic;
|
|||
import javax.naming.Context;
|
||||
import javax.naming.NamingException;
|
||||
import javax.naming.spi.InitialContextFactory;
|
||||
import java.util.HashMap;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
@ -38,6 +39,8 @@ import org.apache.activemq.artemis.uri.ConnectionFactoryParser;
|
|||
*/
|
||||
public class ActiveMQInitialContextFactory implements InitialContextFactory {
|
||||
|
||||
private static final String[] DEFAULT_CONNECTION_FACTORY_NAMES = {"ConnectionFactory", "XAConnectionFactory", "QueueConnectionFactory", "TopicConnectionFactory"};
|
||||
|
||||
public static final String REFRESH_TIMEOUT = "refreshTimeout";
|
||||
public static final String DISCOVERY_INITIAL_WAIT_TIMEOUT = "discoveryInitialWaitTimeout";
|
||||
public static final String DYNAMIC_QUEUE_CONTEXT = "dynamicQueues";
|
||||
|
@ -50,13 +53,14 @@ public class ActiveMQInitialContextFactory implements InitialContextFactory {
|
|||
public Context getInitialContext(Hashtable<?, ?> environment) throws NamingException {
|
||||
// lets create a factory
|
||||
Map<String, Object> data = new ConcurrentHashMap<>();
|
||||
|
||||
Map<String, ConnectionFactory> connectionFactories = new HashMap<>();
|
||||
for (Map.Entry<?, ?> entry : environment.entrySet()) {
|
||||
String key = entry.getKey().toString();
|
||||
if (key.startsWith(connectionFactoryPrefix)) {
|
||||
String jndiName = key.substring(connectionFactoryPrefix.length());
|
||||
try {
|
||||
ConnectionFactory factory = createConnectionFactory((String) environment.get(key), jndiName);
|
||||
data.put(jndiName, factory);
|
||||
connectionFactories.put(jndiName, createConnectionFactory((String) environment.get(key), jndiName));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new NamingException("Invalid broker URL");
|
||||
|
@ -64,6 +68,21 @@ public class ActiveMQInitialContextFactory implements InitialContextFactory {
|
|||
}
|
||||
}
|
||||
|
||||
if (connectionFactories.isEmpty()) {
|
||||
String providerUrl = (String) environment.get(javax.naming.Context.PROVIDER_URL);
|
||||
if (providerUrl != null) {
|
||||
for (String factoryName : DEFAULT_CONNECTION_FACTORY_NAMES) {
|
||||
try {
|
||||
connectionFactories.put(factoryName, createConnectionFactory(providerUrl, factoryName));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new NamingException("Invalid broker URL");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
connectionFactories.forEach((name, factory) -> data.put(name, factory));
|
||||
|
||||
createQueues(data, environment);
|
||||
createTopics(data, environment);
|
||||
|
||||
|
|
|
@ -35,6 +35,7 @@ import java.util.HashSet;
|
|||
import java.util.Hashtable;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.activemq.artemis.api.config.ActiveMQDefaultConfiguration;
|
||||
|
@ -526,4 +527,30 @@ public class SimpleJNDIClientTest extends ActiveMQTestBase {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void providerURLTest() throws NamingException {
|
||||
String url = "(tcp://somehost:62616,tcp://somehost:62616)?ha=true";
|
||||
|
||||
Properties props = new Properties();
|
||||
props.setProperty(javax.naming.Context.INITIAL_CONTEXT_FACTORY, ActiveMQInitialContextFactory.class.getName());
|
||||
props.setProperty(javax.naming.Context.PROVIDER_URL, url);
|
||||
|
||||
InitialContext context = new InitialContext(props);
|
||||
ConnectionFactory connectionFactory = (ConnectionFactory)context.lookup("ConnectionFactory");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void connectionFactoryProperty() throws NamingException {
|
||||
String url = "(tcp://somehost:62616,tcp://somehost:62616)?ha=true";
|
||||
|
||||
Properties props = new Properties();
|
||||
props.setProperty(javax.naming.Context.INITIAL_CONTEXT_FACTORY, ActiveMQInitialContextFactory.class.getName());
|
||||
props.setProperty(javax.naming.Context.PROVIDER_URL, url);
|
||||
|
||||
props.setProperty("connectionFactory.ConnectionFactory",url);
|
||||
|
||||
InitialContext context = new InitialContext(props);
|
||||
ConnectionFactory connectionFactory = (ConnectionFactory)context.lookup("ConnectionFactory");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue