0
votes

I get an Exception while connecting to MQQueueManager. The Exception reads - MQException: MQJE001: An MQException occurred: Completion Code 2, Reason 2009 MQJE016: MQ queue manager closed channel immediately during connect Closure reason = 2009. Below is my code to push a message to queue.

@SuppressWarnings("unchecked")
public boolean postMessage(String mqMessage, String correlationId) throws TradeException, MQException {
    String logStr = TradeUtil.getLoggerPrefix(this.getClass().getSimpleName(),
            Thread.currentThread().getStackTrace()[1].getMethodName(), "");
    boolean result = false;

    MQQueueManager queueManager = null;

    try {
        
        MQEnvironment.hostname = ConfigFileLoader.getProperty("MQ_HOSTNAME");
        MQEnvironment.channel = ConfigFileLoader.getProperty("MQ_CHANNEL");
        MQEnvironment.port = Integer.parseInt(ConfigFileLoader.getProperty("MQ_PORT"));

        tradeLogger.info(logStr + "MQ HOST NAME : " + MQEnvironment.hostname + " MQ CHANNEL : "
                + MQEnvironment.channel + " MQ PORT : " + MQEnvironment.port);
        try {
            queueManager = new MQQueueManager(ConfigFileLoader.getProperty("MQ_QUEUE_MANAGER"));
        } catch (MQException e) {
            tradeLogger.info("queueManager is Busy, try after some time");
            Thread.sleep(1000 * 60);
            if(maxCount < 3) {
                maxCount += 1;
                postMessage(mqMessage, correlationId);  
            }
        }
        
        tradeLogger.info(logStr + "MQhostname : " + queueManager + ConfigFileLoader.getProperty("MQ_QUEUE_MANAGER"));

        int openOptions = MQC.MQOO_OUTPUT + MQC.MQOO_INPUT_AS_Q_DEF;
        MQQueue queue = queueManager.accessQueue(ConfigFileLoader.getProperty("MQ_INPUT_QUEUE_NAME"), openOptions);
        tradeLogger.info("INPUT QUEUE NAME : " + queue + ConfigFileLoader.getProperty("MQ_INPUT_QUEUE_NAME"));

        if (queueManager.isConnected()) {
            tradeLogger.info("queue manager is connected!");
            MQPutMessageOptions mqPutMessageOptions = new MQPutMessageOptions();
            MQMessage message = new MQMessage();
            message.format = MQC.MQFMT_STRING;
            message.correlationId = correlationId.getBytes();
            message.writeString(mqMessage);
            queue.put(message, mqPutMessageOptions);

            tradeLogger.info(logStr + "POSTED MESSAGE : " + mqMessage);
            result = true;

            queue.close();

            tradeLogger.info("queue is closed.");

        } else {
            tradeLogger.error("unable to connect to Queue");
            throw new TradeException("Unable to connect to Queue");
        }

    } catch (MQException e) {
        tradeLogger.error(logStr + " MQException : " + e.getMessage());
        JSONObject errorJSON = new JSONObject();
        errorJSON.put("errorDesc", e);
        throw new TradeException(ResponseStatus.failure, 404,
                "Unable to post message in MQ. Please try after sometime.", errorJSON);
    } catch (Exception e) {
        tradeLogger.error(logStr + " Exception : " + e.getMessage());
        JSONObject errorJSON = new JSONObject();
        errorJSON.put("errorDesc", e);
        throw new TradeException(ResponseStatus.failure, 404,
                "Unable to post message in MQ due to some techical error. Please contact administrator.",
                errorJSON);
    } finally {
        if (null != queueManager) {
            queueManager.disconnect();
            tradeLogger.info("queueManager is disconnected!");
        }
    }
    return result;
}

This issue gets resolved after manually restarting the Queue Manager. Also, I read somewhere that MQQueueManager() is synchronous. Is it possible that other Threads might be accessing this queue manager to establish connection which results in this exception?

