Message Storage Management


From the Command Line

As of Jasper v0.9.5, Jasper comes with a built in command for adminstering database backed persistence. Assuming that you're using Jasper's command line support, you have the command storage with several options.

At the command line in the root of your application, you can rebuild the message storage schema objects with:

dotnet run -- storage rebuild

You can also query the current counts of persisted input, output, and scheduled messages with:

dotnet run -- storage counts

You can dump the SQL to create the necessary database objects to a file for usage in database migration scripts with:

dotnet run -- storage script --file SomeFilePath.sql

And lastly, if you just want to clear out any persisted incoming, outgoing, or scheduled messages in your application's database, use:

dotnet run -- storage clear

Message Storage in Testing

Let's say that we're all good developers who invest in automated testing of our applications. Now, let's say that we're building a Jasper application that uses Sql Server backed message persistence like so:


public class MyJasperApp : JasperOptions
{
    public override void Configure(IHostEnvironment hosting, IConfiguration config)
    {
        // Enables Sql Server-backed message persistence using
        // a connection string from your application's appsettings.json
        Extensions.PersistMessagesWithSqlServer(config.GetConnectionString("sqlserver"));


        // If running in development mode, just rebuild the
        // message database storage objects on application startup
        if (hosting.IsDevelopment())
        {
            Advanced.StorageProvisioning = StorageProvisioning.Rebuild;
        }
    }
}

If we write integration tests for our application above, we need to guarantee that as part of the test setup the necessary Sql Server schema objects have been created in our test database before we run any tests. If you notice in the code above, there's a property called JasperOptions.Advanced.StorageProvisioning that is defaulted to None, but can be overridden to either Clear to delete any persisted messages on application startup or Rebuild to completely drop and rebuild all message persistence storage objects in the database upon application startup.

In addition to the StorageProvisioning property, there is also an extension method hanging off of IJasperHost called RebuildMessageSchema() that will completely rebuild all the necessary schema objects for message persistence. Below is an example of using an xUnit shared fixture approach for integration tests of the MyJasperApp application.


public class MyJasperAppFixture : IDisposable
{
    public MyJasperAppFixture()
    {
        Host = JasperHost.For<MyJasperApp>();

        // This extension method will blow away any existing
        // schema items for message persistence in your configured
        // database and then rebuilds the message persistence objects
        // before the *first* integration test runs
        Host.RebuildMessageStorage();
    }

    public IHost Host { get;  }

    public void Dispose()
    {
        Host?.Dispose();
    }


}

// An xUnit test fixture that uses our MyJasperAppFixture
public class IntegrationTester : IClassFixture<MyJasperAppFixture>
{
    private readonly MyJasperAppFixture _fixture;

    public IntegrationTester(MyJasperAppFixture fixture)
    {
        _fixture = fixture;
    }
}