JMS 2 MDB侦听多个队列

时间:2018-10-22 17:16:12

标签: java jms websphere-liberty open-liberty jms2

我在Liberty 18.0.0.2上使用JavaEE8。
我像这样(server.xml)设置jms激活配置:

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

这是我简单的MDB消息侦听器:

@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 {
...
}

我也定义了两个这样的队列:

@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);
    }
}   

我的问题是MDB监听多个队列。
我想让mdb只听onlineQueue。
如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

您有两个不同的@JMSDestinationDefinition。一个具有name的{​​{1}},另一个具有java:app/onlineQueue的名称。但是,他们两个都具有java:app/offlineQueue的应用程序,这对我来说似乎是错误的。无论您在JNDI中查找destinationName还是java:app/onlineQueue,都将得到对名为java:app/offlineQueue的目的地的引用。几乎可以肯定,这就是为什么发送到application的邮件被MDB接收的原因。