您的位置:首页 > Web前端 > JavaScript

基于weblogic 的EJB 学习笔记-JSP教程,资料/其它

2014-03-03 14:25 501 查看
http://www.west263.com/www/info/14655-1.htm

ejb 学习笔记

1、ejb 基础知识

(1) 无状态会话bean

不保存客户机的会话状态

优点:使用小量的实例即可满足大量的客户。每个实例都没有标识,相互之间是等价的。

等冪的无状态会话bean: 多次和一次调用的结果和效应相同。

在集群中可以负载均衡 a 机器失败,可以在b机器上重试

非等冪的无状态会话bean: 如:计数器

不能自动因故障而进行切换。

(2) 有状态会话bean

保存客户机的会话状态

特点: 在有会话状态会话的bean例子中,出纳员的数量等于活动的顾客的数量,这可以简化编程模式

weblogic 通过内存复制技术 在集群中进行负载均衡

内存复制技术: 每个有会话状态的bean实例都将存储在两个服务器的内存中,一个服务器作为主服务器,另一个作为辅助服务器。

如果主失败,辅助变为主,然后自动选择别的可用的服务器作为辅助。

遗憾: 很难在servlet 和jsp中用好有状态会话bean。可能会发生并发现象,产生remoteexception

weblogic 的<allow-concurrent-calls> 可以封锁任何并发的调用。

同步: 可以有选择地实现 javax.ejb.sessionsynchronization接口

afeterbegin() //进入事务时

beforecompletion() //提交事务前,用于提交前把缓存的数据写到数据库中.

aftercompletion() //提交事务后,用于释放共享资源或者更新事务提交和终止方面的统计信息.

会话bean通过其 sessioncontext 对象中的 getusertransaction() 方法,取得对usertransaction的应用

通常 sessioncontext 被存放在成员变量中

** 记住是在调用ejb.create()方法前调用 setusertransaction() 方法

利用对 usertransaction 的引用会话可以使用 begin() 、commit()、rollback() 方法界定一个事务.

(3) 实体 bean:

它有一个主健作为唯一的标识符

组成部分: 由本地接口、远程接口、bean类、主健类和配置描述器组成。

本地接口:

扩展了javax.ejb.ejbhome接口,包括create()、remove()、finder 和home等方法

1)create()方法调用bean类中的ejbcreate()方法。相当于数据的insert 方法。

2)remove()方法相当于数据库的delete操作。

3)finder()方法,使客户能够查询和接收满足查询条件的实体bean的引用。每个实体bean的本地接口中都必须

有一个findbyprimarykey() 方法

4)home 方法,类似于无状态会话bean。

主健类:

实体bean必须包括一个主健类,主健类用于标识实体bean实例,而且实体bean数据类型必须是唯一的。

主健类可以是java的基本类型string integer 也可以是用户自定义的。

也可以是多个字段的主健的复合主健。

bean 类和bean的上下文环境:

实现javax.ejb.ejbobject 接口,其中包含业务方法的语法格式定义.

bean 类实现了javax.ejb.entitybean接口,同javax.ejb.sessionbean接口一样,entitybean 接口包含了ejb

容器调用bean实例的语法格式.

在bean的构造器执行之后,立即调用setentitycontext() 方法,同时把bean实例的entitycontext 传递给它.

bean类实现了home方法和远程接口中的业务方法,home方法是针对匿名实例的方法不应使用有关的主健值.

分为:

容器管理持久性(container-managerd persistence)cmp

特点: ejb 容器自动生成,用于把实体bean的数据写入到数据库中。

优点: bean作者可以避免编写实体bean与关系数据库数据访问方面的代码。cmp将自动处理这一过程。

个性: 每一个cmp 实体bean 都有一组容器管理的字段,这些字段存储在数据库,并可从中加载.通常,每个容器管理的字段都对应于

关系数据库中的一个列.

容器管理的每个字段必须在ejb-jar.xml中定义,这使容器能够把容器管理的字段与bean类中的set和get方法进行匹配比较.

另外,bean作者可以增加另外一个cmp配置描述文件 weblogic-cmp-rdbms.xml,其中包含数据库表名和每个容器管理的字

段和相应的数据列的映射.

bean管理持久性(bean-managerd persistence ) bmp

特点: 在bmp实体中,bean作者需要自己编写数据库访问代码,也就是编写jdbc代码,插入、删除和查询数据库中的实体bean数据。

