Skip to content

CrystalQuartz OWIN Configuration

guryanovev edited this page Jan 17, 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
  2. Configuration
  3. Running
  4. Examples

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

As a next step, you need to hook CrystalQuartz middleware into your OWIN environment. It should be done by calling UseCrystalQuartz extension method at the moment of pipeline initialization, usualy it is in Startup.cs file. The generic syntax for panel configuration looks like this:

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

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

This section describes different ways of passing schedulerOrProvider argument to the UseCrystalQuartz method. You need to pick one of these options.

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)
{
    string url = "tcp://localhost:555/QuartzScheduler"; // YOUR URL HERE

    NameValueCollection properties = new NameValueCollection();
    properties["quartz.scheduler.proxy"] = "true";
    properties["quartz.scheduler.proxy.address"] = url; 

    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:

    public class MySchedulerProvider : StdSchedulerProvider
    {
        protected override void InitScheduler(IScheduler scheduler)
        {
            var jobDetail = JobBuilder.Create<HelloJob>()
                .WithIdentity("myJob")
                .StoreDurably()
                .Build();
    
            var trigger = TriggerBuilder.Create()
                .WithIdentity("myTrigger")
                .StartNow()
                .WithSimpleSchedule(x => x.WithIntervalInMinutes(1).RepeatForever())
                .Build();
    
            scheduler.ScheduleJob(jobDetail, trigger);
        }
    }

    In this case you can pass the same provider instance to the UseCrystalQuartz call:

    public void Configuration(IAppBuilder app)
    {
        /* ... */
        app.UseCrystalQuartz(new MySchedulerProvider());
    }
  • 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:

    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.UseCrystalQuartz(new RemoteSchedulerProvider
            {
                SchedulerHost = "tcp://localhost:555/QuartzScheduler"
            });
        }
    }

2.2 Options

Second (optional) argument of UseCrystalQuartz method allows to do some panel customizations. The object should be an instance of CrystalQuartzOptions class:

public class CrystalQuartzOptions
{
    public string Path { get; set; }

    public string CustomCssUrl { get; set; }
}

Usage could be like this:

public void Configuration(IAppBuilder app)
{
    app.UseCrystalQuartz(
        new RemoteSchedulerProvider
        {
            SchedulerHost = "tcp://localhost:555/QuartzScheduler"
        },
        new CrystalQuartzOptions
        {
            CustomCssUrl = "...",
            Path = "..."
        });
}
  • Path is a url component for the panel. Default is /quartz
  • CustomCssUrl - a valid url (absolute or relative) to load additional css styles to the panel.

3 Running the panel

Once the configuration is done, you can run your project and navigate to http://YOUR_URL/quartz to see the panel UI.

4 Examples

Please check these working samples demonstrating different cases of using CrystalQuartz with OWIN environment: