Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I can't setup my jndi.properties to access remote EJBs on Jboss 5

I'm trying to setup the Jboss server "client" (version 5.1.0) to use remote EJBs from another Jboss server (10.90.0.91), but I can't do this using a jndi.properties file on the Jboss client.

I can get the remote EJB using this simple code on my client:

        InitialContext ctx = null;
        try {
            Hashtable<String, String> jndiProps = new Hashtable<String, String>();
            jndiProps.put(InitialContext.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
            jndiProps.put(InitialContext.PROVIDER_URL, "jnp://10.90.0.91:1099");
            ctx = new InitialContext(jndiProps);
            return ctx.lookup(jndiName);
        } catch (NamingException e) {
            throw new RuntimeException(e);
        }

This works fine.

Now I would like to setup the Jboss client with this properties. But if I edit the existent jndi.properties file localized on server/{application}/conf/ from:

# DO NOT EDIT THIS FILE UNLESS YOU KNOW WHAT YOU ARE DOING
#
java.naming.factory.initial=org.jboss.iiop.naming.ORBInitialContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces

To:

# DO NOT EDIT THIS FILE UNLESS YOU KNOW WHAT YOU ARE DOING
#
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.provider.url=jnp://10.90.0.91:1099

I receive some errors when I start the Jboss client (apparently, I don't know what I'm doing :)):

2016-08-19 10:17:41,645 ERROR [org.jboss.kernel.plugins.dependency.AbstractKernelController] (main) Error installing to Start: name=HASessionStateService state=Create
javax.naming.NameAlreadyBoundException: Default
    at org.jnp.server.NamingServer.bind(NamingServer.java:209)
    at org.jnp.server.NamingServer.bind(NamingServer.java:167)
[...]

2016-08-19 10:17:42,767 ERROR [org.jboss.kernel.plugins.dependency.AbstractKernelController] (main) Error installing to Start: name=ProfileServiceProxyFactory state=Create
javax.naming.NameAlreadyBoundException: ProfileService
    at org.jnp.server.NamingServer.bind(NamingServer.java:209)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[...]

2016-08-19 10:17:44,778 ERROR [org.jboss.kernel.plugins.dependency.AbstractKernelController] (main) Error installing to Start: name=jboss:service=ClientUserTransaction state=Create mode=Manual requiredState=Installed
javax.naming.NameAlreadyBoundException: UserTransaction
    at org.jnp.server.NamingServer.bind(NamingServer.java:209)
    at sun.reflect.GeneratedMethodAccessor487.invoke(Unknown Source)
[...]

And in the final:

2016-08-19 10:17:51,993 ERROR [org.jboss.system.server.profileservice.ProfileServiceBootstrap] (main) Failed to load profile: Summary of incomplete deployments (SEE PREVIOUS ERRORS FOR DETAILS):

DEPLOYMENTS MISSING DEPENDENCIES:
  Deployment "ProfileServiceInvocationHandler" is missing the following dependencies:
    Dependency "ProfileServiceProxyFactory" (should be in state "Configured", but is actually in state "**ERROR**")
    Dependency "ProfileServiceProxyFactory" (should be in state "Configured", but is actually in state "**ERROR**")

DEPLOYMENTS IN ERROR:
  Deployment "jboss:service=ClientUserTransaction" is in error due to the following reason(s): javax.naming.NameAlreadyBoundException: UserTransaction
  Deployment "HASessionStateService" is in error due to the following reason(s): javax.naming.NameAlreadyBoundException: Default
  Deployment "ProfileServiceProxyFactory" is in error due to the following reason(s): javax.naming.NameAlreadyBoundException: ProfileService, **ERROR**

So, I think I can't touch in already existent JNDI properties on that file.

If the jndi.properties file can't be changed because it is being used by JBoss itself, in which location can I set my JNDI lookup settings to the remote EJBs within the Jboss 5? How can I configure a jndi.properties file to be available in the application classpath without put the jndi.properties file inside of my WAR file?

Thanks!

like image 760
Dherik Avatar asked Aug 19 '16 14:08

Dherik


People also ask

Does JBoss supports JNDI?

Every JBoss AS instance has it's own local JNDI namespace (java:) which is unique per JVM. The layout of this namespace is primarily governed by the Java EE specification. Applications which share the same JBoss AS instance can use this namespace to intercommunicate.

What is JNDI name in JBoss?

The JBoss naming service is an implementation of the Java Naming and Directory Interface (JNDI).

How do I access my EJB remote?

To access Enterprise JavaBeans deployed in an application you use the JNDI API. JNDI Registry Service is the standard way to associate names with objects and to find objects by their names. Remote clients use the JNDI to look up objects. To connect to JNDI you have to create a javax.

What is JNDI name in Wildfly?

The Java Naming and Directory Interface (JNDI) is a Java API for a directory service that allows Java software clients to discover and look up data and objects via a name.


2 Answers

An alternative way to do this is to configure a org.jboss.naming.ExternalContext MBean in your jboss-service.xml file:

<mbean code="org.jboss.naming.ExternalContext" 
       name="jboss.jndi:service=ExternalContext,jndiName=external/server2">
    <attribute name="JndiName">external/server2</attribute>
    <attribute name="Properties">
        java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
        java.naming.provider.url=jnp://10.90.0.91:1099
        <!-- other properties as needed -->
    </attribute>
    <attribute name="InitialContext"> javax.naming.IntialContext </attribute>
    <attribute name="RemoteAccess">false</attribute>
</mbean>

Your java code to perform the lookup then becomes:

 Context initialContext = new InitialContext();
 return initialContext.lookup("external/server2/" + jndiName);

You can even navigate the remote JNDI tree using JNDIView in the local management console when you set this up.

More information can be found in org.jboss.naming.ExternalContext MBean.

like image 82
Steve C Avatar answered Sep 24 '22 19:09

Steve C


Well, I found another solution.

I created a new file called jndi-remote.properties on the configuration directory from Jboss:

{jboss_home}/server/default/conf/jndi-remote.properties

And I access the file in Jboss config directory (System.getProperty("jboss.server.config.url")) from Java:

String fileName = System.getProperty("jboss.server.config.url") + "/" + "jndi-remote.properties";

Properties properties = null;
try {
    URL url = new URL(fileName);
    if(new File(url.toURI()).exists()) { 
        properties = new Properties();
        properties.load(url.openStream());
        LOGGER.info("The file " + "jndi-remote.properties" + " was loaded from " + fileName);
    }
} catch (MalformedURLException e) {
    //throw
} catch (URISyntaxException e) {
    //throw
} catch (IOException e) {
    //throw
} 

And initialize my InitialContext:

if (properties != null) {
    ctx = new InitialContext(properties);
}

Works :).

like image 35
Dherik Avatar answered Sep 22 '22 19:09

Dherik