优点: 可以让bean的作者完全灵活的处理实体bean的持久性数据,因为作者需要写数据访问的代码,他几乎可以使用任何持久性存

储方式ejb2.0 cmp提供实体bean之间的标准关系映射,使容器能自动管理业务对象之间的交互。

cmp拥有更多的访问控制,因此cmp比bmp有较好的性能。

(4) 消息 bean

把jms 和ejb 成功结合在一起,集成的结果

特点:客户机不需要调用消息bean 相反: 客户机只需要发一个消息给jms目的。

在消息到达以后,消息bean的onmessage()方法将被调用,以处理这个消息。

消息bean用于在服务器中执行异步操作。

2。ejb 组成

(1)远程接口

public interface helloword extents ejbobject



//ejbobject 接口方法

ejbhome getejbhome() throws remoteexception;

object getprimarykey() throws remoteexception;

void remove() throws remoteexception, removeexception;

handle gethandle() throws remoteexception;

boolean isidentical(ejbobject ejbobject) throws remoteexception;



(2)本地接口

本地接口是ejb工厂,客户机可以使用本地接口创建、找出和删除ejb实例。只需写本地接口中的方法的语法调用格式

public class helloworldhome extends ejbhome



//ejbhome 接口方法

void remove(handle handle) throws remoteexception, removeexception;

void remove(object o) throws remoteexception, removeexception;

ejbmetadata getejbmetadata() throws remoteexception;

homehandle gethomehandle() throws remoteexception;

// home

public helloworld create() throws createexception, remoteexception;



(3)bean 类

public class helloworldbean implements sessionbean



// sessionbean 中的方法

public void setsessioncontext(sessioncontext sessioncontext)

/**调用次方法会话结束*/

public void ejbremove()

//ejb通过待命和活动的机制,管理一组正在工作的有状态会话bean实例

/**活动*/

public void ejbactivate()

/**待命*/

public void ejbpassivate()

// bean类

// 每个home 的create 方法对应一个ejbcreate()方法

// 有会话状态有很多不同版本的create()方法。而create 方法必须有ejbcreate()方法与之一一对应

public void ejbcreate()



不要在ejb类中类中实现远程接口

3. ejb 配置描述器

(1)ejb-jar.xml

<ejb-jar> (注释)

<enterprise-beans>

<session>

<ejb-name>helloworld(ejbname)</ejb-name>

<home>com.dhc.helloworld.helloworldhome(本地接口类)</home>

<remote>com.dhc.helloworld.helloworld(远程接口类)</remote>

<ejb-class>com.dhc.helloworld.helloworldbean(bean类)</ejb-class>

<session-type>stateless(无状态会话)</session-type>

<transaction-type>bean(bean管理的事务)</transaction-type>

</session>

</enterprise-beans>

<container-transaction>

<method>

<ejb-name>shoppingcartejb</ejb-name>

<method-name>*(说明shoppingcartejb的默认事务属性指定为required)</method-name>

</method>

<trans-attribute>required(容器管理的事务使用的属性 nerver、notsupported

、supports、mandatory、required、requirednew)</trans-attribute>

</container-transaction>

</ejb-jar>

(2)weblogic-ejb-jar.xml (注释)

<weblogic-ejb-jar>

<weblogic-enterprise-bean>

<ejb-name>helloworld(ejb名称)</ejb-name>

<jndi-name>helloworldejb(jndi名称)</jndi-name>

<max-bean-in-freepool>10(限制不会有超过10个无状态会话bean并发运行)</max-bean-in-freepool>

<max-bean-in-cache>10(放到内存缓存中的有状态会话bean的最大数量)</max-bean-in-cache>

</weblogic-enterprise-bean>

</weblogic-ejb-jar>

4 . 建立ejb 档案文件

com/dhc/helloworld/(package)

com/dhc/helloworld/helloworld(远程接口)

com/dhc/helloworld/helloworldhome(本地接口)

com/dhc/helloworld/helloworldbean(bean类)

meta-inf

meta-inf/ejb-jar.xml(配置描述器)

meta-inf/weblogic-ejb-jar.xml(weblogic服务器配置描述器)

说明: meta-inf 必须为大写

5 . 容器管理的事务

nerver : 不参与事务,如果参与产生remoteexception

notsupported: 不能参与

