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

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

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

我的问题是,当我的有状态bean被钝化并序列化为磁盘时,Weblogic也会尝试序列化对无状态bean的引用,并抛出NotSerializableException,引用由Weblogic注入的bean的代理.为了进行比较 – 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本地接口:

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)
最佳答案
截至目前,这似乎是Weblogic 12.1.3中的实际错误/限制,因此我将我的解决方法作为可能的解决方案发布.

为了使有状态bean成功通过钝化,需要实现使用javax.ejb.PrePassivate和javax.ejb.PostActivate注释的方法. @PrePassivate方法将使无状态bean引用指向null,并且@PostActivate方法将在再次激活Bean时执行该bean的查找.

@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");
}

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

转载注明原文:java – 由于无状态bean引用,有状态bean钝化失败 - 代码日志