EJB——有状态的会话bean和无状态的会话bean

版权声明:本文为博主原创文章,转载请标明出处,谢谢! https://blog.csdn.net/u010926964/article/details/51279225

  前面文章介绍过EJB的一些基本概念和三种bean,今天重点介绍一下会话bean中的有状态会话bean和无状态会话bean。

  会话bean分为有状态的会话bean和无状态的会话bean:

有状态会话bean :

  每个用户有自己特有的一个实例,在用户的生存期内,bean保持了用户的信息,即“有状态”;一旦用户灭亡(调用结束或实例结束),bean的生命期也告结束。即每个用户最初都会得到一个初始的bean。 

无状态会话bean :

  bean一旦实例化就被加进会话池中,各个用户都可以共用。即使用户已经消亡,bean 的生命期也不一定结束,它可能依然存在于会话池中,供其他用户调用。由于没有特定的用户,那么也就不能保持某一用户的状态,所以叫无状态bean。但无状态会话bean 并非没有状态,如果它有自己的属性(变量),那么这些变量就会受到所有调用它的用户的影响,这是在实际应用中必须注意的。

实例

代码结构

  

配置文件

java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost


有状态会话bean代码

服务端接口

package com.bjsxt.ejb;

public interface StatefulEjb {
	public void compute(int i);
	public int getResult();
}


服务端实现

package com.bjsxt.ejb;

import javax.ejb.Remote;
import javax.ejb.Stateful;

@Stateful
@Remote
public class StatefulEjbBean implements StatefulEjb {

	int state;
	public void compute(int i) {
		state=state+i;

	}

	public int getResult() {
		return state;
	}

}

客户端

package com.bjsxt.ejb;

import javax.naming.InitialContext;

public class StatefulEjbClient {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		InitialContext context = new InitialContext();
		StatefulEjb ejb1=(StatefulEjb)context.lookup("StatefulEjbBean/remote");
		System.out.println(ejb1.getResult());
		ejb1.compute(1);
		System.out.println(ejb1.getResult());
		ejb1.compute(1);
		System.out.println(ejb1.getResult());
		ejb1.compute(1);
		System.out.println(ejb1.getResult());
		ejb1.compute(1);
		System.out.println(ejb1.getResult());
		ejb1.compute(1);
		System.out.println(ejb1.getResult());

		StatefulEjb ejb2=(StatefulEjb)context.lookup("StatefulEjbBean/remote");
		System.out.println(ejb2.getResult());
		ejb2.compute(1);
		System.out.println(ejb2.getResult());
		ejb2.compute(1);
		System.out.println(ejb2.getResult());
		ejb2.compute(1);
		System.out.println(ejb2.getResult());
		ejb2.compute(1);
		System.out.println(ejb2.getResult());
		ejb2.compute(1);
		System.out.println(ejb2.getResult());
	}

}

输出结果

无状态会话bean代码

服务端接口

package com.bjsxt.ejb;

public interface StatelessEjb {
	public void compute(int i);
	public int getResult();
}

服务端实现

package com.bjsxt.ejb;

import javax.ejb.Remote;
import javax.ejb.Stateless;

@Stateless
@Remote
public class StatelessEjbBean implements StatelessEjb {

	int state;
	public void compute(int i) {
		state=state+i;

	}

	public int getResult() {
		return state;
	}

}


客户端

package com.bjsxt.ejb;

import javax.naming.InitialContext;

public class StatelessEjbClient {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		InitialContext context = new InitialContext();
		StatelessEjb ejb1=(StatelessEjb)context.lookup("StatelessEjbBean/remote");
		System.out.println(ejb1.getResult());
		ejb1.compute(1);
		System.out.println(ejb1.getResult());
		ejb1.compute(1);
		System.out.println(ejb1.getResult());
		ejb1.compute(1);
		System.out.println(ejb1.getResult());
		ejb1.compute(1);
		System.out.println(ejb1.getResult());
		ejb1.compute(1);
		System.out.println(ejb1.getResult());

		StatelessEjb ejb2=(StatelessEjb)context.lookup("StatelessEjbBean/remote");
		System.out.println(ejb2.getResult());
		ejb2.compute(1);
		System.out.println(ejb2.getResult());
		ejb2.compute(1);
		System.out.println(ejb2.getResult());
		ejb2.compute(1);
		System.out.println(ejb2.getResult());
		ejb2.compute(1);
		System.out.println(ejb2.getResult());
		ejb2.compute(1);
		System.out.println(ejb2.getResult());
	}

}

输出结果

  从实例中可以看出,有状态的会话bean 每个用户有自己特有的一个实例,每次会话都从零开始。无状态的会话bean各个用户都可以共用bean,第二次会话在第一次的基础上累加。

如何选择

  根据上面分析的有状态会话Bean和无状态会话Bean的优缺点。如果要频繁的访问,并且多次访问之间会共享一些信息,这时候应该使用有状态会话Bean。对于不经常使用的功能,可以使用无状态会话Bean。无状态会话Bean的使用要比有状态会话Bean的使用多。

猜你喜欢

转载自blog.csdn.net/u010926964/article/details/51279225