本文主要是介绍JBoss7 创建客户端通过JNDI调用EJB,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
转:http://k1280000.iteye.com/blog/1654740
参考:http://wenku.baidu.com/link?url=phjFT-9Y03RtAFoqDMgKOvqdk3XdrGghYQBge4ZzC9W3t0fjkVkzJLQ0d92_KaJ5MS9mNa2yIDFNG65XmKfvoHgVAY-TyLrTLunGUIaZWve
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");
JBOSS 6,7
EJB invocations from a remote client using JNDI
官方文档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 <strong>//IP</strong>
- remote.connection.default.port = xxxx <strong>//port</strong>
- 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
这篇关于JBoss7 创建客户端通过JNDI调用EJB的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!