error-handling - 无法模拟 MQ 原因码 2009

标签 error-handling jms ibm-mq

我正在尝试模拟 WebSphere MQ 原因代码 2009 以在下面的 JMS 代码中处理但无法获得它。相反,我得到了 2059。我所做的就是在进行连接调用时断开 SVRCONN channel 。如何在我的示例代码中获得 2009。
我在再次建立连接并使用事务 session 之前添加了 sleep 时间。还可以做些什么来正确处理原因代码 2009,最终队列管理器不会因频繁的不成功连接尝试而受到影响。
请找到代码。

private static void connectToQmgr(MQQueueConnectionFactory cf) {
    // TODO Auto-generated method stub

    MQQueueConnection connection = null;
    MQQueueSession session = null;
    MQQueue queue = null;
    MQQueueSender sender = null;

    //While Statement to make sure multiple connection tries are made until connection establishes

    while (connection == null){

    try {
        connection = (MQQueueConnection) cf.createConnection();
        session = (MQQueueSession) connection.createQueueSession(true, Session.CLIENT_ACKNOWLEDGE);
        queue = (MQQueue) session.createQueue("queue:///LQ");
        sender = (MQQueueSender) session.createSender(queue);
        //MQQueueReceiver receiver = (MQQueueReceiver) session.createReceiver(queue);
        long uniqueNumber = System.currentTimeMillis() % 1000;
        TextMessage message = session.createTextMessage("MQJMSTest "+ uniqueNumber);
        // Start the connection
        connection.start();
        sender.send(message);
        session.commit();
        System.out.println("Sent message:\\n" + message);

       // JMSMessage receivedMessage = (JMSMessage) receiver.receive(10000);
       // System.out.println("\\nReceived message:\\n" + receivedMessage);

        session.commit();

        sender.close();
       // receiver.close();
        session.close();
        connection.stop();
        connection.close();

        System.out.println("\\nSUCCESS\\n");

    } catch (JMSException je) {
        System.err.println("Caught JMSException");

        // Check for linked exceptions in JMSException to catch MQException and Reason Codes 
        Throwable t = je;
        while (t != null) {
            // Write out the message that is applicable to all exceptions
            System.err.println("Exception Msg: " + t.getMessage());
            // Write out the exception stack trace
            t.printStackTrace(System.err);

            // Add on specific information depending on the type of exception
            if (t instanceof JMSException) {
                JMSException je1 = (JMSException) t;
                System.err.println("JMS Error code: " + je1.getErrorCode());

                if (t instanceof JmsExceptionDetail){
                    JmsExceptionDetail jed = (JmsExceptionDetail)je1;
                    System.err.println("JMS Explanation: " + jed.getExplanation());
                    System.err.println("JMS Explanation: " + jed.getUserAction());
                }
            } else if (t instanceof MQException) {
                MQException mqe = (MQException) t;
                System.err.println("WMQ Completion code: " + mqe.getCompCode());
                System.err.println("WMQ Reason code: " + mqe.getReason());

                //###################################################################################
                //MQ Reason Code Error Handle here
                //Currently Handling MQ Reason Code 2059 since unable to simulate 2009
                //If connection handle exists make sure you close everything and add a wait interval and try a new connection again
                if (mqe.getReason()== 2059){
                    System.out.println("Inside MQ Reson Code Handle");

                    if( connection != null){
                         try {
                            sender.close();
                            // receiver.close();
                            session.close();
                            connection.stop();
                            connection.close();
                        } catch (JMSException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }

                    // Add Wait Interval for 5 sec
                    try {
                        Thread.sleep(5000);
                        System.out.println("Inside Thread Sleep for 5 sec");
                        //Try New connecting Again
                        connectToQmgr(cf);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();

                    }   
                }
                 //##################################################################################           
            } else if (t instanceof JmqiException){
                JmqiException jmqie = (JmqiException)t;
                System.err.println("WMQ Log Message: " + jmqie.getWmqLogMessage());
                System.err.println("WMQ Explanation: " + jmqie.getWmqMsgExplanation());
                System.err.println("WMQ Msg Summary: " + jmqie.getWmqMsgSummary());
                System.err.println("WMQ Msg User Response: "
                                   + jmqie.getWmqMsgUserResponse());
                System.err.println("WMQ Msg Severity: " + jmqie.getWmqMsgSeverity());
            }

            // Get the next cause
            t = t.getCause();
       }
    }

    }

}

最佳答案

一个 2009MQRC_CONNECTION_BROKEN .您是否考虑过拔掉网络电缆或关闭连接所经过的网络接口(interface)?在笔记本电脑上运行代码时,这很容易。对于服务器,您可以尝试通过 SSH 代理或其他可以关闭的虚拟接口(interface)路由连接。

要回答问题的第二部分,请确保程序在重新连接尝试之间休眠几秒钟。或者,更好的是,使用现代版本的 WMQ 客户端并使用自动重新连接选项。这将进行几次快速重试,然后放慢速度以进行后续重试。

关于error-handling - 无法模拟 MQ 原因码 2009,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14505144/

相关文章:

java - 如何创建只有两个订阅者的 JMS 目标?

c# - 用于 MQ SSL 测试的测试 SSL 证书

ibm-mq - 在 InQueue 上实现选择器

r - 如何修复R的错误 “missing value where TRUE/FALSE needed”?

language-agnostic - 检查错误的首选方式

java - 用于组广播的 JMS 架构

java - JMSClient(在某些情况下也是生产者)是否接收它自己发送的消息

c++ - strcpy 不适用于相同大小的数组

Spring 如何处理 Restful API 的 "Request method POST not supported"错误

.net - 如何写入 MQMessage : multiple Write calls, 或 StringBuilder?