2
MQQueueManager() creates a connection to queue manager and yes, it is synchronous call. Do you have a requirement connect to queue manager everytime you want to put a message? It's not a good practice unless you are putting//getting messages infrequently. It is a good practice to create connection once and use it everytime you put/get messages and close connection once you are done with put/gets - Shashi
@Shashi - yes, it is required to put/get message from Source and Destination queue. A scheduler is there which will create a new connection every 2 mins to check for queue depth > 0 to pull messages. - user13050995
Thanks @Shashi - After proper handling of closing MQ manager, problem was resolved. I wasn't closing queue manager after it is done pulling messages from destination queue. - user13050995

2 Answers

0
votes

This is not an answer to 2009 you are getting. I have simplified version of your code

@SuppressWarnings("unchecked")
public boolean postMessage(String mqMessage, String correlationId) throws MQException {
    boolean result = false;

    MQQueueManager queueManager = null;
    MQQueue queue = null;
    MQEnvironment.hostname = "localhost";
    MQEnvironment.channel = "SVRCONN_CHN";
    MQEnvironment.port = 1414;

    try {
        queueManager = new MQQueueManager("QM1");
        int openOptions = MQC.MQOO_OUTPUT; // Just open for putting message
        queue = queueManager.accessQueue("DESTQ", openOptions);
        MQPutMessageOptions mqPutMessageOptions = new MQPutMessageOptions();
        MQMessage message = new MQMessage();
        message.format = MQC.MQFMT_STRING;
        message.correlationId = correlationId.getBytes();
        message.writeString(mqMessage);
        queue.put(message, mqPutMessageOptions);
        result = true;
    } catch (MQException e) {
        e.printStackTrace();                
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        if (queue != null)
            queue.close();

        if (queueManager != null)
            queueManager.disconnect(); // Call disconnect
    }
    return result;
}
0
votes

First off, do not use the MQEnvironment class as it is NOT thread safe. I wish IBM would deprecate that class. You should put your connection information in a Hashtable and pass the Hashtable to the MQQueueManager object.

Java sample:

Hashtable<String, Object> mqht = new Hashtable<String, Object>();

mqht.put(CMQC.CHANNEL_PROPERTY, ConfigFileLoader.getProperty("MQ_CHANNEL"));
mqht.put(CMQC.HOST_NAME_PROPERTY, ConfigFileLoader.getProperty("MQ_HOSTNAME"));
mqht.put(CMQC.PORT_PROPERTY, Integer.parseInt(ConfigFileLoader.getProperty("MQ_PORT"));
mqht.put(CMQC.USER_ID_PROPERTY, ConfigFileLoader.getProperty("MQ_USERID")));
mqht.put(CMQC.PASSWORD_PROPERTY, ConfigFileLoader.getProperty("MQ_PASSWORD")));

queueManager = new MQQueueManager(ConfigFileLoader.getProperty("MQ_QUEUE_MANAGER"), mqht);

C# .NET sample:

Hashtable mqht = new Hashtable();

mqht.Add(MQC.CHANNEL_PROPERTY, ConfigFileLoader.getProperty("MQ_CHANNEL"));
mqht.Add(MQC.HOST_NAME_PROPERTY, ConfigFileLoader.getProperty("MQ_HOSTNAME"));
mqht.Add(MQC.PORT_PROPERTY, System.Int32.Parse(ConfigFileLoader.getProperty("MQ_PORT"));
mqht.Add(MQC.USER_ID_PROPERTY, ConfigFileLoader.getProperty("MQ_USERID")));
mqht.Add(MQC.PASSWORD_PROPERTY, ConfigFileLoader.getProperty("MQ_PASSWORD")));

mqht.Add(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_MANAGED);

queueManager = new MQQueueManager(ConfigFileLoader.getProperty("MQ_QUEUE_MANAGER"), mqht);

Also, for proper security, you should be setting a UserId and Password for your connection to the queue manager.

Finally, make sure you close any opened queues as Shashi has noted in his code sample.