0

I am trying to set a new InitialContext in the following manner (which is pretty standard I believe):

private static InitialContext getInitialContext() throws NamingException {

    InitialContext context = null;

    try {
        Properties properties = new Properties();
        properties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
        properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
        properties.put(Context.PROVIDER_URL, "remote://localhost:4447");
        properties.put(Context.SECURITY_PRINCIPAL, "username");
        properties.put(Context.SECURITY_CREDENTIALS, "password");
        context = new InitialContext(properties);
        System.out.println("\n\tGot initial Context: " + context);
    } 
    catch (Exception e) {
        e.printStackTrace();
    }
    return context;
}

public static void sendMessage(RoboticsParameters object_msg) throws Exception {

    InitialContext context = getInitialContext();

    // other code
}

The code "fails" at the line where the new InitialContext is created using the properties and i get a java.lang.NullPointerException. I suspect I am missing an argument. Here is the stack trace:

WARN: EJB client integration will not be available due to a problem setting up the EJB client handler java.lang.NullPointerException
at org.jboss.naming.remote.client.InitialContextFactory.<clinit>(InitialContextFactory.java:118)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:72)
at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:61)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:672)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
at javax.naming.InitialContext.init(InitialContext.java:244)
at javax.naming.InitialContext.<init>(InitialContext.java:216)

Any suggestions?

I am running JBoss EAP 6.4 and using EJB 3. I have jboss-client.jar in the class path.

S.A.Norton Stanley
  • 1,833
  • 3
  • 23
  • 37
Tanvir
  • 1,453
  • 2
  • 16
  • 32
  • I think you're missing a property: `properties.put("jboss.naming.client.ejb.context", true);`. You can find the documentation [here](https://access.redhat.com/documentation/en-US/JBoss_Enterprise_Application_Platform/6.4/html/Migration_Guide/sect-Changes_Dependent_on_Your_Application_Architecture_and_Components.html#Invoke_a_Session_Bean_Remotely_using_JNDI1) and examples [here](http://blog.akquinet.de/2014/09/26/jboss-eap-wildfly-three-ways-to-invoke-remote-ejbs/). – António Ribeiro Feb 17 '16 at 11:11
  • I've tried that but it didn't make any difference. – Tanvir Feb 17 '16 at 16:03
  • How are you testing your `getInitialContext()` method? Do you have an application deployed on JBoss that's invoking it or are you invoking it through a main class...? – António Ribeiro Feb 17 '16 at 16:41
  • @aribeiro I am invoking it through a main class for testing purposes. I found the solution! I needed to add the maven dependencies instead of having the ``jboss-client.jar`` in the class path. – Tanvir Feb 18 '16 at 17:43

1 Answers1

0

I checked the source code for:

jboss-remote-naming/src/main/java/org/jboss/naming/remote/client/InitialContextFactory.java

and found where the log message was coming from:

public class InitialContextFactory implements javax.naming.spi.InitialContextFactory {
    // code
    private static final String REMOTE_NAMING_EJB_CLIENT_HANDLER_CLASS_NAME = "org.jboss.naming.remote.client.ejb.RemoteNamingStoreEJBClientHandler";
    // code
        try {
            klass = classLoader.loadClass(REMOTE_NAMING_EJB_CLIENT_HANDLER_CLASS_NAME);
            method = klass.getMethod("setupEJBClientContext", new Class<?>[] {Properties.class, List.class});
        } catch (Throwable t) {
            logger.warn("EJB client integration will not be available due to a problem setting up the EJB client handler", t);
        }
    // other code
}

The class org.jboss.naming.remote.client.ejb.RemoteNamingStoreEJBClientHandler was in the jar that I added to the class path but for some reason there were problems loading the class.

Then I stumbled upon this small README-EJB-JMS.txt file in the [jboss_home]/bin/client folder which states the following:

"Maven users should not use this jar, but should use the following BOM dependencies instead

<dependencies>
    <dependency>
        <groupId>org.jboss.as</groupId>
        <artifactId>jboss-as-ejb-client-bom</artifactId>
        <type>pom</type>
    </dependency>
    <dependency>
        <groupId>org.jboss.as</groupId>
        <artifactId>jboss-as-jms-client-bom</artifactId>
        <type>pom</type>
    </dependency>
</dependencies>

This is because using maven with a shaded jar has a very high chance of causing class version conflicts, which is why we do not publish this jar to the maven repository."

So, I added the maven dependency instead of having the jar in my class path and VOILA! It works!

Tanvir
  • 1,453
  • 2
  • 16
  • 32