最近工程需要些ejb2.0的测试代码,估顺手瞎写
创建ejb项目 选择2.0
1、 创建bean类
public class SLSHelloBean implements SessionBean{
public String sayHello(String name) throws RemoteException{
return name+" say: this is my S L S Bean";
}
public void ejbActivate() throws EJBException, RemoteException {
// TODO Auto-generated method stub
}
public void ejbPassivate() throws EJBException, RemoteException {
// TODO Auto-generated method stub
}
public void ejbRemove() throws EJBException, RemoteException {
// TODO Auto-generated method stub
}
public void setSessionContext(SessionContext arg0) throws EJBException,
RemoteException {
// TODO Auto-generated method stub
}
}
2、创建远程接口
public interface SLSHello extends EJBObject{
public String sayHello(String name) throws RemoteException;
}
3、创建Home接口
public interface SLSHelloHome extends EJBHome {
public SLSHello create()throws RemoteException,CreateException;
}
4、书写部署描述符
<?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 id="ejb-jar_ID">
<display-name>SLSBean</display-name>
<enterprise-beans>
<session>
<display-name>SLSHello</display-name>
<ejb-name>SLSHello</ejb-name>
<home>com.sun.sls.SLSHelloHome</home>
<remote>com.sun.sls.SLSHello</remote>
<ejb-class>com.sun.sls.SLSHelloBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>
5、用ant部署到Glassfish上
详细见 http://blog.csdn.net/a0501bqzhxy/article/details/6536269
6、书写客户端调用代码
public class Test {
public static void main(String[] args) {
try {
Properties props = new Properties();
props.setProperty("java.naming.factory.initial",
"com.sun.enterprise.naming.SerialInitContextFactory");
props.setProperty("java.naming.factory.url.pkgs",
"com.sun.enterprise.naming");
props.setProperty("java.naming.factory.state",
"com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
props.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
Context ctx = new InitialContext(props);
Object objRef = ctx.lookup("com.sun.sls.SLSHelloHome");
SLSHelloHome home = (SLSHelloHome) PortableRemoteObject.narrow(
objRef, SLSHelloHome.class);
SLSHello hello = home.create();
System.out.println(hello.sayHello("testSLS"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
至此完成一个无状态会话Bean的部署 与调用