注入到MDB中的空EntityManager / EJB

问题描述 投票:3回答:1

我有一个部署到WebLogic 12.1.3的消息驱动bean(MDB)。我尝试使用@PersistenceContext注释将实体管理器注入MDB,但是实体管理器为null。我还尝试注入一个简单的无状态会话bean,它也为null。但是,MessageDrivenContext已正确注入。

我想念的是什么?根据我对搜索SO和Google的了解,您应该能够将EJB和实体管理器注入MDB。我可以将它们注入到应用程序的其他部分中。为什么不使用MDB?

注意:

  • 我正在尝试使用容器管理的交易
  • MDB在EAR内的单个JAR中与另外两个(同样不起作用)一起打包
  • JAR具有在应用程序的其他部分中起作用的persistence.xml
  • JAR也有一个beans.xml
  • 我尝试了事务注释和ActivationConfig属性的许多组合(如下)

MDB:

@MessageDriven(activationConfig =  {
    @ActivationConfigProperty(propertyName="acknowledgeMode", propertyValue="Auto-acknowledge"),
    @ActivationConfigProperty(propertyName="destinationJndiName", propertyValue="jms/MyTopic"),
    @ActivationConfigProperty(propertyName="destinationType", propertyValue="javax.jms.Topic"),
    @ActivationConfigProperty(propertyName="subscriptionDurability", propertyValue="Durable"),
    @ActivationConfigProperty(propertyName="connectionFactoryJndiName", propertyValue="weblogic.jms.XAConnectionFactory"),
    @ActivationConfigProperty(propertyName="transaction-type", propertyValue="Container"),
    @ActivationConfigProperty(propertyName="trans-attribute", propertyValue="Required"),
    @ActivationConfigProperty(propertyName="trans-timeout-seconds", propertyValue="120"),
    @ActivationConfigProperty(propertyName="topicMessagesDistributionMode", propertyValue="One-Copy-Per-Application")
})
@TransactionTimeoutSeconds(120)
@TransactionManagement(TransactionManagementType.CONTAINER)
public class MyMDB implements MessageListener {

    @PersistenceContext(unitName="myPersistenceUnit")
    private EntityManager entityManager;

    @Resource
    private MessageDrivenContext context;

    @EJB
    private HelloWorld helloWorld;

    @Override
    @TransactionAttribute(value = TransactionAttributeType.REQUIRED)
    public void onMessage(final Message message) {

        //entityManager is null
        //context is NOT null
        //helloWorld (EJB) is null
    }
}

persistence.xml:

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
             version="2.0">

    <persistence-unit name="myPersistenceUnit" transaction-type="JTA">

        <jta-data-source>MyDataSource</jta-data-source>

        <mapping-file>META-INF/queries.xml</mapping-file>

        <exclude-unlisted-classes>false</exclude-unlisted-classes>

        <shared-cache-mode>NONE</shared-cache-mode>

        <properties>
            <property name="javax.persistence.lock.timeout" value="30000"/>
        </properties>

    </persistence-unit>
</persistence>

EJB:

@Stateless
@LocalBean
public class HelloWorld {

    @PersistenceContext(unitName="myPersistenceUnit")
    private EntityManager entityManager;


    public String sayHello() {
        return "Hello World";
    }


    public EntityManager getEntityManager() {
        return entityManager;
    }
}
java java-ee ejb-3.1 weblogic12c message-driven-bean
1个回答
0
投票

我们面临着同样的问题。在过去的4年中,使用Weblogic 12.1.3可以完美运行,因为SysAdmins应用了补丁程序(于2019年11月30日发布),因此当我们在entitymanager上调用find方法时会得到NPE。我们的支持团队已聘请Oracle,我们正在等待他们的分析。我会尽快告诉您他们的发现。

© www.soinside.com 2019 - 2024. All rights reserved.