JBOSS 6,7调用通过JNDI查找EJB的方法和JBOSS5不一样。
JBOSS 5
Properties props = new Properties(); props.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory"); //jndi factory props.setProperty("java.naming.provider.url", "localhost:1099"); //jndi server url props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming"); //jndi finding package InitialContext ctx = new InitialContext (props); DBBeanRemote db = (DBBeanRemote) ctx.lookup("DBBean/remote");
官方文档https://docs.jboss.org/author/display/AS71/EJB+invocations+from+a+remote+client+using+JNDI
总结:
1. 查找JNDI
final Hashtable jndiProperties = new Hashtable(); jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");//让JNDI API知道是由谁来管理我们用来查找JNDI 名字的命名空间的。 final Context context = new InitialContext(jndiProperties); //appName 和 moduleName分别就打包的格式而定 //如果是.ear就是appName,其它的是moduleName(.jar,.war) final String appName = ""; final String moduleName = "EJBDBTest"; final String distinctName = ""; //实现类名 final String beanName = DB.class.getSimpleName(); System.out.println(beanName); //接口类名 final String viewClassName = DBRemote.class.getName(); System.out.println(viewClassName); String jndi = "ejb:" + appName + "/" + moduleName + "/" + distinctName + "/" + beanName + "!" + viewClassName; System.out.println(jndi); DBRemote db = (DBRemote) context.lookup(jndi);
In AS7, for remote access to EJBs, you use the ejb: namespace with the following syntax:
For stateless beans: ejb:<app-name>/<module-name>/<distinct-name>/<bean-name>!<fully-qualified-classname-of-the-remote-interface> For stateful beans: ejb:<app-name>/<module-name>/<distinct-name>/<bean-name>!<fully-qualified-classname-of-the-remote-interface>?stateful
2.加入JAR文件
把jboss-client jar加入到项目,在 JBOSS_HOME/bin/client/jboss-client-7.1.0.Final.jar 目录下.
3. 创建客户端调环境(告诉客户端应该去哪里,怎么调server的EJB)
在project path 下创建 jboss-ejb-client.properties
endpoint.name=client-endpoint remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false remote.connections=default //connection 的名字 remote.connection.default.host=xx.xxx.xxx.xx //IP remote.connection.default.port = xxxx //port remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false //JBOSS 用户名密码 remote.connection.default.username=appuser remote.connection.default.password=apppassword
或者你可以另外命名这个文件的名字,只要加入系统参数里就好,如下
-Djboss.ejb.client.properties.file.path=/home/me/my-client/custom-jboss-ejb-client.properties
你还可以建立不同的连接
remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false
remote.connections=one, two remote.connection.one.host=localhost remote.connection.one.port=6999 remote.connection.one.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false remote.connection.two.host=localhost remote.connection.two.port=7999 remote.connection.two.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false