无状态 EJB 未注入消息驱动 bean (MDB)
Stateless EJB not injected in message driven bean (MDB)
我有一个消息驱动 bean (MDB),它实现了 MessageListener 并且有几个 EJB 属性,但是没有注入它们,所以我必须手动注入它们。 MDB 也有一个资源和一个注入良好的 CDI bean。
为什么没有自动注入 EJB?我在应用程序的其他部分使用 NotificationService EJB 并注入它们。关于如何找出问题的任何线索?
我没有从 Weblogic 12.1.3 收到任何错误,所以我不知道这里发生了什么。我的代码是(充满调试目的的痕迹)。我删除了与问题无关的 javadoc 和方法实现:
@MessageDriven(name = "MailMessageConsumer", description = "JMS consumer", mappedName = MailJndiConfiguration.JNDI_QUEUE,
activationConfig = {
@ActivationConfigProperty(propertyName = "acknowledgeMode",
propertyValue = "Auto-acknowledge"),
@ActivationConfigProperty(propertyName = "destinationType",
propertyValue = "javax.jms.Queue")
})
@TransactionAttribute(TransactionAttributeType.REQUIRED)
@MessageReceiver(responsibility = "Consumes JMS messages of type MailMessage")
public class MailMessageConsumer implements MessageListener {
private static final Logger log = LoggerFactory.getLogger(MailMessageConsumer.class);
@Resource
private MessageDrivenContext messageDrivenContext;
@EJB
private NotificationService notificationService;
@EJB
private MailClient mailClient;
@Inject
private ApplicationInformation applicationInformation;
@Override
public void onMessage(Message message) {
if (mailClient == null) {
log.error("mailClient object is null");
try {
log.info("Instantiating MailClient manually...");
mailClient = BeanManagerHelper.getReference(MailClient.class);
} catch (Exception e) {
log.error("Cannot instantiate MailClient manually", e);
}
}
if (notificationService == null) {
log.error("notificationService object is null");
try {
log.info("Instantiating NotificationService manually...");
notificationService = BeanManagerHelper.getReference(NotificationService.class);
} catch (Exception e) {
log.error("Cannot instantiate NotificationService manually", e);
}
}
// This never happens
if (applicationInformation == null) {
log.error("applicationInformation object is null");
}
// This never happens
if (messageDrivenContext == null) {
log.error("messageDrivenContext object is null");
}
deliverMessage(message);
}
private void deliverMessage(Message message) {
// Not important
}
private MailMessage retrieveMessage(Message message) {
// Not important
}
private void sendEmail(MailMessage mailMessage) {
// Not important
}
}
邮件客户端 EJB:
@Stateless
@LocalBean
@TransactionAttribute(TransactionAttributeType.MANDATORY)
@Service
public class MailClient {
private static final Logger logger = LoggerFactory.getLogger(MailClient.class);
@Resource(mappedName = MailJndiConfiguration.JNDI_MAIL_SESSION)
private Session mailSession;
@EJB
private NotificationService notificationService;
@Inject
private ApplicationInformation applicationInformation;
enum ValidationError {
NULL_OBJECT("Mail message is null"),
CONTENT_TYPE_EMPTY("Content type not initialized"),
BODY_EMPTY("Message body content is empty");
private static final String ERROR_MESSAGE_PREFIX = "Invalid mail message: ";
private String message = ERROR_MESSAGE_PREFIX;
ValidationError(String message) {
this.message += message;
}
public String getMessage() {
return message;
}
}
public void sendMail(MailMessage mailMessage) throws MailMessageSendingException {
// Not important
}
}
通知服务 EJB:
@Stateless
@LocalBean
@TransactionAttribute(TransactionAttributeType.MANDATORY)
@Service
public class NotificationService {
private static final Logger logger = LoggerFactory.getLogger(NotificationService.class);
@PersistenceContext(unitName = "myEntityManager")
private EntityManager entityManager;
@EJB
private NotificationPendingMessageValidator notificationPendingMessageValidator;
@EJB
private NotificationFinder notificationFinder;
@Inject
private ApplicationInformation applicationInformation;
public NotificationPendingMessageEntity saveNotificationMessageForDeferredMail(NotificationPendingMessageEntity notificationPendingMessageEntity) throws ValidationException {
// Not important
}
public List<NotificationPendingMessageEntity> findNotificationPendingMessageEntities(TimeSlot timeSlot) {
// Not important
}
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public NotificationMailEntity createNewMailEntity() {
// Not important
}
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public void updateMailEntity(NotificationMailEntity mailEntity) {
// Not important
}
public void createNotificationMessageProcessedEntity(NotificationProcessedMessageEntity notificationProcessedMessageEntity) {
// Not important
}
public void removeNotificationMessagePendingEntity(NotificationPendingMessageEntity notificationPendingMessageEntity) {
// Not important
}
public void reportMailFailure(NotificationMailEntity mailEntity, String failureNotice) {
// Not important
}
}
使用 @Inject
代替 @EJB
注释注入 EJB 效果很好。所以应该是某些Weblogic的补丁有问题,因为在另一个Weblogic(相同版本,不同补丁)中测试它也能正常工作
我有一个消息驱动 bean (MDB),它实现了 MessageListener 并且有几个 EJB 属性,但是没有注入它们,所以我必须手动注入它们。 MDB 也有一个资源和一个注入良好的 CDI bean。
为什么没有自动注入 EJB?我在应用程序的其他部分使用 NotificationService EJB 并注入它们。关于如何找出问题的任何线索?
我没有从 Weblogic 12.1.3 收到任何错误,所以我不知道这里发生了什么。我的代码是(充满调试目的的痕迹)。我删除了与问题无关的 javadoc 和方法实现:
@MessageDriven(name = "MailMessageConsumer", description = "JMS consumer", mappedName = MailJndiConfiguration.JNDI_QUEUE,
activationConfig = {
@ActivationConfigProperty(propertyName = "acknowledgeMode",
propertyValue = "Auto-acknowledge"),
@ActivationConfigProperty(propertyName = "destinationType",
propertyValue = "javax.jms.Queue")
})
@TransactionAttribute(TransactionAttributeType.REQUIRED)
@MessageReceiver(responsibility = "Consumes JMS messages of type MailMessage")
public class MailMessageConsumer implements MessageListener {
private static final Logger log = LoggerFactory.getLogger(MailMessageConsumer.class);
@Resource
private MessageDrivenContext messageDrivenContext;
@EJB
private NotificationService notificationService;
@EJB
private MailClient mailClient;
@Inject
private ApplicationInformation applicationInformation;
@Override
public void onMessage(Message message) {
if (mailClient == null) {
log.error("mailClient object is null");
try {
log.info("Instantiating MailClient manually...");
mailClient = BeanManagerHelper.getReference(MailClient.class);
} catch (Exception e) {
log.error("Cannot instantiate MailClient manually", e);
}
}
if (notificationService == null) {
log.error("notificationService object is null");
try {
log.info("Instantiating NotificationService manually...");
notificationService = BeanManagerHelper.getReference(NotificationService.class);
} catch (Exception e) {
log.error("Cannot instantiate NotificationService manually", e);
}
}
// This never happens
if (applicationInformation == null) {
log.error("applicationInformation object is null");
}
// This never happens
if (messageDrivenContext == null) {
log.error("messageDrivenContext object is null");
}
deliverMessage(message);
}
private void deliverMessage(Message message) {
// Not important
}
private MailMessage retrieveMessage(Message message) {
// Not important
}
private void sendEmail(MailMessage mailMessage) {
// Not important
}
}
邮件客户端 EJB:
@Stateless
@LocalBean
@TransactionAttribute(TransactionAttributeType.MANDATORY)
@Service
public class MailClient {
private static final Logger logger = LoggerFactory.getLogger(MailClient.class);
@Resource(mappedName = MailJndiConfiguration.JNDI_MAIL_SESSION)
private Session mailSession;
@EJB
private NotificationService notificationService;
@Inject
private ApplicationInformation applicationInformation;
enum ValidationError {
NULL_OBJECT("Mail message is null"),
CONTENT_TYPE_EMPTY("Content type not initialized"),
BODY_EMPTY("Message body content is empty");
private static final String ERROR_MESSAGE_PREFIX = "Invalid mail message: ";
private String message = ERROR_MESSAGE_PREFIX;
ValidationError(String message) {
this.message += message;
}
public String getMessage() {
return message;
}
}
public void sendMail(MailMessage mailMessage) throws MailMessageSendingException {
// Not important
}
}
通知服务 EJB:
@Stateless
@LocalBean
@TransactionAttribute(TransactionAttributeType.MANDATORY)
@Service
public class NotificationService {
private static final Logger logger = LoggerFactory.getLogger(NotificationService.class);
@PersistenceContext(unitName = "myEntityManager")
private EntityManager entityManager;
@EJB
private NotificationPendingMessageValidator notificationPendingMessageValidator;
@EJB
private NotificationFinder notificationFinder;
@Inject
private ApplicationInformation applicationInformation;
public NotificationPendingMessageEntity saveNotificationMessageForDeferredMail(NotificationPendingMessageEntity notificationPendingMessageEntity) throws ValidationException {
// Not important
}
public List<NotificationPendingMessageEntity> findNotificationPendingMessageEntities(TimeSlot timeSlot) {
// Not important
}
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public NotificationMailEntity createNewMailEntity() {
// Not important
}
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public void updateMailEntity(NotificationMailEntity mailEntity) {
// Not important
}
public void createNotificationMessageProcessedEntity(NotificationProcessedMessageEntity notificationProcessedMessageEntity) {
// Not important
}
public void removeNotificationMessagePendingEntity(NotificationPendingMessageEntity notificationPendingMessageEntity) {
// Not important
}
public void reportMailFailure(NotificationMailEntity mailEntity, String failureNotice) {
// Not important
}
}
使用 @Inject
代替 @EJB
注释注入 EJB 效果很好。所以应该是某些Weblogic的补丁有问题,因为在另一个Weblogic(相同版本,不同补丁)中测试它也能正常工作