supports: 如果调用者正在参与事务,相应的ejb调用也可以参与事务,否则不能

mandatory 如果调用者有一个事务,相应的ejb可以参与事务,否则,transactionrequiredexception

required 如果调用者有一个事务,相应的ejb可以参与事务,否则,容器将在调用相应的ejb之前,开始一个事务.

当方法调用完成以后,即提交该事务.

requiresnew 在调用相应的ejb之前,开始一个新的事务,当方法调用返回时,即提交这个事务.

6、ejb 引用

在ejb-jar.xml

<ejb-ref>

<description> an ejb reference to the widget ejb(描述)</description>

<ejb-ref-name>ejb/widgetejb</ejb-ref-name>

<ejb-ref-type>session</ejb-ref-type>

<home>com.dhc.widgethome</home>

<remote>com.dhc.widget</remote>

</ejb-ref>

在 weblogic-ejb-jar.xml

<ejb-reference-description>

<ejb-ref-name>ejb/widgeejb</ejb-ref-name>

<jndi-name>deployedwidge</jndi-name>

</ejb-reference-description>

程序

content ctx = new initialcontent();

object h = ctx.lookup("java:/comp/env/ejb"); //环境变量是只读的,而且是当前ejb的本地变量.

widgethome home = (widgethome)portableremoteobject.narrow(h,widgehome.class);

7. 资源管理器的引用

定义资源管理的引用

例子: 建立 jdbc、dbpool与jdbc数据源的映射

在ejb-jar.xml

<resource-ref>

<description>(描述)</description>

<res-ref-name>jdbc/bdpool</res-ref-name>

<res-type>javax.sql.datasource</res-type>

<res-auth>container</res-auth>

</resource-ref>

在 weblogic-ejb-jar.xml

<resource-description>

<res-ref-name>jdbc/dbpool</res-ref-name>

<jndi-name>dbpool</jndi-name>

</resource-description>

config.xml

<jdbctxdatasource

name="dbpool"

targets="myserver"

jddiname="dbpool" (jndi名称)

poolname ="developmentpool"

/>

引用的优点

我们用大量的映射和配置,才建立了资源管理器的引用,但是还是很值得的。

以为便于部署人员重新配置应用而不需要修改实际的bean类代码。甚至也不需要修改ejb的配置描述器

java bean 代码

content ctx = new initialcontent();

datasource datasource = (datasource)ctx.lookup("java:/comp/env/jdbc/dbpool");

8 . 句柄: 作为一个串行化的对象,句柄中封装了足够的信息,以便重建对ejbobject的引用。

句柄可用于在两个相互合作的进程中传递ejbobject的引用。接受进程即可从句柄中取得ejbobject的引用。

为了取得句柄,可以调用ejbobject接口的gethandle()方法,返回一个handle实例

为了重建ejbobject 引用。可以使用handle 接口的getejbobject()方法。

例子:

helloworld hw = home.create();

javax.ejb.handle handle = hw.gethandle();

helloworld helloworld = (helloworld)portableremoteobject.narrow(handle.getejbobject(),helloworld.class);

homehandle:

类似handle ,但不能用于引用ejbobject

homehandle 包含足够的信息,可以重建ejbhome()的引用。

差异:

是调用 gethomehandle()方法 和getejbhome()方法

例子片断:

content ctx = new initialcontext();

object h = ctx.lookup("helloworldejb");

helloworldhome home = (helloworldhome)portableremoteobject.narrow(h,helloworldhome.class);

homehandle homehandle = home.gethomehandle();

object nh = homehandle.getejbhome();

helloworldhome newhomereference = (helloworldhome)portableremoteobject.narrow(nh,helloworldhome.class);

优点:

他们可以自动的存储重建引用所需的信息

9.使用事务的技巧:

(1) 一个事务不要涉及太多的操作.

(2) 容器管理和bean管理的事务

事务既耗费应用服务器中的资源,又耗费数据库资源,所以事务越短越好.

尽量使用容器管理事务而不要采用bean管理事务的方式.

(3) ejb遇到错误,需要强制事务回滚. 使用ejbobject.setrollbackonly();

(4) 不能让事务涉及web层和表示逻辑

(5) 企业应用中不应当选用supports 事务属性,因为只有调用者开始一个事务后,ejb才能在事务中运行.


文章整理:西部数码--专业提供域名注册虚拟主机服务

http://www.west263.com
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: