How to find HornetQ instance from within application running on the same application server? (AS7) (Failed to instantiate InitialContextFactory)

StackOverflow https://stackoverflow.com/questions/19856836

Question

I'm trying to subscribe to a HornetQ instance running on a JBoss AS7 server. I've managed to connect and publish/receive messages with a test application which exists outside of the application server, but when I try to run a similar example deployed within the server I get the following exception at the point of creating an InitialContext instance in my message subscriber class:

javax.naming.NamingException: JBAS011843: Failed instantiate InitialContextFactory         org.jboss.naming.remote.client.InitialContextFactory from classloader ModuleClassLoader for     Module "deployment.Test.war:main" from Service Module Loader
  at org.jboss.as.naming.InitialContextFactoryBuilder.createInitialContextFactory(InitialContextFactoryBuilder.java:64)
  at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:681)
  at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307)
  at javax.naming.InitialContext.init(InitialContext.java:242)
  at javax.naming.InitialContext.<init>(InitialContext.java:216)
  at uk.co.test.MessageSubscriber.startSubscription(MessageSubscriber.java:127)
  at uk.co.test.MessageSubscriber.access$0(MessageSubscriber.java:114)
  at uk.co.test.MessageSubscriber$1.run(MessageSubscriber.java:47)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
  at java.lang.Thread.run(Thread.java:724)

And here's the code where I'm attempting to create the context (I've excluded the actual subscription, topic setup code etc out for clarity):

String factoryName = "jms/RemoteConnectionFactory"; 
String topicName = "jms/topic/test";

Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
props.put(Context.PROVIDER_URL, "remote://server:4447");
props.put(Context.SECURITY_PRINCIPAL, "testuser");
props.put(Context.SECURITY_CREDENTIALS, "password");
props.put("jboss.naming.client.ejb.context", true);
InitialContext context = new InitialContext(props); // Exception

The context creation works fine when I connect remotely to the server, so I assume there's a different approach for discovering the HornetQ that I've overlooked. Or maybe I'm missing a dependency somewhere, or it's a general configuration issue...either way I'm struggling to find any examples I can use to push forward with this.

Any ideas where I'm going wrong?

Thanks.

Was it helpful?

Solution

I think following shall be enough to connect locally:

InitialContext jndiContext = new InitialContext();
QueueConnectionFactory qf = (QueueConnectionFactory) jndiContext.lookup( "java:/ConnectionFactory" );

when connection factory is defined as

 <jms-connection-factories>
    <connection-factory name="InVmConnectionFactory">
       <connectors>
          <connector-ref connector-name="in-vm"/>
       </connectors>
       <entries>
          <entry name="ConnectionFactory"/>
       </entries>
     </connection-factory>
  </jms-connection-factories>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top