Domanda

This is my first question at Stack Overflow, so feel free to tell me if I do something wrong :)

I'm working on a project involving EJB and JBoss 4.2.3.GA. In a point, we try to access every node of the cluster, locating an EJB and returning it.

This is the piece of code that does the JNDI lookup:

public static <I> I getCache(Class<I> i, String clusterNode) {
    ServiceLocator serviceLocator = ServiceLocator.getInstance();
    String jndi = serviceLocator.getRemoteJNDIName(i);

    Properties props = new Properties();
    props.setProperty(Context.PROVIDER_URL, "jnp://" + clusterNode + ":"
            + jndiPort);
    props.setProperty(Context.URL_PKG_PREFIXES, "org.jboss.naming");
    props.setProperty(Context.INITIAL_CONTEXT_FACTORY,
            "org.jnp.interfaces.NamingContextFactory");
    Object result = null;
    try {
        InitialContext ctx = new InitialContext(props);
        result = ctx.lookup(jndi);
    } catch (NamingException e) {
        return null;
    }

    return (I) result;
}

Here:

  • clusterNode is a simple string with the IP address or the dns name of the node. E.g: "192.168.2.65" or "cluster1".
  • getRemoteJNDIName returns an String such as this: "MyEARName/MyEJBName/remote"

The problem is that when I call this method with, for example, "127.0.0.1" it works fine. Also, if I call it with an existing and working IP address where a server is up and running, it's OK too.

However if I call the method with a non-existing or non-working address or dns name, instead of throwing the NamingException, it returns the EJB in my own machine. Therefore, I don't know wether the node is up or not.

I guess there may be better ways to do it. I'd like to hear about them, but we cannot make "big" changes to the product due to it being in production for a few years by now.

That's it. Thank you in anticipation and best regards.

È stato utile?

Soluzione

However if I call the method with a non-existing or non-working address or dns name, instead of throwing the NamingException, it returns the EJB in my own machine

I think this behavior can be explained if you have automatic naming discovery. When the Contex.PROVIDER_URL is not specified or the nodes in the list are not reachable (which is your case) the client is allowed to search in the network for available JNDI services.

However this only works under certain conditions, some of them:all clusters node running in ALL mode, all nodes located in the same subnet.

You can disable this behavior setting the InitialContext property jnp.disableDiscovery=true.

I guess there may be better ways to do it

According to the code, you are not catching the object polled from the JNDI, this implies that every time you need to execute a service, a new lookup (wich is a time consuming operation) has to be done. The ServiceLocator pattern suggests caching the lookup result for improving performance.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top