MQ

MQ

Join this online group to communicate across IBM product users and experts by sharing advice and best practices with peers and staying up to date regarding product enhancements.

 View Only
  • 1.  IBM MQ delete a message from queue using XMS library

    Posted Mon August 29, 2022 10:28 AM

    IBM MQ Team,

    Greetings of the day.

    Please help on the below requirement:



    Requirement:

    1. We want to delete message from MQ only after it is processed successfully.

    2. Use event based message detection technique and avoid loop

    So, to achieve above:

    1. I have created message listener and consumer class below:

    sessionIn = connectionIn.CreateSession(false, AcknowledgeMode.ClientAcknowledge);

    // Create message listener and assign it to consumer
    messageListener = new MessageListener(OnMessageCallback);
    consumerAsync.MessageListener = messageListener;
    Console.WriteLine("Message Listener set. Starting the connection now.");

    // Start the connection to receive messages.
    connectionWMQ.Start();

    2. Reading the message from the call back event and push the message into other system:

    OnMessageCallback(Message) {

    if (xmsMessage is IBytesMessage)
    {
    IBytesMessage bytesMessage = (IBytesMessage)xmsMessage;
    byte[] arrayMessage = new byte[bytesMessage.BodyLength];
    bytesMessage.ReadBytes(arrayMessage);
    string message = System.Text.Encoding.Default.GetString(arrayMessage);
    }
    }

    Once the message processed, external system will fire the below over ride method:

    3. Response method override:

    protected override Task<bool> OnResponse(ReponseMessage message)
    {

     //Read the message and get the message id and correlation id.

    //Delete the message from the queue.

    //I am trying to do like this, but Its not working:

    messageConsumerDelete = sessionDelete.CreateConsumer(destinationDelete, query);
    if (messageConsumerDelete != null)
    {
    IMessage m = messageConsumerDelete.Receive(1000);
    LogWrite("Receive Message=" + m);
    m.Acknowledge();
    }
    }

    Please suggest a best solution for this requirement.

    I am trying to find a solution for this since weeks, but no breakthrough. 

    Thanks,
    Balaji



    ------------------------------
    Balaji Patil
    ------------------------------



  • 2.  RE: IBM MQ delete a message from queue using XMS library

    Posted Mon September 12, 2022 11:46 AM

    Hi Balaji

    Unless I am misunderstanding something significant, a far better approach might be use of transactions (syncpoint) and let MQ deal with the 'delete message when work completed' aspect for you.    This would usually be the way to acheive the kind of pattern you seem to be describing.

    I'm not that familiar with the XMS interfaces to be honest (someone else may be able to point you to an example/exact spelling) but I believe you would enable this via the MQGetMessageOptions class, and then Commit or Rollback (on the session object possibly?) when you know whether or not you want to permanently remove the message.

    Regards
    Anthony



    ------------------------------
    Anthony Beardsmore
    IBM MQ Development
    IBM
    ------------------------------



  • 3.  RE: IBM MQ delete a message from queue using XMS library

    Posted Tue September 13, 2022 05:04 AM

    Hi again - sorry to reply to myself but just for completeness...

    Having caught up with one of my colleagues who I gather you've been talking with directly, I understand more of the issues specific to your application now - we have one or two possible additional suggestions, but as this is very specific to your environment they probably aren't worth discussing on this thread!

    Regards

    Anthony



    ------------------------------
    Anthony Beardsmore
    IBM MQ Development
    IBM
    ------------------------------



  • 4.  RE: IBM MQ delete a message from queue using XMS library

    Posted Tue September 13, 2022 09:11 AM
    Hi Balaj,
    You've already been told you have the wrong approach.
    first you need to have something like:

    sessionIn = connectionIn.CreateSession(true, AcknowledgeMode.AUTO_ACKNOWLEDGE);

    // Create message listener and assign it to consumer
    messageListener = new MessageListener(OnMessageCallback);
    consumerAsync.MessageListener = messageListener;
    Console.WriteLine("Message Listener set. Starting the connection now.");

    // Start the connection to receive messages.
    connectionWMQ.Start();

    It is debatable whether or not you want to pass the session into the message listener. Note that the OnMessage method should be transacted, which ever way you make that happen in .NET.

    OnMessageCallback(Message) {

    if (xmsMessage is IBytesMessage)
    {
    IBytesMessage bytesMessage = (IBytesMessage)xmsMessage;
    byte[] arrayMessage = new byte[bytesMessage.BodyLength];
    bytesMessage.ReadBytes(arrayMessage);
    string message = System.Text.Encoding.Default.GetString(arrayMessage);
    # call here the part that works with the message (part 3?). If successful (no exceptions thrown) the transaction needs to be committed. If an exception is thrown, you may have to rethrow as the appropriate exception to roll back the transaction.
    } else {
    #force rollback of the session by throwing the appropriate exception
    }

    Hope it helps
    ​​​​​

    ------------------------------
    Francois Brandelik
    ------------------------------