简体   繁体   English

如何在集成测试(OpenEjb)中测试有状态会话bean的钝化?

[英]Howto test passivation of stateful session beans in integration test (OpenEjb)?

I am using OpenEjb local server (3.1.4) in my integration tests (@LocalClient annotated test classes). 我在集成测试(@LocalClient批注的测试类)中使用OpenEjb本地服务器(3.1.4)。

@LocalClient
public class BoxDaoTest{

    @EJB
    private BoxDao boxDao;
    ...
}

BoxDao ist a stateful session bean. BoxDao ist一个有状态的会话bean。 Ist it possible to trigger passivation of BoxDao in OpenEjb to test it in the integration test? 是否有可能在OpenEjb中触发BoxDao的钝化以在集成测试中对其进行测试?

You can create/configure the Stateful container in the InitialContext properties you use to boot OpenEJB in the tests case. 您可以在用来启动测试案例中的OpenEJB的InitialContext属性中创建/配置Stateful容器。

These settings will do the trick: 这些设置可以解决问题:

final Properties p = new Properties();
p.put("MyStatefulContainer", "new://Container?type=STATEFUL");
p.put("MyStatefulContainer.Capacity", "0");
p.put("MyStatefulContainer.Frequency", "0");

p.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.core.LocalInitialContextFactory");
final InitialContext initialContext = new InitialContext(properties);
//lookup your beans from the initialContext

This will cause the @Stateful bean to essentially never reside in memory, it will be read from disk and written back to disk on each method invocation or lifecycle callback. 这将导致@Stateful bean本质上永远不会驻留在内存中,它将在每个方法调用或生命周期回调中从磁盘读取并写回到磁盘。

The same thing can be done with the EJB 3.1 javax.ejb.embeddable.EJBContainer API: EJB 3.1 javax.ejb.embeddable.EJBContainer API可以完成相同的操作:

final Properties p = new Properties();
p.put("MyStatefulContainer", "new://Container?type=STATEFUL");
p.put("MyStatefulContainer.Capacity", "0");
p.put("MyStatefulContainer.Frequency", "0");

final EJBContainer container = EJBContainer.createEJBContainer(p);
Foo foo = (Foo) container.getContext().lookup("java:global/yourapp/yourbean");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM