4

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?

R Vive L OL
  • 177
  • 3
  • 3
  • 10

2 Answers2

2
  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;
}
Simon Zambrovski
  • 746
  • 6
  • 18
  • Hello Simon, thanks for your answer, but the problem is that you can't cast a remote to a local, what i want is to use both local and remote beans, i think that applications servers are not enaught intelligent to use the local bean if the call is in the same JVM and a remote otherwise. I found a bad solution is to not fix the name of the beans, and reconver them from the jndi of the AS and then use these names to call remote or local bean. – R Vive L OL Jul 02 '13 at 16:58
0

If you're using JBOSS, you can specifiy the JNDI name of both the Local and Remote interfaces with annotations.

@Stateless(mappedName=IRemoteInterface.JNDI_NAME, description="...")
@LocalBinding(jndiBinding = ILocalInterface.JNDI_NAME)
@Local(ILocalInterface.class)
@Remote(IRemoteInterface.class)
public class MystatelessBean extends AbstractBasicBean implements IRemoteInterface, ILocalInterface{
...
}

OR

@Stateless()
@LocalBinding(jndiBinding = ILocalInterface.JNDI_NAME)
@RemoteBinding(jndiBinding = IRemoteInterface.JNDI_NAME)
@Local(ILocalInterface.class)
@Remote(IRemoteInterface.class)
public class MystatelessBean extends AbstractBasicBean implements IRemoteInterface, ILocalInterface{
...
}

Note that the remote JNDI name can be defined with either the Stateless or RemoteBinding annotation. The RemoteBinding and LocalBinding annotations are JBOSS specific and can be found in jboss-ejb3-ext-api.jar.

Peter Tarlos
  • 731
  • 10
  • 13
  • Since JBoss AS 7 (or enterprise JBoss EAP 6), the binding of EJBs to custom JNDI names isn't allowed anymore either by [annotations](https://developer.jboss.org/thread/176194?tstart=0) or [XML configuration](https://developer.jboss.org/thread/177380?tstart=0). Therefore, your reference to both `@LocalBinding` and `@RemoteBinding` should be revised. – António Ribeiro Mar 24 '16 at 13:00