Null EntityManager/EJB Injected into MDB

杀马特。学长 韩版系。学妹 提交于 2019-12-10 18:33:32

问题


I have a message-driven bean (MDB) deployed to WebLogic 12.1.3. I tried to inject an entity manager into the MDB using the @PersistenceContext annotation, but the entity manager is null. I also tried injecting a simple stateless session bean and it too is null. However, the MessageDrivenContext is injected correctly.

What am I missing? From what I can tell searching SO and Google, you should be able to inject EJBs and entity managers into MDBs. I am able to inject them into other parts of the application just fine. Why not MDBs?

Notes:

  • I am trying to use container-managed transactions
  • The MDB is packaged with two others (that also don't work) in a single JAR that is within an EAR
  • The JAR has a persistence.xml that works in other parts of the application
  • The JAR has a beans.xml in it too
  • I tried many combinations of the transaction annotations and ActivationConfig properties (below)

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;
    }
}

来源:https://stackoverflow.com/questions/36394208/null-entitymanager-ejb-injected-into-mdb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!