我有一个消息驱动 bean (MDB) 部署到 WebLogic 12.1.3。我尝试使用 @PersistenceContext 注释将实体管理器注入 MDB,但实体管理器为空。我还尝试注入一个简单的无状态会话 bean,它也是空的。但是,MessageDrivenContext 已正确注入。
我缺少什么?据我所知,搜索 SO 和 Google,您应该能够将 EJB 和实体管理器注入 MDB。我可以将它们注入应用程序的其他部分。为什么不是多边开发银行?
Notes:
- 我正在尝试使用容器管理的事务
- 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
}
}
持久性.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;
}
}