enterpriseJavaBean 入门 (二)

home interface --本地接口,

定义创建的方法,查找的方法和析构的方法

 

 

package javax.ejb;

 

import java.rmi.Remote;

import java.rmi.RemoteException;

 

// Referenced classes of package javax.ejb:

//      RemoveException, EJBMetaData, HomeHandle, Handle

 

public interface EJBHome

  extends Remote

{

  public abstract EJBMetaData getEJBMetaData()

    throws RemoteException;

 

  public abstract HomeHandle getHomeHandle()

    throws RemoteException;

 

  public abstract void remove(Object obj)

    throws RemoteException, RemoveException;

 

  public abstract void remove(Handle handle)

    throws RemoteException, RemoveException;

 

}

 

 

session bean

 

package javax.ejb;

 

import java.rmi.RemoteException;

 

// Referenced classes of package javax.ejb:

//      EJBException, EnterpriseBean, SessionContext

 

public interface SessionBean

  extends EnterpriseBean

{

  public abstract void ejbActivate()

    throws EJBException, RemoteException;

 

  public abstract void ejbPassivate()

    throws EJBException, RemoteException;

 

  public abstract void ejbRemove()

    throws EJBException, RemoteException;

 

  public abstract void setSessionContext(SessionContext sessionconte

xt)

    throws EJBException, RemoteException;

 

}

 

setSessionContext(SessionContext sessioncontext)

session context 是session bean 和container交互的通道, 通常的实现:

 

import javax.ejb.*;

public class MyBean implements SessionBean

{

private SessionBean sessiontext;

public void setSessionContext(SessionContext sessioncontext)

     throws EJBException, RemoteException

    {

     this.sessiontext = sessiontext;

    }

 

    ......

}

 

 

public void ejbCreate(...)

 

至少实现一个

 

home object实现相应参数的一个create方法

比如

你的bean中有一个ejbCreate(int i)时

home object中有

public void create(int i)

 

 

钝化和激活 ---仅用于stateful session bean

  public abstract void ejbPassivate()

  public abstract void ejbActivate()

 

当太多的session bean被事例化时,container做钝化和激活操作, 释放和打开资

 

 

 

 

 

//stateless session bean

 

对于所有的客户端是相同的,所有的信息通过参数传递或从数据库等外部得到

初始化的唯一方式是无参数的 ejbCreate()方法

home object 有相应的无参数create()方法

 

 

客户端调用过程:

 

1、Look up a home object.

2、Use the home object to create an EJB object.

3、Call business methods on the EJB object.

4、Remove the EJB object.

 

Look up a home object

your client code must use the JNDI. J2EE products exploit directory se

rvices to stroe location infromation for resources that your applicati

on code uses in an enterprise deployment. These resources could be EJB

home objects, enterprise bean enviroment properties, database deriver

s, message service drivers, and other resources. By using directory se

rvices, you can writer application code that does not depend on specif

ic machine names or locations. This is all part of EJB's location tran

sparency, and it keeps your code portable. If later you decide thata r

esources should be located elsewhere, your code will not need to be re

built because the directory service can simply be updated to reflect t

he new resource locations. This greatly enhances maintenance of a mult

i-tier deployment that may evolve over time.

 

There are two common steps that must be taken to find any resource in

a J2EE deployment:

1. Associate the resource with a "nickname" in your deployment descrip

tor. Your J2EE product will bind the nickname to the resource.

2. Clients of the resource can use the nickname with JNDI to look up t

he resource across a deployment.

 

目前的主要的分布式应用框架

 

1、 Miscrosoft's Distribute interNet Appplications Architecture(DNA)

相关的平台和技术

NT

DCOM

MSMQ

MTS

Microsoft Wolfpack

Microsoft SQL Server

Microsoft Internet Information Server

Microsoft Management Console

2、SUN's J2EE

J2EE是规范而不是产品, 不至于让用户绑定到一个卖家(Microsoft)

支持高端的Unix平台

内置的CORBA支持

 

3、The Object Management Group's CORBA Standard

Common Object Request Broker Architecture (CORBA)

Internet Inter-ORB Protocol (IIOP)

 

EJB开发概述

1、EJB的开发

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

 

1.1 SessionBean的开发

第一步, 写远程接口(remote interface),

继承EJBObject接口,把需要调用的public方法写在里面(这些方法将在SessionB

ean中实现),注意要声明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和jav

ax.ejb.CreateException。

比如:

 

 

 

package jsper.ejb;

 

