Several implementation methods of Java quartz timer task and spring task timing

1、 Classification

At present, there are mainly three technologies (or three products) according to the realized technologies:

1. Java has its own java.util.timer class, which allows you to schedule a java.util.timertask task. In this way, you can make your program execute at a certain frequency, but it can't run at a specified time. Generally used less, this article will not be introduced in detail.

2. Using quartz, this is a powerful scheduler that allows your program to execute at a specified time or at a certain frequency. The configuration is slightly complex, which will be described in detail later.

3. The built-in task after spring 3.0 can be regarded as a lightweight quartz, and it is much simpler to use than quartz, which will be introduced later.

In terms of inheritance methods of job classes, they can be divided into two types:

1. The job class needs to inherit from a specific job class base class, such as org.springframework.scheduling.quartz.quartzjobbean in quartz; Java.util.timer needs to inherit from java.util.timertask.

2. The job class is an ordinary Java class and does not need to inherit from any base class.

Note: the second method is recommended because all classes are ordinary and do not need to be treated differently in advance.

From the trigger timing of task scheduling, here are mainly the triggers used for jobs, mainly including the following two types:

1. Trigger every specified time. The corresponding trigger in quartz is org.springframework.scheduling.quartz.simpletriggerbean

2. It will be triggered every specified time. The corresponding scheduler in quartz is org.springframework.scheduling.quartz.crontriggerbean

Note: not every task can use these two triggers. For example, java.util.timertask task can only use the first trigger. Both quartz and spring tasks can support these two trigger conditions.

2、 Usage instructions

The usage of each task scheduling tool is introduced in detail, including quartz and spring task.

Quartz

First, the job class inherits from a specific base class: org.springframework.scheduling.quartz.quartzjobbean.

Step 1: define job class

Step 2: configure job class jobdetailbean in spring configuration file

Note: org.springframework.scheduling.quartz.jobdetailbean has two attributes. Jobclass attribute is the task class defined in Java code, and jobdataasmap attribute is the attribute value to be injected into the task class.

Step 3: configure the trigger mode (trigger) of job scheduling

Quartz has two job triggers, namely

org.springframework.scheduling.quartz.SimpleTriggerBean

org.springframework.scheduling.quartz.crontriggerBean

The first simpletriggerbean only supports calling tasks at a certain frequency, such as running every 30 minutes.

The configuration method is as follows:

The second kind of crontriggerbean supports running once at a specified time, such as at 12:00 every day.

The configuration method is as follows:

Step 4: configure the dispatching factory

Note: this parameter specifies the name of the previously configured trigger.

Step 5: start your application and deploy the project to Tomcat or other containers.

Second, the job class does not inherit a specific base class.

Spring supports this approach thanks to two classes:

These two classes correspond to the two methods of task scheduling supported by spring, that is, the timer task mode and quartz mode of Java mentioned earlier. Here I only write the usage of methodinvokingjobdetailfactorybean. The advantage of using this class is that our task class no longer needs to inherit from any class, but an ordinary POJO.

Step 1: write a task class

As you can see, this is an ordinary class and has a method.

Step 2: configure job class

Note: this step is a key step. It declares a methodinvokingjobdetailfactorybean with two key attributes: targetobject specifies the task class and targetmethod specifies the running method. The next steps are the same as method 1. For completeness, they are also posted.

Step 3: configure the trigger mode (trigger) of job scheduling

Quartz has two job triggers, namely

The first simpletriggerbean only supports calling tasks at a certain frequency, such as running every 30 minutes.

The configuration method is as follows:

The second kind of crontriggerbean supports running once at a specified time, such as at 12:00 every day.

The configuration method is as follows:

Either of the above two scheduling methods can be selected according to the actual situation.

Step 4: configure the dispatching factory

Note: this parameter specifies the name of the previously configured trigger.

Step 5: start your application and deploy the project to Tomcat or other containers.

So far, the basic configuration of quartz in spring has been introduced. Of course, before use, it is needless to say that you should import the corresponding spring package and quartz package.

In fact, it can be seen that the configuration of quartz looks very complex. There is no way, because quartz is actually a heavyweight tool. If we just want to simply perform a few simple timing tasks, is there a simpler tool? Yes!

Spring-Task

This paper introduces the self-developed timing task tool, spring task, after spring 3.0. It can be compared to a lightweight quartz, and it is very simple to use. In addition to spring related packages, there is no need for additional packages, and it supports annotation and configuration files

These two methods will be introduced below.

First: configuration file mode

Step 1: write a job class

That is, ordinary POJO, as follows:

Step 2: add namespace and description in spring configuration file header

Step 3: set specific tasks in the spring configuration file

Note: the task class specified by ref parameter, the method specified by method, and cron and cronexpression expressions are not described here. See the appendix of the previous article for details.

< context: component scan base package = "com. Gy. Mytask" / > needless to say, this configuration is used for spring scanning annotations.

The configuration is completed here. Isn't it very simple.

Second: use annotation form

Maybe we don't want to configure every task class in the XML file. We can use the annotation @ scheduled. Let's see the definition of the annotation in the source file:

It can be seen that the annotation has three methods or parameters, which respectively mean:

Cron: specifies a cron expression

Fixed delay: Official Document explanation: an interval based trigger where the interval is measured from the completion time of the previous task. The time unit value is measured in milliseconds.

Fixedrate: Official Document explanation: an interval based trigger where the interval is measured from the start time of the previous task. The time unit value is measured in milliseconds.

Let me configure it.

Step 1: write POJO

Step 2: add task related configuration:

Note: theoretically, you only need to add the sentence < task: annotation driven / > configuration. These parameters are not necessary.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>