`
suhuanzheng7784877
  • 浏览: 692904 次
  • 性别: Icon_minigender_1
  • 来自: 北京
博客专栏
Ff8d036b-05a9-33b5-828a-2633bb68b7e6
读金庸故事,品程序人生
浏览量:47291
社区版块
存档分类
最新评论

JavaEE5学习笔记05-EJB之会话Bean(sessionBean)总结-----3

阅读更多

1.      有状态的会话Beanhelloworld

下面我们来看看有状态的SessionBean是否在可靠的调用中严格维护着它的实例变量。

下面看代码

ShopService接口

 

 

 

 

/**

 

 *

 * @author liuyan

 *

 */

@Remote

public interface ShopService {

    void addItem(String item);

    Map<String, Integer> showItems();

    void removeForClient();

}

实现类如下

 

 

 

 

 

 

@Stateful

public class ShopServiceEAOImpl implements ShopService {

 

    private Map<String, Integer> buyInfo = new HashMap<String, Integer>();

 

    @Override

    public void addItem(String item) {

       if (buyInfo.containsKey(item)) {

           buyInfo.put(item, buyInfo.get(item) + 1);

       } else {

           buyInfo.put(item, 1);

       }

    }

 

    @Override

    public Map<String, Integer> showItems() {

       System.out.println("buyInfo:" + buyInfo);

       return buyInfo;

    }

 

    @PostConstruct

    public void postConstruct() {

       System.out.println("有状态SessionBeanShopServiceEAOImpl构造完毕");

    }

 

    @PreDestroy

    public void preDestroy() {

       System.out.println("调用毁灭对象之前的方法--preDestroy()");

    }

 

    @Remove

    public void removeForClient() {

       System.out.println("客户端调用了显示的移除EJBBean的方法!");

    }

 

}

junit测试代码如下:

 

import java.util.Properties;

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.naming.NamingException;

import junit.framework.TestCase;

import ejb.sessionBean.ShopService;

 

public class TestStatefulSessionBean extends TestCase {

 

    static ShopService shopService = null;

 

    public Context init() {

         …………………………

 

 

    }

 

 

    public void test01() throws NamingException {

       if (shopService == null) {

           Context context = init();

           shopService = (ShopService) context

                  .lookup("ShopServiceEAOImpl/remote");

       }

       shopService.addItem("素还真");

       System.out.println(shopService.showItems());

    }

   

    public void test02() throws NamingException {

       if (shopService == null) {

           Context context = init();

           shopService = (ShopService) context

                  .lookup("ShopServiceEAOImpl/remote");

       }

       shopService.addItem("素还真");

       System.out.println(shopService.showItems());

 

    }

   

    public void test03() throws NamingException {

       if (shopService == null) {

           Context context = init();

           shopService = (ShopService) context

                  .lookup("ShopServiceEAOImpl/remote");

       }

      

       shopService.addItem("叶小钗");

       System.out.println(shopService.showItems());

    }

   

    public void test04() throws NamingException {

       if (shopService == null) {

           Context context = init();

           shopService = (ShopService) context

                  .lookup("ShopServiceEAOImpl/remote");

       }

       System.out.println(shopService.showItems());

    }

}

每次单独调用testXXX的时候服务端的结果总是单一的元素,可以看出,服务端的实例变量在每次、分别调用的时候都是穿件了一个新的、刚出锅的、热乎乎、无污染的实例变量。(在此代码中就不列出结果了)

加入我们一气呵成执行此用例呢?让测试用例的静态变量所有方法中处在一个JVM中。

结果如下

 

11:32:09,896 INFO  [STDOUT] 有状态SessionBeanShopServiceEAOImpl构造完毕

11:32:10,022 INFO  [STDOUT] buyInfo:{素还真=1}

11:32:10,038 INFO  [STDOUT] buyInfo:{素还真=2}

11:32:10,069 INFO  [STDOUT] buyInfo:{素还真=2, 叶小钗=1}

11:32:10,080 INFO  [STDOUT] buyInfo:{素还真=2, 叶小钗=1}

由此观之在同一次调用中,服务端的无状态会话Bean确实是维护了实例变量的属性值及其状态。不会像无状态的会话调用,结果无法预期……

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics