Reputation: 177
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?
Upvotes: 4
Views: 20606
Reputation: 756
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).
name
attribute of the @Stateless
/@Stateful
annotation defines the name of the bean. It's default is the unqualified class name.
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;
}
Upvotes: 2
Reputation: 901
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.
Upvotes: 0