添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I inherited this lovely bit of code below.

The way I read it the developer makes three assumptions:

  • An MQQueueManager instance is not necessarily created in a state where isConnected() returns true
  • If it is created in state isConnected() == false, the state might change "later", hence the timeout code
  • If you try to create an access queue from a disconnected MQQueueManager, it will not throw an exception.
  • What I would expect is that an MQQueueManager instance is created in state isConnected() == true, that this state might change later (network failure etc), and that this state change (isConnected() == false) would cause an operation on the queue to fail with an MQException.

    The documentation is delightfully silent on these points, except to note that the only way to reconnect to a queue after manually disconnecting the MQQueueManager is to create a new instance of MQQueueManager.

    Who can set me straight here?

    qMgr = new MQQueueManager( qManager );
    // Set up the options on the queue we wish to open...
    // Note. All WebSphere MQ Options are prefixed with MQC in Java.
    final int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT;
    // Now specify the queue that we wish to open,
    // and the open options...
    queue = qMgr.accessQueue( queueName, openOptions );
    // Set the get message options...
    final MQGetMessageOptions gmo = new MQGetMessageOptions(); // accept the
    // defaults
    gmo.options = MQC.MQGMO_WAIT;
    gmo.waitInterval = 1000;
    connectionStatus = CONNECTING;
    int timeOutCounter = 0;
    while(!qMgr.isConnected()) {
        InboundMsgTask.sleep(1000);
        timeOutCounter++;
        if(timeOutCounter > 4) {
            connectionStatus = TIME_OUT;
            return;
    connectionStatus = CONNECTED;
    

    Instead of checking the IsConnected==True, it is better to go ahead make the actual MQ .NET method call (Get, Put etc). If the connection is broken these calls would throw a connection broken execption (MQRC 2009). Remember the IsConnected could be True before a MQ method is called but it can change during the execution of a MQ method. Your code needs to handle the connection broken exception and call the MQQueueManager.Disconnect method and then re-establish the connection. The Disconnect call would free up any resources allocated and close all gracefully any queue manager objects that were opened. Ignore any exception thrown by the Disconnect method.

    If you are using MQ v7.1 or v7.5, then the .NET client can automatically reconnect to queue manager if it detects connection errors. You will need to enable the automatic reconnect option. Please see the MQ InfoCenter.

    EDIT: A new MQQueueManager() will return an instance of MQQueueManager class if connection to queue manager is successfully established. In case of errors, a MQExceptionwill be thrown. There is no need to wait for connection to complete as MQQueueManager constructor is a blocking call.

    Thanks. But this does not answer part of my question, which is basically "will the = newMQConnectionManager(name) return an MQConnectionManager that is connection, or will it sometimes be necessary to wait for the connection to complete". Or more directly: Is the new MQConnectionManager call blocking until connection is established or not? – Anders Johansen Oct 10, 2013 at 13:23

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.