精通ejb【三】(转载)

类别:Java 点击:0 评论:0 推荐:

转载自:http://www.java-cn.com/technology/technology_detail.jsp?id=783

无状态会话bean基础
无状态会话bean是可以模仿业务过程的组件,它可以在单独的方法调用中被执行。Stateless Session Bean不能够维持一个调用客户的状态,在一个方法调用中,Stateless Session Bean 可以维持调用客户的状态,当方法执行完,状态不会被保持。在调用完成后,Stateless Session Bean被立即释放到缓冲池中,所以Stateless Session Bean具有很好的伸缩性,可以支持大量用户的调用。
无状态会话beans的特点
没有对话状态
无状态会话bean可以拥有内部状态,它们的状态不能为特殊的客户端定制。这意味着所有的无状态bean对于客户端是无差别的,客户端也不能分离它们。客户端必须将所有的必需的客户端数据作为业务逻辑方法的参数传给无状态bean,无状态bean可以从外部资源(例如数据库)获得所需的数据。
初始化无状态bean只有一种方法
我们知道会话bean的初始化调用ejbCreate()方法,因为无状态会话bean不能够在方法调用之间保留状态,因此它也不能在客户端给ejbCreate()调用传递数据以后保留状态。调用不带参数的ejbCreate()或create()。
容器可以聚集和重用无状态会话Bean
构建“Hello,World!”远程接口
package com.wiley.compBooks.roman.session.helloworld;
import javax.ejb.*;
import java.rmi.RemoteException;
import java.rmi.Remote;
/**
* This is the HelloBean remote interface.
*
* This interface is what clients operate on when
* they interact with EJB objects. The container
* vendor will implement this interface; the
* implemented object is the EJB object, which
* delegates invocations to the actual bean.
*/
public interface Hello extends EJBObject {
/**
* The one method - hello - returns a greeting to the client.
*/
public String hello() throws java.rmi.RemoteException;
}
Source 4.1 Hello.java.
Hello接口继承了EJBObject接口,EJBObject继承Remote接口,因此hello可以抛出rmi异常。
下面建立bean,实现业务方法:hello()。
他实现了javax.ejb.SessionBean接口
package com.wiley.compBooks.roman.session.helloworld;
import javax.ejb.*;
/**
* Demonstration stateless session bean.
*/
public class HelloBean implements SessionBean {
//
// EJB-required methods
//
public void ejbCreate() {
System.out.println("ejbCreate()");
}
public void ejbRemove() {
System.out.println("ejbRemove()");
}
public void ejbActivate() {
System.out.println("ejbActivate()");
}
public void ejbPassivate() {
System.out.println("ejbPassivate()");
}
public void setSessionContext(SessionContext ctx) {
System.out.println("setSessionContext()");
}
//
// Business methods
//
public String hello() {
System.out.println("hello()");
return "Hello, World!";
}
}
Source 4.2 HelloBean.java
注意:不需要实现自己的远程接口,初始化方法不带参数。破坏bean时,使用比较简单的ejbRemove()方法。ejbActivate() 和ejbPassivate()方法不需应用在无状态会话bean,因此,这两个方法为空。
建立“Hello,World!”Home接口
Home接口继承了javax.ejb.EJBHome。Home接口为EJB对象扩展了一个不带参数的方法??create()方法。
package com.wiley.compBooks.roman.session.helloworld;
import javax.ejb.*;
import java.rmi.RemoteException;
/**
* This is the home interface for HelloBean. This interface
* is implemented by the EJB Server´s glue-code tools - the
* implemented object is called the Home Object and serves
* as a factory for EJB Objects.
*
* One create() method is in this Home Interface, which
* corresponds to the ejbCreate() method in HelloBean.
*/
public interface HelloHome extends EJBHome {
/*
* This method creates the EJB Object.
*
* @return The newly created EJB Object.
*/
Hello create() throws RemoteException, CreateException;
}
creat方法抛出了a java.rmi.RemoteException和aavax.ejb.CreateException.异常。
写配置描述符
在EJB1.0中,配置描述符是作为文件存储在磁盘上的java对象。在EJB1.1种,配置描述符是一个XML文档。EJB容器或IDE环境应该提供生成配置描述符的工具。
配置描述符的设置
bean home的名字
企业级bean类名
home接口类名
远程接口类名
Re-entrant
状态或无状态
会话时间



HelloBean的配置描述符
环境属性
bean通过使用此信息来适应不同的特殊环境。
Ejb-jar文件
我们需要将我们所需要的文件打包成Ejb-jar文件。
企业级的bean
远程接口
home接口
配置描述符,包括属性
以上这些必须被包含进Ejb-jar文件。在EJB1.0中,jar文件理有一个文本文件的列表。它表示jar的详细信息。它用来鉴别哪个企业bean在Ejb-jar文件。在EJB1.1中,XML文件包含了所有的必要信息。
生成Ejb-jar文件
jar cmf ..\manifest HelloWorld.jar *
配置bean
最后,我们还需要在Ejb容器中配置bean。常常执行一下步骤:
Ejb-jar文件的检验
容器工具来产生EJB对象和home对象
容器工具来生成RMI所需的stubs和skeletons
写无状态bean的客户代码
package com.wiley.compBooks.roman.session.helloworld;
import javax.ejb.*;
import javax.naming.*;
import java.rmi.*;
import java.util.Properties;
/**
* This class is an example of client code that invokes
* methods on a simple stateless session bean.
*/
public class HelloClient {
public static void main(String[] args) {
try {
/*
* Get System properties for JNDI initialization
*/
Properties props = System.getProperties();
/*
* Form an initial context
*/
Context ctx = new InitialContext(props);
/*
* Get a reference to the home object
* (the factory for EJB objects)
*/
HelloHome home = (HelloHome) ctx.lookup("HelloHome");
/*
* Use the factory to create the EJB Object
*/
Hello hello = home.create();
/*
* Call the hello() method, and print it
*/
System.out.println(hello.hello());
/*
* Done with EJB Object, so remove it
*/
hello.remove();
} catch (Exception e) {
e.printStackTrace();
}
}
}
客户端代码执行了一下任务:
定位home接口
使用home接口建立EJB对象
调用EJB对象上的hello()
移走EJB对象
运行
首先运行应用服务器。对于BEA的WebLogic,执行
t3server
客户端执行:
java -Djava.naming.factory.initial=
weblogic.jndi.TengahInitialContextFactory
-Djava.naming.provider.url=
t3://localhost:7001
com.wiley.compBooks.roman.session.helloworld.HelloClient
服务端输出:
setSessionContext()
ejbCreate()
hello()
ejbRemove()
客户端输出:
Hello, World!

本文地址:http://com.8s8s.com/it/it15557.htm