javajmswebsphere-libertyopen-libertyjms2

JMS 2 MDB listen to multiple queue


I use JavaEE 8 on Liberty 18.0.0.2 .
I setup jms activation config like this (server.xml):

  <messagingEngine>
    <queue id="application" />
  </messagingEngine>
  <jmsActivationSpec id="app/appMDB">
    <properties.wasJms destinationRef="java:app/onlineQueue" />
  </jmsActivationSpec>   

And this is my simple MDB message listener :

@MessageDriven(
        name = "appMDB",
        mappedName = "java:app/onlineQueue",
        activationConfig = {
                @ActivationConfigProperty(propertyName = "destinationType",
                        propertyValue = "javax.jms.Queue"),
                @ActivationConfigProperty(propertyName = "destination",
                        propertyValue = "java:app/onlineQueue")
        }
)
public class ApplicationMessageListener implements MessageListener {
...
}

And also I defined two queues like this :

@Stateless
@JMSDestinationDefinitions(
        value = {
                @JMSDestinationDefinition(
                        name = "java:app/onlineQueue",
                        interfaceName = "javax.jms.Queue",
                        destinationName = "application"),
                @JMSDestinationDefinition(
                        name = "java:app/offlineQueue",
                        interfaceName = "javax.jms.Queue",
                        destinationName = "application")
        }
)
public class MessageService {

    @Inject
    private Logger logger;

    @Resource(lookup = "java:app/onlineQueue")
    private Queue onlineQueue;

    @Resource(lookup = "java:app/offlineQueue")
    private Queue offlineQueue;

    @Inject
    private JMSContext context;

    @EJB
    private MessageUtils messageUtils;

    public void sendToOnlineQueue(SimpleMessage simpleMessage) {
        TextMessage message = messageUtils.createTextMessage(simpleMessage);
        logger.info("Send online Message : " + simpleMessage);
        JMSProducer producer = context.createProducer();
        producer.send(onlineQueue, message);
    }

    public void sendToOfflineQueue(SimpleMessage simpleMessage) {
        TextMessage message = messageUtils.createTextMessage(simpleMessage);
        logger.info("Send offline Message : " + simpleMessage);
        JMSProducer producer = context.createProducer();
        producer.send(offlineQueue, message);
    }
}   

My problem is MDB listen to multiple queues .
I want mdb only listen to onlineQueue .
How can fix this problem ?


Solution

  • You have two different @JMSDestinationDefinition. One has the name of java:app/onlineQueue, and the other has the name of java:app/offlineQueue. However, both of them have the destinationName of application which seems wrong to me. Whether you look up java:app/onlineQueue or java:app/offlineQueue in JNDI you'll get a reference to a destination named application. This is almost certainly why messages sent to java:app/offlineQueue are picked up by the MDB.