EJB

EJB

 

1.EJB(企业级javaBean)——是bean组件;用于实现业务的分离(进程的分布式);底层是socket机制。

 

2.EJB的分类

 2.1:SessionBean——用于问答式会话,专人负责

  2.1.1:stateless——不保存会话状态,所有客户都共用一个EJB

   ×应用——登录验证、完成单一算法

  2.1.2:stateful——有会话状态,对于每个客户都产生一个EJB

   ×实现“购物车”功能,跟踪客户的流程

 2.2:EntityBean——与数据库实体打交道

  2.2.1:CMP——简单,方便,效率低;由容器自动完成

  2.2.2:BMP——编写难,效率高;由程序员编写

 2.3:MessageDriverBean——批量信息异步处理

3.EJB发展

    3.1:通过socket来实现不同进程间的信息通讯,客户端需要服务器里的类信息——容易泄露代码

 3.2:为服务器端提供接口,将接发送给客户端,避免了代码泄露——传参复杂

 3.3:在服务器端书写引用名和类、方法对应的配置文件,只传引用名。——JNDI (Java命名与目录接口)

 3.4:RMI

 3.5:容器

 

 

4.EJB的第三方插件

 4.1:Jboss——Jboss中,客户端是端口1099,web服务是端口8080

 4.2:WebLogic

 4.3:WebSpare

5.EJB代码

 5.1:Home interface ——创建EJB实例,提供生命周期管理方法。

 

 

Java代码 

//第一步:书写继承javax.ejb.EJBHome接口的接口   

 

package com.lovo.stateless;   

 

import java.rmi.RemoteException;   

 

import javax.ejb.CreateException;   

import javax.ejb.EJBHome;//必须继承该接口   

 

public interface MyStatelessHome extends EJBHome{   

    //create方法返回远程接口类型且抛出CreateException异常   

    public MyStatelessRemote create() throws CreateException,RemoteException;   

    //所有的方法都需要跑RemoteException异常   

}  

 

//第一步:书写继承javax.ejb.EJBHome接口的接口

 

package com.lovo.stateless;

 

import java.rmi.RemoteException;

 

import javax.ejb.CreateException;

import javax.ejb.EJBHome;//必须继承该接口

 

public interface MyStatelessHome extends EJBHome{

//create方法返回远程接口类型且抛出CreateException异常

public MyStatelessRemote create() throws CreateException,RemoteException;

//所有的方法都需要跑RemoteException异常

}

 

 5.2:Remote interface——定义业务方法和辅助方法

 

 

Java代码 

//第二步:书写继承接口java.rmi.RemoteException的接口   

 

package com.lovo.stateless;   

 

import java.rmi.RemoteException;   

 

import javax.ejb.EJBObject;//必须继承该接口   

 

public interface MyStatelessRemote extends EJBObject{   

//所有的方法必须抛RemoteException   

    public String sayHelloWorld(String name) throws RemoteException;   

 

    public String sayGoodBye(String name) throws RemoteException;   

 

}  

 

//第二步:书写继承接口java.rmi.RemoteException的接口

 

package com.lovo.stateless;

 

import java.rmi.RemoteException;

 

import javax.ejb.EJBObject;//必须继承该接口

 

public interface MyStatelessRemote extends EJBObject{

//所有的方法必须抛RemoteException

public String sayHelloWorld(String name) throws RemoteException;

public String sayGoodBye(String name) throws RemoteException;

}

 

 5.3:Bean class——实现2个接口的方法和自己的业务方法

 

 

Java代码 

//第三步:书写实现接口的EJB类   

 

 

package com.lovo.stateless;   

 

import java.rmi.RemoteException;   

 

import javax.ejb.EJBException;   

import javax.ejb.SessionBean;   

//必须继承该接口,并实现回调方法:激活、钝化、移除设置会话文档   

import javax.ejb.SessionContext;   

 

/**  

 * XDoclet-based session bean. The class must be declared public according to  

 * the EJB specification.  

 *   

 * To generate the EJB related files to this EJB: - Add Standard EJB module to  

 * XDoclet project properties - Customize XDoclet configuration for your  

 * appserver - Run XDoclet  

 *   

 * Below are the xdoclet-related tags needed for this EJB.  

 *   

 * @ejb.bean name="MyStateless" display-name="Name for MyStateless"  

 *           description="Description for MyStateless"  

 *           jndi-name="ejb/MyStateless" type="Stateless" view-type="remote"  

 */  

