Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Next, implement your batch processing method. It is important to note that the message handling behavior within batch processing differs from the regular ProcessMessageAsync method.You

If you are processing the entire batch of messages within one method (for example, sending the entire batch to an external service), then you can follow the same pattern as the non-batch ProcessMessageAsync method. Exceptions thrown from this method will be passed to the OnBatchError method and you can choose to either retry the entire batch, or, error the entire batch.

However, if you are processing messages individually, or, in smaller batches, you must update each individual message context within a batch. You must mark each message context as:

...

Code Block
languagec#
public async Task ProcessMessagesAsync(IMessageBatchContext context, CancellationToken token)
{
  // iterate through each message context and process the message. Remember that
  // you should be handling exceptions within your loop and setting each message's
  // state. If you throw an exception out of this method, then the OnBatchError method
  // will be called.
  await context.Contexts.ForEachAsync(async messageContext =>
  {
    if(token.IsCancellationRequested)
      return;
        
    try
    {
      // your business logic here...
      await Task.Delay(500, token).ConfigureAwait(false);          
           
      // ** you *must* mark your context as successfully processed **
      messageContext.BatchProcessingSuccess = true;
    }
    catch(OperationCanceledException)
    {
	  // exit when the device/channel is stopped
      return;
    }
    catch(Exception ex)
    {
      // message moved to the error queue when you add an error event
      messageContext.WriteEvent(EventSeverity.Error, ex);
    }
  }, 10).ConfigureAwait(false);
}

You should not throw exceptions out of the BatchProcessMessages method, however, if you do, there is an OnBatchError method which will be called. Within this method you can choose to retry the entire batch, or, error any messages in the batch which are not processed/errored/filtered. If you are sending the entire batch of messages to an external service, then you can throw an exception from ProcessMessagesAsync and set the ShouldRetry property in the OnBatchError method to true. This will cause the entire batch to be retried.

Code Block
languagec#
// Called when an exception is thrown from the ProcessMessagesAsync method


Code Block
languagec#
// Called when an exception is thrown from the ProcessMessagesAsync method.
// *This only applies when you are processing the entire batch in one go. If you
// *are processing messages within the batch individually (or in smaller batches,
// *you need to handle exceptions within the ProcessMessagesAsync method.
public override void OnBatchError(IMessageBatchContext context, Connexion.Core.ErrorEventArgs args)
{
  if(args.Exception is OperationCanceledException)
    return;	// stopped device/channel
  
  args.ShouldRetry = true;
  
  args.SleepTime = TimeSpan.FromMinutes(2);
  // this error is displayed over your device
  Logger.Write(EventSeverity.Error, 54321, args.Exception.Message);
}

...