zoukankan      html  css  js  c++  java
  • 有状态的EJB对象和无状态的EJB对象



    一,定义有状态Bean和无状态Bean


    有状态Bean:


    @Stateful
    @Remote
    public class StatefulEjbBean implements StatefulEjb{
    
    	private int state;
    	
    	@Override
    	public void compute(int i) {
    		state=state+i;
    	}
    
    	@Override
    	public int getResult() {
    		return state;
    	}
    
    }


    无状态Bean:


    @Stateless
    @Remote
    public class StatelessEjbBean implements StatelessEjb {
    
    	private int state;
    
    	@Override
    	public void compute(int i) {
    		state = state + i;
    	}
    
    	@Override
    	public int getResult() {
    		return state;
    	}
    }


    二,client測试及结果


    1。測试有状态EJB对象:


    public class StatefulEjbClient {
    
    	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());
    		System.out.println("第一次会话结束---------");
    		
    		//第二次会话
    		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());
    		System.out.println("第二次会话结束---------");
    		
    		
    		
    	}
    
    }
    

    结果:




    2,測试无状态EJB对象:



    public class StatelessEjbClient {
    
    	public static void main(String[] args) throws NamingException {
    		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());
    		System.out.println("第一次会话结束---------");
    		
    		//第二次会话
    		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());
    		System.out.println("第二次会话结束---------");
    		
    		//推断每次查找到的对象是否一样
    		System.out.println(ejb1==ejb2);//false
    		
    	}
    
    }
    

    结果:





    三,结果对照


            通过多次运行,发现对于有状态的EJB对象。每次通过查找获得的对象都是新对象。而对于无状态的EJB对象,每次查找获得的对象都有一个单例类的效果。多次运行測试无状态的EJB对象的方法,会发现服务端的貌似始终在对一个对象进行操作。








  • 相关阅读:
    剧集更新表
    Pyhton资源
    JAVA资源
    012 循环
    011 条件判断
    010 使用list和tuple
    009 字符串和编码
    007 Python基础
    python 内置函数
    python 获取当前运行的类名函数名inspect.stack()[1][3]
  • 原文地址:https://www.cnblogs.com/wzzkaifa/p/7228479.html
Copyright © 2011-2022 走看看