ejb2.0---无状态会话bean

最近工程需要些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的部署 与调用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值