2017-03-02 223 views
0

我正在使用websphere版本9並使用示例程序進行訂閱以訂閱主題。以下是我的代碼。我已將工作單元引入樣本。IBM Websphere MQ - MQGET不會從隊列中刪除郵件

問題是 - 在每個MQGET並提交隊列中的消息不會被清除之後。

有什麼想法?這與創建隊列的方式有關嗎?持久,耐用等。?

MQSUB(Hcon,      /* connection handle   */ 
     &sd,      /* object descriptor for queue */ 
     &Hobj,      /* object handle (output)  */ 
     &Hsub,      /* object handle (output)  */ 
     &S_CompCode,    /* completion code    */ 
     &Reason);     /* reason code     */ 
    CompCode = S_CompCode;   /* use MQOPEN result for initial test */ 

    gmo.Options = MQGMO_WAIT   /* wait for new messages  */ 
       | MQGMO_SYNCPOINT /* transaction    */ 
       | MQGMO_CONVERT;  /* convert if necessary  */ 

    gmo.WaitInterval = MQWI_UNLIMITED; 
    while (CompCode != MQCC_FAILED) 
    { 
    buflen = sizeof(buffer) - 1; /* buffer size available for GET */ 
    memcpy(md.MsgId, MQMI_NONE, sizeof(md.MsgId)); 
    memcpy(md.CorrelId, MQCI_NONE, sizeof(md.CorrelId)); 
    md.Encoding  = MQENC_NATIVE; 
    md.CodedCharSetId = MQCCSI_Q_MGR; 

    /************************************************************************/ 
      /* Start a unit of work             */ 
    /************************************************************************/ 
    MQBEGIN (Hcon, &bo, &CompCode, &Reason); 

    MQGET(Hcon,    /* connection handle     */ 
      Hobj,    /* object handle      */ 
      &md,     /* message descriptor    */ 
      &gmo,    /* get message options    */ 
      buflen,    /* buffer length      */ 
      buffer,    /* message buffer     */ 
      &messlen,   /* message length     */ 
      &CompCode,   /* completion code     */ 
      &Reason);   /* reason code      */ 

    /****************************************************************/ 
    /* Display each message received        */ 
    /****************************************************************/ 
    if (CompCode != MQCC_FAILED) 
    { 
     buffer[messlen] = '\0';   /* add terminator   */ 
     char* strings[] = {buffer}; 
     bool client_commit_status = callback(strings); 
     if(client_commit_status){ 
      MQCMIT(Hcon, &CompCode, &Reason); 
      if (MQCC_OK != CompCode){ 
       MQBACK(Hcon, &CompCode, &Reason); 
      } 
     }else{ 
      MQBACK(Hcon, &CompCode, &Reason); 
     } 
    } 
+0

你怎麼知道'MQCMIT()'被調用?什麼是'callback()'返回? 'MQCMIT()'失敗了,事務被回滾了嗎?一些'printf()'語句可以打印出'client_commit_status'的值,並顯示是否調用'MQBACK()'可以清除它。 – mhawke

+0

我添加了'printf()'並確認MQCMIT正在被調用。我已經從發佈的代碼中刪除了它們,以減少行數。 – Muthukumar

+0

好的,你是否還證實'MQBACK()'不被調用? – mhawke

回答

0

我刪除了MQSUB調用,並用下面的代碼替換它。由於我的所有訂閱都是通過配置導向到目標隊列,所以我直接聆聽隊列。現在MQGET清除隊列。

if (strlen(target_queue_name)) { 
        strncpy(od.ObjectName, target_queue_name, MQ_Q_NAME_LENGTH); 
        MQOPEN(Hcon, &od, MQOO_INPUT_AS_Q_DEF | MQOO_FAIL_IF_QUIESCING | MQOO_INQUIRE, 
         &Hobj, &CompCode, &Reason); 
        if (CompCode != MQCC_OK) { 
          printf("MQOPEN ended with reason code %d\n", Reason); 
          return (int)Reason; 
        } 
     }