Skip to content

CrystalQuartz OWIN Configuration

guryanovev edited this page Jan 11, 2017 · 18 revisions

This article describes how you can use OWIN version of CrystalQuartz to plug Quartz.NET Scheduler viewer into your application.

1. Setup

As a first step, you need to install CrystalQuartz.Owin NuGet package to the target project:

Install-Package CrystalQuartz.Owin

That will add a CrystalQuartz.Owin.dll reference to your project. The CrystalQuartz.Owin.dll is a single dll that contains all the resources needed for CrystalQuartz to work.

2. Configuration

The generic syntax for panel configuration looks like this:

/*
 * app is your IAppBuilder instance
 */
app.UseCrystalQuartz(schedulerOrProvider, options);

The UseCrystalQuartz is an extension method that adds panel middleware to your environment. The arguments are:

  • schedulerOrProvider is a direct IScheduler reference or a provider poining to it;
  • options is an optional for panel customization.

2.1 Configuration - Scheduler

2.1.1 Scheduler instance

If you already have the IScheduler object instance then you can pass it directly to the configuration extension method:

public void Configuration(IAppBuilder app)
{
    IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();

    // define the job and tie it to our HelloJob class
    IJobDetail job = JobBuilder.Create<HelloJob>()
        .WithIdentity("job1", "group1")
        .Build();

    // Trigger the job to run now, and then repeat every 10 seconds
    ITrigger trigger = TriggerBuilder.Create()
        .WithIdentity("trigger1", "group1")
        .StartNow()
        .WithSimpleSchedule(x => x
        .WithIntervalInSeconds(10)
        .RepeatForever())
        .Build();

    // Tell quartz to schedule the job using our trigger
    scheduler.ScheduleJob(job, trigger);

    scheduler.Start();

    /*
     * Init CrystalQuartz Panel with scheduler instance.
     */
    app.UseCrystalQuartz(scheduler);
}

Using this technic you can manually configure panel to work with a remote scheduler:

public void Configuration(IAppBuilder app)
{
    NameValueCollection properties = new NameValueCollection();
    properties["quartz.scheduler.proxy"] = "true";
    properties["quartz.scheduler.proxy.address"] = "tcp://localhost:555/QuartzScheduler"; // YOUR URL HERE

    ISchedulerFactory schedulerFactory = new StdSchedulerFactory(properties);
    IScheduler scheduler = schedulerFactory.GetScheduler();

    /*
     * Init CrystalQuartz Panel with Remote scheduler instance.
     */
    app.UseCrystalQuartz(scheduler);
}

2.1.2 Scheduler Func provider

Depending on your application lifecycle, you might have no IScheduler instance ready at the moment of OWIN environment setup. For this case you can use a Func<IScheduler> instance as a first argument of UseCrystalQuartz call:

public void Configuration(IAppBuilder app)
{
    app.UseCrystalQuartz(() => GetSchedulerInstance());
}

For example, you can get it from an IoC container:

public void Configuration(IAppBuilder app)
{
    /* ... */
    app.UseCrystalQuartz(() => container.Resolve<IScheduler>());
}

2.1.3 Legacy Scheduler provider

Non-OWIN version of CrystalQuartz Panel uses internal ISchedulerProvider abstraction for getting access to the scheduler instance. You can pass an instance of ISchedulerProvider as a first argument of UseCrystalQuartz call. That might be helpful in some cases, for example:

  • you are migrating from CrystalQuartz.Simple to CrystalQuartz.Owin and you already have your custom implementation of ISchedulerProvider. In this case you can pass the same provider instance.

  • you are going to connect to a remote scheduler. In this case the RemoteSchedulerProvider implementation of ISchedulerProvider would be more concise than the explicit configuration.

2.2 Options

[tbd]

Clone this wiki locally