Coder Social home page Coder Social logo

quartz-with-asp.net-core's Introduction

Quartz-with-ASP.Net-Core

This project is an example of what Quartz.Net framework is and how can it be used in ASP.NET Core Web application

Getting Started

Before you proceed further, please make sure you've basic understanding of how ASP.NET Core MVC web application works. Also you need to have some basic idea about Quartz.Net framework. You can find useful information from the following link. You can also refer to these videos to see things in action. We will be using a nuget package offered by Quartz called Quartz

Getting Started with ASP.NET Core and Quartz.Net

Step 1

Download Quartz nuget package into the project

Step 2

Now we need to configure Quartz within our Startup.cs class. To do that, we will be creating a new instance of SchedulerFactory and then registering it so that it can be accessed across the application through Dependency Injection

    private IScheduler GetScheduler()
        {
            var properties = new NameValueCollection
            {
                ["quartz.scheduler.instanceName"] = "QuartzWithCore",
                ["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool, Quartz",
                ["quartz.threadPool.threadCount"] = "3",
                ["quartz.jobStore.type"] = "Quartz.Simpl.RAMJobStore, Quartz",
            };
            var schedulerFactory = new StdSchedulerFactory();
            var scheduler = schedulerFactory.GetScheduler().Result;
            scheduler.Start();
            return scheduler;
        }

Once the instance is created, we will register it in ConfigureServices method in Startup.cs class

services.AddSingleton(provider => GetScheduler());

Step 3

We need to create few implementations that will have the actual code to do some of the operations. The implementations classes should implement IJob interface offered by Quartz. When Quartz is execting, it automatically starts with Execute method.

public class SomeJob : IJob
    {
        public Task Execute(IJobExecutionContext context)
        {
            // do something here
            return Task.FromResult(0);
        }
    }

Step 4

Create a method that will schedule the job into Quartz. To schedule a job, there are two parts 1. Setup the Trigger with details like when to start the job, how often to run, is it once or execute at regular intervals, etc.

ITrigger trigger = TriggerBuilder.Create()
             .WithIdentity($"Check Availability-{DateTime.Now}")
             .StartNow()
             .WithPriority(1)
             .Build();
  1. Setup the job with information about the implementations, attach data to the context, specify the identity to the job.
IJobDetail job = JobBuilder.Create<SomeTask>()
                        .WithIdentity("Some Unique ID")
                        .Build(); 

Once the trigger and the job is initialized correct, we can schedule the job into Quartz

   _scheduler.ScheduleJob(job, trigger);

Connect the UI element to the method and see the flow in action

License

This project is open source.

Thank you

quartz-with-asp.net-core's People

Contributors

harshitgindra avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.