消费者:在队列管理器断开连接时使用甚至提交的消息和同步点

时间:2018-04-11 21:39:22

标签: c# ibm-mq

在这种情况下,消息会丢失:

1消费者从队列中读取消息,

在提交之前

2,队列管理器断开连接或崩溃

3消息不再在队列中。

应该在这种情况下提交抛出异常吗?因为客户端可能在调用Commit之前崩溃。

实施它的正确方法是什么?

 Open(ConnectionMode.Read);
    var message = GetMessage();

     //process other works when queue manager is disconnected

    queueManager.Commit(); //this still runs successfully                
    queue.Close();           
    queueManager.Disconnect();


  public string GetMessage()
    {
        const int MaxMessageSizeInBytes = 8000;
        const int ReadTimeoutInMilliseconds = 3000;

        message = new MQMessage { Format = MQC.MQFMT_STRING };
        var mqMessageOptions = new MQGetMessageOptions
        {
            WaitInterval = ReadTimeoutInMilliseconds,
            Options = MQC.MQGMO_WAIT | MQC.MQGMO_FAIL_IF_QUIESCING
            | MQC.MQGMO_SYNCPOINT  //use syncpoint for consumer acknowledgment
        };

        var content = message.ReadString(message.MessageLength);
        message.ClearMessage();

        return content;
    }

    public IMessage Get()
    {
        var mqMessage = new MQMessage { Format = MQC.MQFMT_STRING };
        var mqMessageOptions = new MQGetMessageOptions
        {
            WaitInterval = ReadTimeoutInMilliseconds,
            Options = MQC.MQGMO_WAIT | MQC.MQGMO_FAIL_IF_QUIESCING | MQC.MQGMO_SYNCPOINT
        };

        Connection.MqQueue.Get(mqMessage, mqMessageOptions, MaxMessageSizeInBytes);

        return mqMessage.MapToMessage();
    }


    public void Open(connectionMode connectionMode)
    {
        var connectionSettings = new Hashtable
        {
            {MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_MANAGED },
            {MQC.CONNECT_OPTIONS_PROPERTY, MQC.MQCNO_RECONNECT }
        };

        int openOptions = 0;

        switch (connectionMode)
        {
            case connectionMode.Read:
                openOptions = MQC.MQOO_INPUT_SHARED + MQC.MQOO_FAIL_IF_QUIESCING;
                break;
            case connectionMode.Write:
                openOptions = MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING;
                break;
        }


        MqQueueManager = new MQQueueManager(_queueManagerName, connectionSettings);
        MqQueue = MqQueueManager.AccessQueue(_queueName, openOptions);
    }

Windows上的MQ Server V8

.NET客户端:8.0.0.5

1 个答案:

答案 0 :(得分:0)

  在提交之前

2,队列管理器断开连接或崩溃

请描述一下你是如何模拟的。您刚刚从客户端服务器/ PC /笔记本电脑上拔下网线吗?你等了多久?默认TCP超时为2小时。队列管理器不知道客户端已经消失,直到TCP超时期限到期。之后,队列管理器应该再次使消息可用。