//类必须是public的   

public class MyStatelessBean implements SessionBean {   

 

    /** The session context */  

    private SessionContext context;   

//必须有一个无参构造方法   

    public MyStatelessBean() {   

        // TODO Auto-generated constructor stub   

    }   

 

    // 创建——根据主接口中create方法的参数和个数创建EJB   

    public void ejbCreate() throws EJBException, RemoteException {   

 

    }   

 

    // 激活——将代码从硬盘中提取到硬盘。   

    public void ejbActivate() throws EJBException, RemoteException {   

        // TODO Auto-generated method stub   

 

    }   

 

    // 钝化——将长时间不用的代码从内存中移动到EJB所在的硬盘。   

    public void ejbPassivate() throws EJBException, RemoteException {   

        // TODO Auto-generated method stub   

 

    }   

 

    // 移除——清楚EJB的所有信息。   

    public void ejbRemove() throws EJBException, RemoteException {   

        // TODO Auto-generated method stub   

 

    }   

 

    /**  

     * Set the associated session context. The container calls this method after  

     * the instance creation.  

     *   

     * The enterprise bean instance should store the reference to the context  

     * object in an instance variable.  

     *   

     * This method is called with no transaction context.  

     *   

     * @throws EJBException  

     *             Thrown if method fails due to system-level error.  

     */  

 

    public void setSessionContext(SessionContext newContext)   

            throws EJBException {   

        context = newContext;   

    }   

//  必须实现远程接口中定义的每个方法   

    public String sayHelloWorld(String name) throws RemoteException {   

        return name + "说:大家好,我是清晨!";   

    }   

 

    public String sayGoodBye(String name) throws RemoteException{   

        return name + "说:再见了,飞扬的青春!";   

    }   

 

}  

 

//第三步:书写实现接口的EJB类

 

 

package com.lovo.stateless;

 

import java.rmi.RemoteException;

 

import javax.ejb.EJBException;

import javax.ejb.SessionBean;

//必须继承该接口,并实现回调方法:激活、钝化、移除设置会话文档

import javax.ejb.SessionContext;

 

/**

 * XDoclet-based session bean. The class must be declared public according to

 * the EJB specification.

 * 

 * To generate the EJB related files to this EJB: - Add Standard EJB module to

 * XDoclet project properties - Customize XDoclet configuration for your

 * appserver - Run XDoclet

 * 

 * Below are the xdoclet-related tags needed for this EJB.

 * 

 * @ejb.bean name="MyStateless" display-name="Name for MyStateless"

 *           description="Description for MyStateless"

 *           jndi-name="ejb/MyStateless" type="Stateless" view-type="remote"

 */

//类必须是public的

public class MyStatelessBean implements SessionBean {

 

/** The session context */

private SessionContext context;

//必须有一个无参构造方法

public MyStatelessBean() {

// TODO Auto-generated constructor stub

}

 

// 创建——根据主接口中create方法的参数和个数创建EJB

public void ejbCreate() throws EJBException, RemoteException {

 

}

 

// 激活——将代码从硬盘中提取到硬盘。

public void ejbActivate() throws EJBException, RemoteException {

// TODO Auto-generated method stub

 

}

 

// 钝化——将长时间不用的代码从内存中移动到EJB所在的硬盘。

public void ejbPassivate() throws EJBException, RemoteException {

// TODO Auto-generated method stub

 

}

 

// 移除——清楚EJB的所有信息。

public void ejbRemove() throws EJBException, RemoteException {

// TODO Auto-generated method stub

 

}

 

/**

* Set the associated session context. The container calls this method after

* the instance creation.

* The enterprise bean instance should store the reference to the context

* object in an instance variable.

* This method is called with no transaction context.

* @throws EJBException

*             Thrown if method fails due to system-level error.

*/

 

public void setSessionContext(SessionContext newContext)

throws EJBException {

context = newContext;

}

// 必须实现远程接口中定义的每个方法

public String sayHelloWorld(String name) throws RemoteException {

return name + "说:大家好,我是清晨!";

}

 

public String sayGoodBye(String name) throws RemoteException{

return name + "说:再见了,飞扬的青春!";

}

 

}

 

 5.4:配置文件

 

