无状态会话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!
闽公网安备 35060202000074号