views:

173

answers:

1

Hi,

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();
}
+3  A: 

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."

Jeff Sternal
Alternativly add processed and messages with error to a special validation failed queue.
Sascha
Hmm, nice remarks. But I'm not rolling back the operation manually, I'm validating the message inside of a using{} scope, so if an error occurs, I guess it's default behavior to rollback... isn't it?
born to hula
@Sascha - agreed, if you just need to retry once. I've updated my answer to include that suggestion. @born - could you update your answer with some calling code? It may just be a matter of expanding your using to a `try ... catch ... finally` and manually 'rolling back' (that is, re-queueing) inside the `catch` block.
Jeff Sternal