Timed Task-Framework Learning Based on Quartz

Keywords: Maven Database JDBC

Quartz Notes

Recently, I came across a scene where I need to be able to perform tasks at a specified time, such as opening a service at 3 o'clock every Saturday and Sunday. I can't say that I want to list the dates of every Saturday and Sunday in the future for the program to execute. So Baidu found such an open source framework, Qartz, which is still quite simple to use, so I plan to record it

To configure

maven makes it easier to add project dependencies

        <!-- https://mvnrepository.com/artifact/org.quartz-scheduler/quartz -->
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>2.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz-jobs</artifactId>
            <version>2.2.1</version>
        </dependency>

emmm, maven repository can find the latest 2.3.0, but there are no tutorials on the official website = =so I chose 2.2.1, which is always true, and it doesn't require much functionality.

configuration file

This is easy. Just put a file named quartz.properties in the resources folder and write a few sentences:

org.quartz.scheduler.instanceName = MyScheduler
org.quartz.threadPool.threadCount = 3
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore

So the first is the name, the second is the maximum number of threads, the same thing as that thread pool, the third is the storage method, it seems that he can save the task to the database or to RAM. We ultimately want to save it to the database, but RAM is the way of novice tutorials, so I will try RAM first, and then go to JDBC.

Function

This should be the easiest way I've ever run it

public class Scheduler implements Runnable{

    private static org.quartz.Scheduler scheduler;

    @Override
    public void run() {
        try {
            // Grab the TaskHandler instance from the Factory
            scheduler = StdSchedulerFactory.getDefaultScheduler();

            // and start it off
            scheduler.start();

        } catch (SchedulerException se) {
            se.printStackTrace();
        }
    }
}

Get a default Scheduler and start

Then you can see this:

Start up and finish

Create a task

One of his tasks is called job. As its name implies, it is a task, such as how to work, and what to do, mainly a task process problem. Let's look inside.

public interface Job {
    void execute(JobExecutionContext var1) throws JobExecutionException;
}

It's very simple inside, just an execute, so all we need to do is do it

public class MyJob implements Job {

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        System.out.println("fin's job");
    }
}

We created a job and execute it when we want to do it!So we just need to define this execute to execute exactly what we want.

So how does this job fit into a task? He has a builder pattern named org.quartz.JobBuilder, so we can construct a job from this

 JobDetail jobDetail = JobBuilder.newJob(MyJob.class).build();

JobDetail is one of the tasks we need to perform. It builds our instantiated Job through the newJob static method of the JobBuilder class, then builds the JobDetail objects we need.

Job Pass Value

job's builder can use the usingJobData method to pass values, so this is what is called in the framework: JobDataMap, or obviously, it means task data map, usingJobData can be understood as Map.put(), preceded by key, followed by value.

JobDetail jobDetail = JobBuilder.newJob(MyJob.class)
    .usingJobData("name","finQQ")
    .build();

So how do we get this value?

Just this sentence:

        Map map = jobExecutionContext.getJobDetail().getJobDataMap();
        System.out.println(map.get("name") + "'s job");

This gives us the data we need.Similarly, there are many functions within builder that we can choose from.

trigger

Just what Job did, so when does Trigger do it, what we need most is this Trigger

Creating a Trigger is also created by the so-called TriggerBuilder

Trigger trigger = TriggerBuilder.newTrigger()
    .startNow()
     .withSchedule(simpleSchedule()
         .withIntervalInSeconds(40)
         .repeatForever())
     .build();

This creates a trigger.

  • startNow Starts Now
  • With Schedule Calendar, this is what determines when we do what, SimpleSchedule and CronSchedule. This is to create a normal calendar that loops every 40 seconds and then dies
  • build Build Begins

Schedule

Cronschedule should be the one I need, so I'll look into it later.

Last step

Add our job and trigger to the workspace

scheduler.scheduleJob(jobDetail, trigger);

Effect

As you can see, a message is printed every 40 seconds, and the next one I'll write about my use based on actual application

Posted by everydayrun on Fri, 17 May 2019 01:58:25 -0700