简体   繁体   中英

MSMQ - Messages being processed over and over

We're using MSMQ and an integration mechanism which processes the messages. This mechanism analyzes and validates each message under a transactional context and if validation fails, a rollback occurs and the message is delivered back to the queue. Also, the integration mechanism waits for 20 seconds to process error messsages again.

Problem is that this approach is causing the error messages to be processed over and over, even if we clean the queue. We also tried cleaning the cache, but that showed no results either.

Does anyone have a clue?

Updated with calling code

using (TransactionScope ts = new TransactionScope(TransactionScopeOption.RequiresNew))
{
    //message validation function
    servicoIntegracao.Validar(identificadorMensagem, mensagem.Substring(_tamanhoCampoTipoEvento));
    servicoIntegracao.ExecutarServico();
    AtualizarStatusEventoNegocio(identificadorMensagem, Status.Finalizado);
    retorno = 0;
    ts.Complete();
}

Instead of rolling back the read operation, you should update the message and re-queue it.

The first thing you should do is create a failed validation queue for messages that you don't want to process again. (It's useful to keep the messages around to research problems, and a queue is a natural place.)

Next, if you only want to retry it once, you can write it to a retry queue and modify your validation process so that it sends failures originating from the retry queue to the failed validation queue .

If you want to be able to retry validation more than once, you should modify the message format itself to include the number of tries and increment that number each time your process re-queues a message.

Once a message reaches the maximum allowed tries, your process can send it to the failed validation queue .

With MSMQ you don't necessarily have to modify the message format: you can use Message.Extension to store the number of tries, though it's generally frowned upon - as it says in that property's documentation: "Where possible, you should include message data in the Body property of the message rather than the Extension property."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM