java 本地和远程无状态 bean 的 jndi 绑定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16924402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jndi binding for local and remote stateless bean
提问by R Vive L OL
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 @EJBbut it returns null or ClassCastException(java.lang.ClassCastException: com.sun.proxy.$Proxy58 cannot be cast).
我正在尝试使用远程和本地接口实现 EJB3 无状态,问题是在其他远程 EJB 中使用注释调用本地接口,@EJB但它返回 null 或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.
要在服务器上执行查找以获得本地无状态,我必须为无状态放置 2 个 JNDI 名称,否则它会给我远程名称。
@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 @EJBI get NPEand when I use @EJB(beanName=IRemoteInterface.JNDI_NAME)I get ClassCastExceptionwhich is right JNDI name of the remote interface.
实际上,当我使用时@EJB我得到NPE,当我使用时@EJB(beanName=IRemoteInterface.JNDI_NAME)我得到ClassCastException远程接口的正确 JNDI 名称。
I m looking for something like @LocalBindingand @RemoteBindingin JBoss.
我在 JBoss 中寻找类似@LocalBinding和的东西@RemoteBinding。
Maybe I'm missing something?
也许我错过了什么?
回答by Simon Zambrovski
If you use EJB3.0 you may use
@Localbinding/@Remotebindingin JBoss. If you use EJB 3.1, the JNDI bindings are standardized (called portable global JNDI name).nameattribute of the@Stateless/@Statefulannotation defines the name of the bean. It's default is the unqualified class name.mappedNameattribute of the@Stateless/@Statefulannotation is used to map the bean to a JNDI name. If you provide this attribute, you need to provide themappedNameattribute of the@EJBannotation in order to reference the bean. In terms of mapping:@Stateless(name="Bar") => @EJB(beanName="Bar") @Stateless(mappedName="Foo") => @EJB(mappedName="Foo")
如果您使用 EJB3.0,您可以在 JBoss 中使用
@Localbinding/@Remotebinding。如果您使用 EJB 3.1,则 JNDI 绑定是标准化的(称为可移植全局 JNDI 名称)。name@Stateless/@Stateful注释的属性定义了 bean 的名称。它的默认值是不合格的类名。mappedName@Stateless/@Stateful注释的属性用于将 bean 映射到 JNDI 名称。如果提供此属性,则需要提供注解的mappedName属性@EJB才能引用bean。在映射方面:@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;
}
回答by Peter Tarlos
If you're using JBOSS, you can specifiy the JNDI name of both the Local and Remote interfaces with annotations.
如果您使用 JBOSS,您可以使用注释指定本地和远程接口的 JNDI 名称。
@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.
请注意,远程 JNDI 名称可以使用无状态或远程绑定注释来定义。RemoteBinding 和 LocalBinding 注释是 JBOSS 特定的,可以在 jboss-ejb3-ext-api.jar 中找到。

