服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

ejb的入门教材


  1、ejb的开发

  先泛泛而论,讲一讲ejb的开发步骤。

  1.1 sessionbean的开发

  第一步,写远程接口(remote interface),继承ejbobject接口,把需要调用的public方法写在里面(这些方法将在sessionbean中实现),注意要声明throws
java.rmi.remoteexception。 

  例如:

package
jsper.ejb;

import java.rmi.*;
import javax.ejb.*;

public
interface myejb extends ejbobject 
{

public string
sayhello() throws java.rmi.remoteexception;

}

  第二步,

  写home接口(生成ejbobject引用的factory)  至少生成一个create方法, 注意要声明throws java.rmi.remoteexception和javax.ejb.createexception。

  比如:

package
jsper.ejb;

import java.rmi.*;
import
javax.ejb.*;


public interface myejbhome extends
ejbhome 
{



myejb create() throws
java.rmi.remoteexception, javax.ejb.createexception;

}

  第三步,

  写真正的session bean的实现(实现定义在远程接口中的方法), 需要实现javax.ejb.sessionbean接口

  注意:不能用implents

  myejb的方式直接实现远程接口,此处不用抛出remoteexception package jsper.ejb;

import
java.rmi.remoteexception;
import javax.ejb.*;
public class
myejbclass implements sessionbean {

 
 public myejbclass()
{
 }
 //定义在sessionbean 中的方法 
 public void ejbcreate() throws
remoteexception, createexception {
 }

 public void ejbactivate()
throws remoteexception {
 }

 public void ejbpassivate() throws
remoteexception {
 }

 public void ejbremove() throws
remoteexception {
 }

public void
setsessioncontext(sessioncontext ctx) 
throws remoteexception
{

 }

 //此处是具体的实现

 public string
sayhello()

 {

  system.out.println("hello");
 }

  第四步,写一个发布用的配置文件ejb-jar.xml 需要提供的信息:

bean
home name -- the nickname that clients use to lookup your bean’s home
object.
enterprise bean class name -- the fully qualified name of the
enterprise bean class.
  home interface class name
  remote
interface class name
re-entrant -- whether the enterprise bean allow
re-entrant calls. this setting must be false for session beans(it applies
to entity beans only)
  stateful or stateless 
  session
timeout -- the length of time (in seconds) before a client should time out
when calling methods on your
bean.

  最后你还可以提供属于自己的配置信息供自己控制ejb的工作方式。

  例子:    

helloejb
com.jsper.ejb.myejbhome
com.jsper.ejb.myejb
com.jsper.ejb.myejbclass
stateless
container

  第五步,将你的所有文件用jar工具生成jar文件

  ejb-jar.xml须在顶级的meta-inf子目录

  这句话比较咬嘴, 举个例子

mylib----meta-inf--*.xml

|com--coucouniu--ejb---ejbclass
   |-ejbhome
   |-ejb
   
在生成.jar文件时

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 :

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);
  }
再比如weblogic的调用方式:
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();
}
由于和具体的目录服务、协议相关,为了达到可移植的目的,
只好多做一些工作,幸好一般不需要做这些工作。

/*

扫描关注微信公众号