由于无状态bean引用,有状态bean钝化失败

时间:2015-07-14 11:41:41

标签: java ejb weblogic weblogic12c ejb-passivation

我使用Weblogic 12c(特定于12.1.3)来部署我的(EJB 3.1)应用程序。

在我的应用程序中,我有一个@Stateful bean,它包含EntityManager和其他@Stateless bean的引用,使用@PersistenceContext@EJB注释注入分别

我的问题是,当我的有状态bean被钝化并序列化为磁盘时,Weblogic也尝试序列化对无状态bean的引用,并抛出NotSerializableException引用bean的代理,是由Weblogic注入的。为了进行比较 - EntityManager引用被钝化并重新激活而没有任何问题。只有无国籍豆才会引发问题。

我知道我可以定义@PrePassivate@PostActivate方法来使我的代码工作,但有什么方法可以让容器自己处理无状态bean引用?

附上为我重现问题的示例代码。

远程bean接口:

import javax.ejb.Remote;

@Remote
public interface Passivate {

    public void doSomething();

}

有状态Bean实现:

import javax.ejb.EJB;
import javax.ejb.PostActivate;
import javax.ejb.PrePassivate;
import javax.ejb.Stateful;

@Stateful(mappedName = "PassivateBean")
public class PassivateBean implements Passivate {

    @EJB(mappedName = "NoStateBean")
    private NoState noStateBean;

    @Override
    public void doSomething() {
        System.out.println("Hello world");
    }

    @PrePassivate
    public void prePassivate() {
        // as a workaround - can set noStateBean to null here
    }

    @PostActivate
    public void postActivate() {
        // as a workaround - can lookup and set noStateBean here manually
    }
}

NoState Local interface:

import javax.ejb.Local;

@Local
public interface NoState {

    public void foo();

}

NoState bean实现:

import javax.ejb.Stateless;

@Stateless(mappedName = "NoStateBean")
public class NoStateBean implements NoState {

    @Override
    public void foo() {
        System.out.println("foo");
    }

}

最后,当PassivateBean被钝化到磁盘时我得到的异常:

<Jul 14, 2015 2:36:20 PM IDT> <Error> <EJB> <BEA-010024> <Error occurred during passivation: java.io.NotSerializableException: passivateTest.NoStateBean_i02rk_Impl
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
    at weblogic.ejb.container.utils.Serializer.writeObject(Serializer.java:52)
    at passivateTest.PassivateBean_dmn8u8_Impl.writeObject(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:988)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1496)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
    at weblogic.ejb.container.swap.PassivationUtils.write(PassivationUtils.java:84)
    at weblogic.ejb.container.swap.DiskSwap.write(DiskSwap.java:173)
    at weblogic.ejb.container.manager.StatefulSessionManager.swapOut(StatefulSessionManager.java:1246)
    at weblogic.ejb.container.cache.LRUCache.passivateNode(LRUCache.java:199)
    at weblogic.ejb.container.cache.LRUCache.timerExpired(LRUCache.java:187)
    at weblogic.timers.internal.TimerImpl.run(TimerImpl.java:304)
    at weblogic.work.SelfTuningWorkManagerImpl$WorkAdapterImpl.run(SelfTuningWorkManagerImpl.java:548)
    at weblogic.work.ExecuteThread.execute(ExecuteThread.java:311)
    at weblogic.work.ExecuteThread.run(ExecuteThread.java:263)

1 个答案:

答案 0 :(得分:1)

截至目前,这似乎是Weblogic 12.1.3中的实际错误/限制,因此我将解决方法作为可能的解决方案发布。

要使javax.ejb.PrePassivate bean成功完成钝化,需要实现使用javax.ejb.PostActivate@PrePassivate注释的方法。 null方法将使无状态bean引用指向@PostActivate,并且@PrePassivate public void prePassivate() { noStateBean = null; } @PostActivate public void postActivate() { // The lookup string is correct assuming the ejb module is deployed within an ear. If your setup is different the JNDI lookup name may be slightly different. noStateBean = InitialContext.doLookup("java:module/NoStateBean!NoState"); } 方法将在再次激活bean时对该bean执行查找。

PRODUCT_NAME

如果在接下来的几周左右没有评论或其他答案,我会将此答案标记为解决方案。