Invoking or Enqueuing a Message Locally Edit on GitHub


Assuming that your application has a message handler for a given message like InvoiceCreated in the sample below, you can consume and process that message inline with this code:


public Task Invoke(IMessageContext bus)
{
    var @event = new InvoiceCreated
    {
        Time = DateTime.UtcNow,
        Purchaser = "Guy Fieri",
        Amount = 112.34,
        Item = "Cookbook"
    };

    return bus.Invoke(@event);
}

The Invoke() method will not apply any error policies to retry the message if it fails and you will see exceptions bubble up on failures. This mechanism will process Cascading Messages if the message succeeds.

As an alternative, you can enqueue a message to a local queue to be handled later by the currently running node with IMessageContext.Enqueue():


public Task Enqueue(IMessageContext bus)
{
    var @event = new InvoiceCreated
    {
        Time = DateTime.UtcNow,
        Purchaser = "Guy Fieri",
        Amount = 112.34,
        Item = "Cookbook"
    };

    return bus.Enqueue(@event);
}

The InvoiceCreated message above will be enqueued locally using the application's rules for worker queue routing and durability. See Worker Queues and Message Priority for information about how to control these factors to give certain message types more or less priority and to tell Jasper which message types should be durable.

Today that pretty well means using the loopback transport, but before too long the Store and Forward Messaging will be usable locally as well for persistent queueing in this case. See this GitHub issue to track the progress.

Enqueue to a Specific Worker Queue

If you want to enqueue a message locally to a specific worker queue, you can use this syntax:


public Task EnqueueToQueue(IMessageContext bus)
{
    var @event = new InvoiceCreated
    {
        Time = DateTime.UtcNow,
        Purchaser = "Guy Fieri",
        Amount = 112.34,
        Item = "Cookbook"
    };

    // Put this message in a local worker
    // queue named 'highpriority'
    return bus.Enqueue(@event, "highpriority");
}

See Worker Queues and Message Priority for more information about worker queues.