3.2.10. 远程调用的修改
3.2.10.1. 将进行远程调用的 JBoss EAP 5 应用程序移植到 JBoss EAP 6
在 JBoss EAP 5 里,EJB 远程接口绑定在 JNDI 里,在默认情况下,本地接口是 "ejbName/local",而远程接口是 "ejbName/remote"。客户应用程序可以用 "ejbName/remote" 来查找 Bean。
ejb:BEAN_REFERENCE 语法来远程访问 EJB。
ejb:BEAN_REFERENCE 语法是:
ejb:<app-name>/<module-name>/<distinct-name>/<bean-name>!<fully-qualified-classname-of-the-remote-interface>对于 stateful bean,
ejb:BEAN_REFERENCE 语法是:
ejb:<app-name>/<module-name>/<distinct-name>/<bean-name>!<fully-qualified-classname-of-the-remote-interface>?stateful
<app-name>- 部署的 EJB 的应用程序名称。这通常是 EJB 部署的 EAR 名称,不带 .ear 后缀,但可以在 application.xml 里进行覆盖。如果应用程序没有部署为 .EAR,这个值是一个空字符串。我们假设这个例子没有部署为 EAR。<module-name>- 服务器上部署的 EJB 名称。这通常是 EJB 部署的 JAR 名称,不带 .jar 后缀,但可以在 ejb-jar.xml 里进行覆盖。在这个例子里,假设 EJB 是部署在 jboss-ejb-remote-app.jar 里,所以其模块名是 jboss-ejb-remote-app。<distinct-name>- EJB 的一个可选的不同名称。这个例子没有使用不同名称,所以它使用空字符串。<bean-name>- 默认是 bean 实现类的简单名称。<fully-qualified-classname-of-the-remote-interface>- the remote view fully qualified class name.
假设您已经将下列 stateless EJB 部署到了 JBoss EAP 6 服务器。请注意,它开放了 bean 的一个远程视图。
@Stateless
@Remote(RemoteCalculator.class)
public class CalculatorBean implements RemoteCalculator {
@Override
public int add(int a, int b) {
return a + b;
}
@Override
public int subtract(int a, int b) {
return a - b;
}
}
在 JBoss EAP 5 里,使用上面的信息,客户 EJB 查找和调用的代码类似于:
InitialContext ctx = new InitialContext();
RemoteCalculator calculator = (RemoteCalculator) ctx.lookup("CalculatorBean/remote");
int a = 204;
int b = 340;
int sum = calculator.add(a, b);
在 JBoss EAP 6 里,使用上面的信息,客户查找和调用的代码类似于:
final Hashtable jndiProperties = new Hashtable();
jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
final Context context = new InitialContext(jndiProperties);
final String appName = "";
final String moduleName = "jboss-ejb-remote-app";
final String distinctName = "";
final String beanName = CalculatorBean.class.getSimpleName();
final String viewClassName = RemoteCalculator.class.getName();
final RemoteCalculator statelessRemoteCalculator = (RemoteCalculator) context.lookup("ejb:" + appName + "/" + moduleName + "/" + distinctName + "/" + beanName + "!" + viewClassName);
int a = 204;
int b = 340;
int sum = statelessRemoteCalculator.add(a, b);
如果您的客户应用程序访问 stateful EJB,您必须在上下文查找后面附加 “?stateful”:
final RemoteCalculator statefulRemoteCalculator = (RemoteCalculator) context.lookup("ejb:" + appName + "/" + moduleName + "/" + distinctName + "/" + beanName + "!" + viewClassName + "?stateful")

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.