Java – jndi binding for local and remote stateless bean

ejb-3.0jakarta-eejavajndistateless-session-bean

I'm trying to implements an EJB3 stateless with a remote and local interfaces the problem is the local one is called in an other remote EJB with the annotation @EJB but it returns null or ClassCastException (java.lang.ClassCastException: com.sun.proxy.$Proxy58 cannot be cast).

To perform the lookup on the server to get the local stateless I have to put 2 JNDI names for the stateless else it gives me the remote one.

@Stateless(mappedName=IRemoteInterface.JNDI_NAME, description="...")
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
@Interceptors({GenericInvocationHandler.class})
@Remote(IRemoteInterface.class)
@Local(ILocalInterface.class)
public class MystatelessBean extends AbstractBasicBean implements 
    IRemoteInterface, ILocalInterface {
   ...
}

@Stateless(mappedName=IRouting.JNDI_NAME, description="gives access to other services")
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
@Interceptors({GenericInvocationHandler.class})
@Remote(IRouting.class)
public class RoutingServiceBean extends AbstractBasicBean implements IRouting {

    @EJB
    public ILocalInterface iLocalInterface;

}

Actually, when I use @EJB I get NPE and when I use @EJB(beanName=IRemoteInterface.JNDI_NAME) I get ClassCastException which is right JNDI name of the remote interface.

I m looking for something like @LocalBinding and @RemoteBinding in JBoss.

Maybe I'm missing something?

Best Answer

  1. If you use EJB3.0 you may use @Localbinding / @Remotebinding in JBoss. If you use EJB 3.1, the JNDI bindings are standardized (called portable global JNDI name).

  2. name attribute of the @Stateless/@Stateful annotation defines the name of the bean. It's default is the unqualified class name.

  3. mappedName attribute of the @Stateless/@Stateful annotation is used to map the bean to a JNDI name. If you provide this attribute, you need to provide the mappedName attribute of the @EJB annotation in order to reference the bean. In terms of mapping:

    @Stateless(name="Bar")       => @EJB(beanName="Bar")
    @Stateless(mappedName="Foo") => @EJB(mappedName="Foo")
    

In your example, try to use:

public class RoutingServiceBean {
    ...
    @EJB(mappedName=IRemoteInterface.JNDI_NAME)
    public ILocalInterface iLocalInterface;
}
Related Topic