从Listener onmessage向MQQueue发送消息

时间:2015-05-07 17:14:36

标签: java jms ibm-mq

我有这个特殊场景:

  • MQSender(发送" Hello"文本消息到IBMMQ中的测试队列)
  • MQSenderInternal(发送"循环"文本消息到测试队列中) IBMMQ)
  • MQListener(接收消息,如果它的Hello然后将发送另一个消息) 消息与MQSenderInternal)

MQSender作为独立的Java应用程序运行,而MQSenderInternal& MQListener是同一个jar的一部分,并部署在JBoss服务器中。

流程是这样的 - > MQSender - > MQListener - > MQSenderInternal - > MQListener(因为消息是Loop它应该停止)

当MQListener调用MQSenderInternal将消息发送到它侦听的队列时,它会抛出异常(JMSCC0109:消息驱动的bean引发运行时异常)并且它进入一个重新传递的无限循环。

我希望能够从侦听它的onmessage方法内部向测试队列发送消息,而不会抛出任何异常......这可能吗?

代码段:

MQSender

MQQueueConnectionFactory cf = new MQQueueConnectionFactory();
cf.setHostName(host);
cf.setPort(port);
cf.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT);
cf.setQueueManager(manager);
cf.setChannel(channel);

MQQueueConnection connection = (MQQueueConnection)
cf.createQueueConnection();

MQQueueSession session = (MQQueueSession) connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
MQQueue queue = (MQQueue) session.createQueue(queuename);
MQQueueSender sender = (MQQueueSender) session.createSender(queue);
TextMessage message = (TextMessage) session.createTextMessage("Hello");

// Start the connection
connection.start();
sender.send(message);

sender.close();
session.close();
connection.close();

MQSenderInternal

MQQueueConnectionFactory cf = new MQQueueConnectionFactory();
cf.setHostName(host);
cf.setPort(port);
cf.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT);
cf.setQueueManager(manager);
cf.setChannel(channel);

MQQueueConnection connection = (MQQueueConnection)
cf.createQueueConnection();

MQQueueSession session = (MQQueueSession) connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
MQQueue queue = (MQQueue) session.createQueue(queuename);
MQQueueSender sender = (MQQueueSender) session.createSender(queue);
TextMessage message = (TextMessage) session.createTextMessage("Loop");

// Start the connection
connection.start();
sender.send(message);

sender.close();
session.close();
connection.close();

MQListener

public void onMessage(Message rcvMessage) {
    TextMessage msg = null;
    try {
        if (rcvMessage instanceof TextMessage) {
            msg = (TextMessage) rcvMessage;

            if (msg.getText().equals("Loop")) {
                System.out.println("Received Message from internal sender: "
                        + msg.getText());
            } else {
                System.out.println("Received Message from standalone sender: "
                        + msg.getText());

                MQSenderInternal.send();
            }
        } else {
            System.out.println("Message of wrong type: "
                    + rcvMessage.getClass().getName());
        }
    } catch (JMSException e) {
        e.printStackTrace(); ;
        mdbContext.setRollbackOnly();
    }
}

敬请任何帮助:)

0 个答案:

没有答案