Java中的IBM-MQ消息到达事件监听器

时间:2017-02-20 08:05:13

标签: java jms ibm-mq

规范:IBMMQ 7.x,Jdk1.7,TOMCAT7,WINOS-7

问题:一个监听器事件,用于持续监视指定IBM-MQ上的队列,并警告/触发/通知我“x no no fresh messages”可供使用。

问题:'For-Loop'或Timer或Cron不是 - 允许监视队列中的新消息。

建议得到高度赞赏。

1 个答案:

答案 0 :(得分:0)

相反,为什么不设置消息监听器并在消息到达时使用消息?当消息到达队列时,将调用您的应用程序消息侦听器。

以下是示例代码:

        JmsFactoryFactory ff = JmsFactoryFactory.getInstance(WMQConstants.WMQ_PROVIDER);

        // Create connection factory
        cf = ff.createConnectionFactory();
        // Set MQ properties
        cf.setStringProperty(WMQConstants.WMQ_QUEUE_MANAGER, "QM1");
        cf.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT);
        cf.setIntProperty(WMQConstants.WMQ_PORT,1414);
        cf.setStringProperty(WMQConstants.WMQ_HOST_NAME, "localhost");
        cf.setStringProperty(WMQConstants.WMQ_CHANNEL, "MY.SVRCONN");           
        System.out.println("Connection Factory created.");

        connection = cf.createConnection();
        System.out.println("Connection created.");

        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        System.out.println("Session created.");

        Destination destination = session.createQueue("SUB.Q");
        System.out.println("Destination created: " + destination.toString());
        consumer = session.createConsumer(destination);
        System.out.println("Consumer created.");
        // Now receive messages asynchronously
        // Create and register a new MessageListener for this consumer
        consumer.setMessageListener(new MessageListener() {
          public void onMessage(Message msg) {
            try {
                // Display the message that just arrived
                //System.out.println(msg);
                TextMessage txtMsg = (TextMessage)msg;
                System.out.println(txtMsg.getText());
            } // end try
            catch (Exception e) {
              System.out.println("Exception caught in onMessage():\n" + e);
            }
            return;
          } // end onMessage()
        }); // end setMessageListener               
        connection.start();
        Thread.sleep(5000);
        session.close();