Home > ejb3 > EJB3 JNDI

EJB3 JNDI

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: , , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: