3

我有一個消息驅動bean(MDB),它實現MessageListener並具有多個EJB屬性,但其中沒有注入它們,因此我必須手動注入它們。 MDB還有一個資源和一個CDI bean,它們被正確注入。無狀態EJB未注入消息驅動Bean(MDB)

爲什麼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 
    } 
} 

的MailClient 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 
    } 
} 

NotificationService 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 
    } 
} 
+2

好問題,我的同事有同樣的問題 – Lastnico

回答

2

使用@Inject用於注入EJB的替代@EJB註釋工作正常。所以應該有一些Weblogic補丁的問題,因爲在另一個Weblogic(相同的版本,不同的補丁)測試它的工作,以及

+0

任何機會,你知道嗎確切的補丁?我們有確切的案例與12.2.1 – meskobalazs

+0

對不起,不知道。我沒有負責Weblogic管理方面的工作。 –

+0

謝謝:) – meskobalazs

相關問題