etSessionContext
public void ejbCreate() {
}//ejbCreate
public void ejbPassivate() {
}//ejbPassivate
}//class sailorsy4.ejbActivate()方法
和ejbPassivate正好相反,它将被挂起的Bean从新调回。
import java.rmi.*;
import javax.ejb.*;
public class sailorsy implements SessionBean{
private SessionContext ctx=null;
public voic setSessionContext(SessionContext ctx){
this.ctx=ctx;
}//setSessionContext
public void ejbCreate() {
}//ejbCreate
public void ejbPassivate() {
}//ejbPassivate
public void ejbActivate() {
}//ejbActivate
}//class sailorsy
5.ejbRemove()方法
它可以清除EJB容器中的Bean。
import java.rmi.*;
import javax.ejb.*;
public class sailorsy implements SessionBean{
private SessionContext ctx=null;
public voic setSessionContext(SessionContext ctx){
this.ctx=ctx;
}//setSessionContext
public void ejbCreate() {
}//ejbCreate
public void ejbPassivate() {
}//ejbPassivate
public void ejbActivate() {
}//ejbActivate
public void ejbRemove() {
}//ejbRemove
public String showname(){
return "Hello,my name is sailorsy";
}//自己的商务方法
}//class sailorsy
对EJB中Session Bean的见解(3)
时间:2011-02-15
以上这些是EJB必需的回调方法,我们可以在里面加入自己的方法,加入自己的商务逻辑。
B.Home 接口: sailorsyHome
import java.rmi.*;
import javax.ejb.*;
public interface sailorsyHome extends EJBHome {
public sailorsyRemote create() throws RemoteException, CreateException;
}
C. Remote接口:sailorsyRemote
import java.rmi.*;
import javax.ejb.*;
public interface sailorsyRemote extends EJBObject {
public java.lang.String showname() throws RemoteException;
}
三.调用会话 Bean:sailorsyTestClient1
import javax.naming.*;
import javax.ejb.*;
import javax.rmi.PortableRemoteObject;
import java.rmi.*;
public class sailorsyTestClient1 {
private sailorsyHome sailorsyHomeObject = null;
//Construct the EJB test client
public sailorsyTestClient1() {
try {
//
以下是客户端使用JNDI定位Home对象。
Context ctx = new InitialContext();
//look up jndi name
Object ref = ctx.lookup("sailorsy");
//cast to Home interface
sailorsyHomeObject = (sailorsyHome) PortableRemoteObject.narrow(ref, sailorsyHome.class);
}
catch(Exception e) {
e.printStackTrace();
}
}
//----------------------------------------------------------------------------
// Utility Methods
//----------------------------------------------------------------------------
public sailorsyHome getHome() {
return sailorsyHomeObject;
}
//Main method
public static void main(String[] args) throws Exception{
sailorsyTestClient1 client = new sailorsyTestClient1();
sailorsyRemote sr=client.getHome() .create() ;
String s=sr.showname() ;
System.out.print(s);
// Use the getHome() method of the client object to call Home interface
// methods that will return a Remote interface reference. Then
// use that Remote interface reference to access the EJB.
}
}
以上的EJB在win2000+jbuilder5/jbuilder6+BAS4.5经过测试 |