EJB的入门教材
第五步,将你的所有文件用jar工具生成jar文件
ejb-jar.xml须在优异的META-INF子目录
这句话比较咬嘴, 举个例子
在生成.jar文件时mylib----META-INF--*.XML
|
|com--coucouniu--ejb---EJBClass
|-EJBHome
|-EJB
sh>cd mylib //注意此处所在目录
sh>jar cv0f myejb.jar *
请注意: 到这一步我们做出的东西都是和和特定的EJB Server是无关的, 只是和遵循EJB的标准有关
第六步,使用特定平台的发布工具生成发布使用的jar文件。
不同的中间件产品此步骤非常不同, 产生的结果都是生成只有自己的EJB Server能理解的远程接口和Home接口实现等等东西,打包在一个jar文件中 ,一般是很简单的 .
第七步,把.jar文件发布到EJB Server
根据不同的中间件产品此步骤非常不同, 可以分为启动时发布和运行时发布两种,一般是很简单的, 以weblogic为例:
1、在weblogic.properties 文件中配置使weblogic 启动时自动装载。
添加一个条目比如:
weblogic.ejb.deploy=C:/weblogic510/myserver/ejb_basic_beanManaged.jar,\
C:/weblogic510/myserver/ejb_basic_test.jar
2、使用deploy或DeployerTool动态装载/卸载/更新
第八步,写客户端的程序(我迄今为止的理解)
在我们使用发布工具把EJB发布到EJB Container的过程中,会绑定一个名字到Container的目录服务中,现在我们要调用时从这个目录服务中把EJBHome对象取出, 这里分为从本地和外部两种情况:
一种是客户端本地调用EJB。 比如和EJB引擎和Servlet引擎是整合在同一个Application Server中, 这时当一个Servlet要调用EJB时无须验证,即可得到EJBHome接口的实现
Context ic = new InitialContext();
System.out.println("Looking for the EJB published as ’hello’");
com.jsper.ejb.MyEJBHome homeInterface =
(com.jsper.ejb.MyEJBHome) ic.lookup(“hello”); //发布时绑定的名字是hello
这样就可从目录服务中得到Home接口的实现, 也是我们最常用的方式, 可移植性很好 ,外部调用的话首先要经过身份验证, 比如Oracle8i :
再比如weblogic的调用方式:String ejbUrl = "sess_iiop://localhost:2481:ORCL/test/MyEJB";
String username = "scott";
String password = "tiger";
![]()
// Setup the environment
Hashtable environment = new Hashtable();
// Tell JNDI to speak sess_iiop
environment.put(javax.naming.Context.URL_PKG_PREFIXES, "oracle.aurora.jndi");
// Tell sess_iiop who the user is
environment.put(Context.SECURITY_PRINCIPAL, username);
// Tell sess_iiop what the password is
environment.put(Context.SECURITY_CREDENTIALS, password);
// Tell sess_iiop to use credential authentication
environment.put(Context.SECURITY_AUTHENTICATION, ServiceCtx.NON_SSL_LOGIN);
// Lookup the URL
com.jsper.ejb.MyEJBHome homeInterface = null;
try ...{
System.out.println("Creating an initial context");
Context ic = new InitialContext(environment);
System.out.println("Looking for the EJB published as ’test/MyEJB’");
homeInterface = (com.jsper.ejb.MyEJBHome) ic.lookup(ejbUrl);
}
catch (ActivationException e) ...{
System.out.println("Unable to activate : " + e.getMessage());
e.printStackTrace();
System.exit(1);
}
由于和具体的目录服务、协议相关,为了达到可移植的目的,只好多做一些工作,幸好一般不需要做这些工作。try
...{
// Get an InitialContext
String url="t3://localhost:7001";
Properties h = new Properties();
h.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
h.put(Context.PROVIDER_URL, url);
![]()
Context ctx = new InitialContext(h);
![]()
System.out.println("Getting the EJBHome object…");
com.jsper.ejb.EJBHome tmp= (com.jsper.ejb.EJBHome)ctx.lookup("hello");
![]()
//create three element array of COUNT object
EJB ejb =tmp.create();
System.out.println(ejb.sayHello());
}
catch(Exception e)
...{
e.printStackTrace();
}
0
相关文章
