JasperFx 0.9.14


Next

Messaging Transports

Previous

Messaging

Configuring the Service Bus Edit on GitHub


The message bus capabilities of Jasper can be configured in a mix of ways:

  1. Through your application configuration file
  2. Directly through the JasperOptions model
  3. JasperRegistry, which ultimately are just fluent interface wrappers to just configure the underlying JasperOptions model

Using Your Configuration File

If you want to, you can completely configure the JasperOptions model in your appsettings.json file with a section called Jasper that would look like this:

{
  "Jasper":{
    "HostedServicesEnabled": true,
    "DisableAllTransports": true,
    "ThrowOnValidationErrors": false,
    "Retries": {
      "Cooldown": "00:00:05",
      "FailuresBeforeCircuitBreaks": 3,
      "MaximumEnvelopeRetryStorage": 100,
      "RecoveryBatchSize": 100,
      "NodeReassignmentPollingTime": "00:01:00",
      "FirstNodeReassignmentExecution": "00:00:00"
    },
    "ScheduledJobs": {
      "FirstExecution": "00:00:00",
      "PollingTime": "00:00:10"
    },
    "MetricsCollectionSamplingInterval": "00:00:05",
    "MaximumLocalEnqueuedBackPressureThreshold": 10000,
    "BackPressurePollingInterval": "00:00:02",
    "PersistDeadLetterEnvelopes": true,
    "Listeners": [
      "tcp://localhost:2000",
      "tcp://localhost:2001"
    ],
    "Subscriptions": [
      {
        "Scope": "All",
        "Uri": "tcp://localhost:2002",
        "ContentTypes": [
          "application/json"
        ],
        "Match": null
      },
      {
        "Scope": "Type",
        "Uri": "tcp://localhost:2004",
        "ContentTypes": [
          "application/json"
        ],
        "Match": "Jasper.Testing.Message1"
      }
    ]
  }
}

In the JSON section above, the primary sections you care about are:

  • The Listeners section is an array that tells Jasper where and how to listen for incoming messages through registered transports
  • The Subscriptions section allows you to make all necessary message subscriptions in configuration, but this is probably more easily accomplished by the strong typed routing configuration.

Programmatic Configuration

All system configuration for a Jasper application starts with the JasperRegistry or JasperRegistry classes. Underneath JasperRegistry are these sections that are specific to the messaging support:

  • JasperRegistry.Handlers - Configure policies about how message handlers are discovered, middleware is applied, and error handling policies. See Handling Received Messages for more information.
  • JasperRegistry.Publish - Optionally declare what messages or events are published by the Jasper system and any static publishing rules. See Routing Messages for more information.
  • JasperRegistry.Transports - Configure or disable the built in transports in Jasper. See Messaging Transports for more information.

Sample usages of each of these sections are shown below:


public class MyMessagingApp : JasperRegistry
{
    public MyMessagingApp()
    {
        // Configure handler policies
        Handlers.Retries.MaximumAttempts = 3;
        Handlers.Retries.Add(x => x.Handle<SqlException>().Reschedule(3.Seconds()));

        // Declare published messages
        Publish.Message<Message1>().To("tcp://server1:2222");

        // Configure the built in transports
        Transports.LightweightListenerAt(2233);
    }
}

Listen for Messages

You can direct Jasper to listen for incoming messages with the built in transports by just declaring the incoming port for the transport or by providing a Uri that expresses both the transport type and port number like this:


public class MyListeningApp : JasperRegistry
{
    public MyListeningApp()
    {
        // Use the simpler, but transport specific syntax
        // to just declare what port the transport should use
        // to listen for incoming messages
        Transports.LightweightListenerAt(2233);

        // or use a Uri to declare both the transport type and port
        Transports.ListenForMessagesFrom("tcp://localhost:2233");
    }
}

Other transport types like the forthcoming RabbitMq and Azure Service Bus transports will probably be configured strictly by using the Uri mechanism.

Advanced Configuration

If desirable, you can directly manipulate the JasperOptions model from your system's IConfiguration with this bootstrapping model:


var host = WebHost.CreateDefaultBuilder()
    .UseStartup<Startup>()
    .UseJasper(configure: (context, options) =>
    {
        // I'm not using it here, but you have access to
        // the ASP.Net Core HostingEnvironment
        var hosting = context.HostingEnvironment;

        // And the IConfiguration for your system
        var config = context.Configuration;

        // Add a transport listener at the Uri in
        // your configuration
        options.ListenForMessagesFrom(config["listener"]);

        // Add a subscription for a specific message type
        options.AddSubscription(Subscription.ForType<Message1>(config["outgoing"]));

        // Or add a subscription for all messages
        options.AddSubscription(Subscription.All(config["outgoing"]));
    })
    .Start();

or its equivalent with JasperRegistry:


public class ConfigUsingApp : JasperRegistry
{
    public ConfigUsingApp()
    {
        Settings.Messaging((context, options) =>
        {
            // I'm not using it here, but you have access to
            // the ASP.Net Core HostingEnvironment
            var hosting = context.HostingEnvironment;

            // And the IConfiguration for your system
            var config = context.Configuration;

            // Add a transport listener at the Uri in
            // your configuration
            options.ListenForMessagesFrom(config["listener"]);

            // Add a subscription for a specific message type
            options.AddSubscription(Subscription.ForType<Message1>(config["outgoing"]));

            // Or add a subscription for all messages
            options.AddSubscription(Subscription.All(config["outgoing"]));
        });
    }
}