Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mass Transit with Quartz / Scheduling - Are there any example implementations out there?

I have searched high and low for an example implementation or blog post on how to use Mass Transit's Quartz integration (https://github.com/MassTransit/MassTransit-Quartz).

At the moment I have to make do with just looking at the unit tests that come with the code base and I'm not making much progress.

Are there any examples or good blog posts out there to help me get started with Mass Transit and Quartz Scheduling?

like image 266
Mark Robinson Avatar asked Dec 03 '13 15:12

Mark Robinson


1 Answers

This example lets you persist a MassTransit scheuled message in a SQL database. Out of the box, MassTransit only persists in memory without some config changes.

First of all you need a subtle change to your app/web.config file to include the following 2 blocks:

<configSections>
<section name="quartz" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />

 <quartz>
<add key="quartz.scheduler.instanceName" value="MassTransit-Quartz" />
<add key="quartz.scheduler.instanceId" value="AUTO" />
<add key="quartz.threadPool.type" value="Quartz.Simpl.SimpleThreadPool, Quartz" />
<add key="quartz.threadPool.threadCount" value="4" />
<add key="quartz.threadPool.threadPriority" value="2" />
<add key="quartz.jobStore.misfireThreshold" value="60000" />

<add key="quartz.jobStore.type" value="Quartz.Impl.AdoJobStore.JobStoreTX, Quartz" />
<add key="quartz.jobStore.useProperties" value="false" />

<add key="quartz.jobStore.driverDelegateType" value="Quartz.Impl.AdoJobStore.SqlServerDelegate, Quartz" />
<add key="quartz.jobStore.clustered" value="true" />
<add key="quartz.jobStore.tablePrefix" value="QRTZ_" />
<add key="quartz.jobStore.dataSource" value="quartzDS" />

<add key="quartz.dataSource.quartzDS.connectionString" value="Server=(local);Database=Quartz;Integrated Security=SSPI" />
<add key="quartz.dataSource.quartzDS.provider" value="SqlServer-20" />

Then, in your local SQL, create a new database named "Quartz", download the quartz.net source and locate the database script

"tables_sqlServer.sql"

run this against your Quartz local database to create the schema. Now you have everything ready to persist scheduled messages in the database, you need to subscribe these two consumers from the MassTransit Quartz integration library:

var scheduler = CreateScheduler();      
sb.SubscribeConsumer(() => new ScheduleMessageConsumer(scheduler));
sb.SubscribeConsumer(() => new CancelScheduledMessageConsumer(scheduler));

Where scheduler is an IScheduler:

static IScheduler CreateScheduler()
{
    ISchedulerFactory schedulerFactory = new StdSchedulerFactory();
    return schedulerFactory.GetScheduler();
}

and sb is your servicebus of type IServiceBus.

Finally, in your code call:

 Bus.ScheduleMessage(SchedulePeriodInSecondsFromNow, MessageToSchedule); 

And have a consumer for the "MessageToSchedule" type. If you open up the database and query the QRTZ_TRIGGERS table, you will see jobs appearing there and in QRTZ_JOB_DETAILS.

Hope this helps!

like image 163
Paul Avatar answered Nov 04 '22 00:11

Paul