Archive

Archive for March, 2009

EJB3 JNDI

March 24, 2009 Leave a comment

EJB3 follow a strange naming convention by default. I have yet to find a way to define how this can be set using annotations.

Lets say you define the following bean:

package test.mybeans.service;

@Stateless(name=”TestServiceBean”, mappedName=”TestService”)

@Remote(TestService.class)

public class TestServiceBean implements TestService

This creates a stateless bean implementation of the interface TestService, this is the remote interface you will use to call the services.

When you deploy this bean, if you check your containers jndi tree, it will have the following name

TestService#test.mybeans.service.TestService

In order to acess the remote interface you will need to get the bean from your context: you can use something like this:

ctx.lookup(“TestService#test.mybeans.service.TestService”);

This will return a proxy object of TestService which will allow you to access your service calls. Note: ctx will be your initial context(the context where you have your service running)

I use the following methods to make this access easier:

public static synchronized Object getSessionInterface(Class interfaceClass){
    try{
        InitialContext ctx = new InitialContext();
        return ctx.lookup(getSessionJndiName(interfaceClass));
} catch (NamingException e) { log.error("Error Obtaining sessionbean for " +interfaceClass, e); return null; } }
public static synchronized String getSessionJndiName(Class interfaceClass){ return interfaceClass.getSimpleName() + "#" + interfaceClass.getName();
}
Note: Mind that the creation of your InitialContext will be diferent if for instance you are running your services in a diferent server.
Advertisements
Categories: ejb3 Tags: , , ,