JasperFx 0.5.0


Next

Messaging Transports

Previous

Messaging

Configuring the Service Bus Edit on GitHub


All system configuration for a Jasper application starts with the JasperRegistry class. 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.Subscribe - Register as a subscriber for messages from other systems. See Dynamic Subscriptions 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.DefaultMaximumAttempts = 3;
        Handlers.OnException<SqlException>().RetryLater(3.Seconds());

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

        // Register to receive messages
        Subscribe.At("tcp://loadbalancer1:2233");
        Subscribe.To<Message2>();
        Subscribe.To(type => type.IsInNamespace("MyMessagingApp.Incoming"));

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

Using Strong Typed Configuration

Messaging can also take advantage of Jasper's support for strong typed configuration like this:


public class MySettings
{
    public int LightweightPort { get; set; } = 2222;
    public Uri DefaultListener { get; set; } = "durable://localhost:2223".ToUri();

    public int MaximumSendAttempts { get; set; } = 5;
}

public class MyAppWithSettings : JasperRegistry
{
    public MyAppWithSettings()
    {
        Configuration
            .AddEnvironmentVariables()
            .AddJsonFile("appSettings.json");

        Settings.With<MySettings>(_ =>
        {
            Transports.LightweightListenerAt(_.LightweightPort);

            Transports.ListenForMessagesFrom(_.DefaultListener);

        });
    }
}

Uri Lookups and Aliasing

Another way to configure the service bus is to use Uri aliasing that allows you to just make the configuration with "stand in" Uri's that are resolved later to the real value. So far, the only mechanism built into the core Jasper framework will pull the real values from the underlying IConfigurationRoot for the application.

See this example below:


public class MyAppUsingUriLookups : JasperRegistry
{
    public MyAppUsingUriLookups()
    {
        Configuration
            .AddInMemoryCollection(new Dictionary<string, string>{{"incoming", "tcp://server3:2000"}});

        // This usage assumes that there is a value in the configuration
        // with the key "incoming" that corresponds to a Uri
        Transports.ListenForMessagesFrom("config://incoming");
    }
}

Behind the scenes, the value for config://incoming is interpreted by Jasper as the Uri string stored in configuration with the key "incoming," or in code terms, IConfigurationRoot.GetValue<string>("incoming")

There is also an addon Uri lookup that uses Consul. See Jasper.Consul for more information.

The Uri aliases are respected anywhere where Uri's are accepted as arguments in JasperRegistry or when explicitly specifying the destination of a message being sent through the messaging.