Jboss调用ejb3
Jboss6及以下版本
数据源,ejb发布到
E:\jboss-6.0.0.Final\server\default\deploy下
调用jndi调用ejb
Properties props = newProperties();
props.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");
props.setProperty("java.naming.provider.url", "localhost:1099");
InitialContextctx;
ctx = newInitialContext(props);
UserDao s = (UserDao) ctx.lookup("UserDaoBean/remote");
User temp=newUser();
temp=s.getPerson(pp);
jboss7通过jndi调用ejb
ejb图样
try {
//UserDao是接口类,UserDaoBean是实现类,User是实体类
UserDao dao = null;
Hashtable<String,String>jndiProperties = new Hashtable<String, String>();
jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
Context context;
context= newInitialContext(jndiProperties);
//appName和moduleName分别就打包的格式而定
//如果是.ear就是appName,其它的是moduleName(.jar,.war)
final String appName = "";
final String moduleName = "EJBTest";//EJB的名字,如果ejb和项目一期就写项目名称
final String distinctName = "";
Object obj = context.lookup("ejb:" + appName + "/" + moduleName+"/"
+ distinctName + "/UserDaoBean!com.ejb.dao.UserDao");//实现类名+“!”+接口类全名(包括路径)
dao= (UserDao) obj;
User temp=dao.getPerson("1206100168");
System.out.println(temp.getName());
}
catch
(NamingException e) {
e.printStackTrace();
}
还有就是要先建jboss-ejb-client.properties文件 并把它放到src目录下
jboss-ejb-client.properties 内容如下
remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false
remote.connections=default
remote.connection.default.host=localhost
remote.connection.default.port =
4447
remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false