8

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?

Mark Robinson
  • 13,128
  • 13
  • 63
  • 81
  • 2
    4 years later and I cannot find a single decent example either. I have messages being scheduled and making into the Quartz queue where it remaings "running" but my consumers never ack the message or perform work. – Jacob Barnes Oct 06 '17 at 16:05
  • did you find the solution?! – Mazdak Apr 22 '19 at 18:35

1 Answers1

7

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!

Paul
  • 1,483
  • 14
  • 32
  • 1
    Is there another step that's missing to get the messages from the db back to MassTransit again? If I'm correct, the ScheduleMessageConsumer translates messages to Quartz and saves them, but it seems like there should be something else to process them. I see there's a MassTransitJobFactory in GitHub but I can't seem to set it up correctly. – mikebridge Jun 13 '14 at 21:34
  • 1
    MikeBridge - Its been a while since I've looked at this, apologies for the delay. In my implementation I ended up not using MassTransit Quartz integration library as I needed repeatable jobs which isn't or wasn't supported by MassTransit. The next part may answer your question though - I had to implement "IJob", an empty constructor, and public void Execute(IJobExecutionContext context). – Paul Aug 06 '14 at 07:24