Ejb-jar.xml代码 

<?xml version="1.0" encoding="UTF-8"?>   

<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd" >   

<ejb-jar>   

  <enterprise-beans>   

 

  <!-- 会话bean用session标签;实体bean用entity标签;消息驱动bean用message-driven标签 -->   

 

    <session>   

        <ejb-name>test67stateless</ejb-name><!-- EJB对外的引用 -->   

        <home>com.lovo.stateless.MyStatelessHome</home><!--指明EJB的主接口类  -->   

        <remote>com.lovo.stateless.MyStatelessRemote</remote><!--指明EJB的远端接口类  -->   

        <ejb-class>com.lovo.stateless.MyStatelessBean</ejb-class><!--指明EJB的实现类  -->   

        <session-type>Stateless</session-type><!--指明session类型  -->   

        <transaction-type>Container</transaction-type><!--  -->   

    </session>   

 

    <session>   

        <ejb-name>test67stateful</ejb-name>   

        <home>com.lovo.stateful.MyStatefulHome</home>   

        <remote>com.lovo.stateful.MyStatefulRemote</remote>   

        <ejb-class>com.lovo.stateful.MyStatefulBean</ejb-class>   

        <session-type>Stateful</session-type>   

        <transaction-type>Container</transaction-type>   

    </session>   

  </enterprise-beans>   

</ejb-jar>   

 

 

 

 

<!-- 第四步:书写服务器的配置文件 -->  

Files contained in javax.ejb.jar: META-INF/MANIFEST.MF javax.ejb.AccessLocalException.class javax.ejb.AccessTimeout.class javax.ejb.ActivationConfigProperty.class javax.ejb.AfterBegin.class javax.ejb.AfterCompletion.class javax.ejb.ApplicationException.class javax.ejb.Asynchronous.class javax.ejb.AsyncResult.class javax.ejb.BeforeCompletion.class javax.ejb.ConcurrencyManagement.class javax.ejb.ConcurrencyManagementType.class javax.ejb.ConcurrentAccessException.class javax.ejb.ConcurrentAccessTimeoutException.class javax.ejb.CreateException.class javax.ejb.DependsOn.class javax.ejb.DuplicateKeyException.class javax.ejb.EJB.class javax.ejb.EJBAccessException.class javax.ejb.EJBContext.class javax.ejb.EJBException.class javax.ejb.EJBHome.class javax.ejb.EJBLocalHome.class javax.ejb.EJBLocalObject.class javax.ejb.EJBMetaData.class javax.ejb.EJBObject.class javax.ejb.EJBs.class javax.ejb.EJBTransactionRequiredException.class javax.ejb.EJBTransactionRolledbackException.class javax.ejb.embeddable.EJBContainer.class javax.ejb.EnterpriseBean.class javax.ejb.EntityBean.class javax.ejb.EntityContext.class javax.ejb.FinderException.class javax.ejb.Handle.class javax.ejb.HomeHandle.class javax.ejb.IllegalLoopbackException.class javax.ejb.Init.class javax.ejb.Local.class javax.ejb.LocalBean.class javax.ejb.LocalHome.class javax.ejb.Lock.class javax.ejb.LockType.class javax.ejb.MessageDriven.class javax.ejb.MessageDrivenBean.class javax.ejb.MessageDrivenContext.class javax.ejb.NoMoreTimeoutsException.class javax.ejb.NoSuchEJBException.class javax.ejb.NoSuchEntityException.class javax.ejb.NoSuchObjectLocalException.class javax.ejb.ObjectNotFoundException.class javax.ejb.PostActivate.class javax.ejb.PrePassivate.class javax.ejb.Remote.class javax.ejb.RemoteHome.class javax.ejb.Remove.class javax.ejb.RemoveException.class javax.ejb.Schedule.class javax.ejb.ScheduleExpression.class javax.ejb.Schedules.class javax.ejb.SessionBean.class javax.ejb.SessionContext.class javax.ejb.Session
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值