import java.rmi.*;

import javax.ejb.*;

 

 

public interface MyEJBHome extends EJBHome

{

 

 

 

MyEJB create() throws java.rmi.RemoteException, javax.ejb.CreateExcept

ion;

 

}

 

第三步, 写真正的Session Bean的实现(实现定义在远程接口中的方法), 需要

实现javax.ejb.SessionBean接口

注意:不能用implents MyEJB的方式直接实现远程接口,此处不用抛出RemoteExc

eption

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 enterpri

se 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 on

ly)

  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 Serve

r能理解的远程接口和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的过程中,会绑定一个名字到Co

ntainer的目录服务中,现在我们要调用时从这个目录服务中把EJBHome对象取出

, 这里分为从本地和外部两种情况:

一种是客户端本地调用EJB。 比如和EJB引擎和Servlet引擎是整合在同一个Appl

ication Server中, 这时当一个Servlet要调用EJB时无须验证,即可得到EJBHo

me接口的实现

 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.aur

ora.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_LO

GIN);

// 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("he

llo");

 

//create three element array of COUNT object

  EJB ejb =tmp.create();

  System.out.println(ejb.sayHello());

}

catch(Exception e)

{

e.printStackTrace();

}

由于和具体的目录服务、协议相关,为了达到可移植的目的,只好多做一些工作

,幸好一般不需要做这些工作。

这里讲讲如何使用jdeveloper开发EJB。在此之前请先阅读 ‘EJB开发概述.doc'

 

 

由于jDeveloper中提供了一系列的向导(写起文档很麻烦)完成各种功能,兼之

jDeveloper中的帮助说明得很详细,没有另写文档

jDeveloper的文档在

Help->help topics->user's guaides->Developing Applications->Developing

Java Components->Developing and Deploying Enterprise JavaBeans

 

使用jDeveloper3.0能够开发EJB1.0(可惜不支持EJB1.1), 并能把EJB发布到Or

acle8i(需要配置成多线/进程服务器)或Oracle Application server/iAs

 

开发EJB的方式:

 

通过向导file->new...->Enterprise java bean

1、生成一个新的EJB类框架和Remote interface, Home interface

2、把已经存在的类封装成EJB

3、把已经存在的EJB的class文件封装成EJB

 

注意:

根据jdeveloper提供的这些功能,我们可以省去写Remote interface 和Home in

terface的工作。

而直接写EJB 实现文件甚至bean文件, 但为了使开发的东西看起来干净一些,建

议不要使用包装bean的方式。

 

生成Deployment descriptor文件, jDevelper提供一个向导做这件事情, 挺简单

 

在生成EJB的向导中选择可以生成Deplyment descriptor文件, 以后双击生成的

.prf文件向Oracle8i或OAS发布。

或者:

在项目文件夹右击鼠标->New Deployment Profile...

或者:

在EJB文件夹右击鼠标->Create jServer/EJB deployment profile...或create

OAS/EJB deployment profile...

 

以后就是按照向导的提示一步步进行,不再赘述。

 

 

使用jDeveloper开发EJB的总体印象:

优点:

由于是和自己的产品(8i/OAS)整合在一起, jDeveloper开发EJB是一个好的思路

, 速度是很快的(不出错的情况下), 隐藏了Deploy的大部分细节,大大加快开

发进度。根据。

缺点:

1、8i只支持EJB的1.0规范, 版本有点低。(根据Oracle公司的iAS 白皮书, iA

S的版本2支持EJB1.1)。 而别的产品如weblogic, jrun现在支持的时EJB1.1规范

,这样在向这些平台发布时还有一定的工作量。(1.0中deploy descriper是jav

a类, 在1.1中已改为ejb-jar.xml文件)

3、在8i中没有和servlet和jsp的引擎在本地,这样在web server访问EJB时实际

上是从外部, JNDI访问需要先验证身份, 给客户端servlet程序的移植带来一定

的工作量(OAS没有这个问题)。

2、在有的机器上jDeveloper3.0的connection manager有时连接8i不能成功, 开

发无法进行。具体原因尚未查明。(在程序中能连接成功)

3、在向8i deploy时出错时定位错误困难, 提示往往只是complie failed或dep

loy failed给开发造成困难。

4、稳定性欠佳。较频繁地出现非法访问内存之类的错误

 

 

根据以上原因, 可以考虑使用支持EJB1.1规范的iAS作为将来程序开发的平台。

在别的平台做实施只需用相应平台的Deploy tools重新发布EJB.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值