How to inject a Session Bean into a Message Driven Bean?

前端 未结 4 912
鱼传尺愫
鱼传尺愫 2021-01-19 00:43

I\'m reasonably new to Java EE, so this might be stupid.. bear with me pls :D

I would like to inject a stateless session bean into a message-driven bean. Basically,

4条回答
  •  無奈伤痛
    2021-01-19 01:31

    It seems that my problem was related to Inversion of Control and caused by my lack of knowledge and Netbeans' suggestions for Class/Interface names.

    I found out that - in order to find the the right bean and the right interface - I should name them properly. Here's what works:

    @Remote
    public interface Test {
    
      public void doSomething();
    }
    
    @Stateless
    public class TestBean implements Test {
    
      public void doSomething() {
        // business logic goes here
      }
    }
    

    And in the MDB I access 'Test' not 'TestBean':

    @MessageDriven(mappedName = "jms/mvs.TestController", activationConfig =  {
    @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
        })
    public class TestController implements MessageListener {
    
        @EJB
        private Test testBean;
    
        public TestController() {
        }
    
        public void onMessage(Message message) {
          testBean.doSomething();
        }
    }
    

提交回复
热议问题