EJB服务开发完毕后,可以打包为*.jar,并部署到应用服务器,如JBoss AS 7或WildFlly AS 10,供EJB客户端程序调用。
EJB客户端程序是调用服务器上部署的EJB方法的应用。从运行环境的不同,可以将EJB客户端程序分为两大类:
- 独立应用的客户端(如单元测试程序)
- 部署到JBoss AS7 server的客户端(客户端服务器上的程序调用另一个EJB服务器上的EJB方法)
本文主要讨论独立应用作为EJB客户端的调用形式。第二种方式将另有专术。
从编程实现的不同,可以将EJB客户端分为两大类:
- 使用JBoss specific EJB client API
JBoss/WildFly服务器专有的访问API,调用远程EJB无需JNDI API,本文不讨论。
- 使用JNDI to lookup a proxy for EJB and invoke on that returned proxy
这是最通用的访问远程EJB的做法。首先通过JDNI查找EJB的代理,并通过代理调用远程EJB的方法。
JNDI调用远程EJB的具体实现方法如下:
1. 无论采用哪种方法,由于JBoss AS 7/WildFly AS 8-10都进行安全检查,所以首先要在EJB服务器上设置有效的用户/密码,如通过执行add-user.sh脚本等,此处略。
2. 此外,无论采用哪种方法,都需要在EJB客户端程序的执行环境classpath中有jboss-ejb-client.properties文件,必须将jboss-ejb-client.properties文件加入到classpath中,客户端才能够成功调用远程EJB的方法。即时jboss-ejb-client.properties文件的内容为空,也必须有。
调用部署在JBoss AS 7中的EJB方法时,jboss-ejb-client.properties文件的内容如下:
remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false
remote.connections=default
remote.connection.default.host=10.20.30.40
remote.connection.default.port = 4447
remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false
remote.connection.default.username=appuser
remote.connection.default.password=apppassword
调用部署在WildFly AS 7中的EJB方法时,jboss-ejb-client.properties文件的内容如下:
endpoint.name=client-endpoint
remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false
remote.connections=default
remote.connection.default.host=10.20.30.40
remote.connection.default.port=8080
remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false
remote.connection.default.username=appuser
remote.connection.default.password=apppassword
3. 将%WildFly%/bin/jboss-client.jar文件复制到Eclipse项目中,并添加到classpath
4. 设置JNDI上下文参数
JNDI上下文的参数与使用的远程JNDI类型相关。具体可以分为http-remoting方式和ejb方式2种。
举例说明如下:
http-remoting方式
props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
props.put(Context.PROVIDER_URL, "http-remoting://localhost:8080");
props.put("jboss.naming.client.ejb.context", true);
String fullEJBName = "CustomView/ToolsCommonEJB//ToolsCommonBean!com.jdsu.netcomplete.common.framework.toolscommon.ToolsCommonRemote";
等价于
ejb方式
props.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
String fullEJBName = "ejb:CustomView/ToolsCommonEJB//ToolsCommonBean!com.jdsu.netcomplete.common.framework.toolscommon.ToolsCommonRemote";
至此完成EJB客户端的开发,详细代码如下:
public class ToolsCommonEjbTest {
private static IToolsCommon remote;
@Before
public void setUp() throws Exception
{
Properties props = new Properties();
props.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
Context ic = new InitialContext(props);
String fullEJBName = "ejb:CustomView/ToolsCommonEJB//ToolsCommonBean!com.jdsu.netcomplete.common.framework.toolscommon.ToolsCommonRemote";
remote = (IToolsCommon) ic.lookup( fullEJBName );
}
@Test
public void sendMessage() {
...
try {
remote.sendAlertMessage(userList, "Hi");
} catch( Exception e ) {
e.printStackTrace();
fail( "Message not Sent" );
}
}
...
}
本文详细介绍了EJB客户端如何通过JNDI查找并调用远程EJB的方法,包括设置有效的用户/密码,配置jboss-ejb-client.properties文件,以及针对JBoss AS 7和WildFly AS 10的不同配置。文中强调了无论使用哪种方法,该文件都必须在classpath中,即使是空的。

633

被折叠的 条评论
为什么被折叠?



