Several implementation methods of spring timed tasks

Source: Internet
Author: User

Recent project development needs to perform some scheduled tasks, such as the need to release information on time 9:00 every day, to take this opportunity to collate a few of the timing tasks of implementation, because the project uses the spring framework, so I will combine

Spring Framework to introduce.

A Classification
    • From the implementation of the technology to classify, there are currently three kinds of technology (or three products):
    1. Java comes with the Java.util.Timer class, which allows you to dispatch a Java.util.TimerTask task. Using this method allows your program to execute at a certain frequency, but not at a specified time. Generally used less, this article will not do a detailed introduction.
    2. Using quartz, this is a powerful scheduler that allows your program to execute at a specified time, and can be executed at a certain frequency, which is slightly more complex to configure, and will be described in more detail later.
    3. SPRING3.0 's own task, which can be seen as a lightweight quartz, is much simpler to use than quartz and will be introduced later.
    • From the inheritance of the job class, you can be divided into two categories:
    1. Job classes need to inherit from a particular job class base class. The need to inherit from the Org.springframework.scheduling.quartz.quartzjobbean;java.util.timer in quartz is required to inherit from Java.util.TimerTask.
    2. The job class is a normal Java class and does not need to inherit from any base class.

Note: A second approach is recommended, because the classes are generic and do not need to be treated differently beforehand.

    • From the trigger time of task scheduling, here is mainly for the job use of the trigger, mainly the following two kinds:
    1. Fires once every specified time, and the corresponding trigger in quartz is: Org.springframework.scheduling.quartz.SimpleTriggerBean
    2. Each time it is specified, it is triggered once, and the corresponding scheduler in Quartz is: Org.springframework.scheduling.quartz.CronTriggerBean

Note: These two triggers are not available for each task, such as the Java.util.TimerTask task, which can only be used for the first type. Both quartz and spring tasks can support both of these triggering conditions.

Two Usage

1, Java comes with the Java.util.Timer class, custom a class inherits TimerTask

Example:

Package Com.timertest;import Java.util.timer;import Java.util.timertask;public class Testtimertask {timer timer; Public testtimertask (int a) {timer = new timer (); Timer.schedule (new Goodtimertask (), 0, 1000*a);} public static void Main (string[] args) {System.out.println ("About to schedule task."); New Testtimertask (3);} Class Goodtimertask extends timertask{@Overridepublic void Run () {System.out.println ("Timer running!");}}

2, Spring3.0 after the task that comes with

One, in the Applicationcontext.xml configuration of the way//1, prepare the jar Package//(1) Spring core jar//2, write a class in the project @Servicepublic classes Bookscheduletask {@ Resourceprivate bookservice bookservice;public page<book> findallbook () {//without parameter System.out.println (" Bookscheduletask.findallbook () "); return Bookservice.findbook (1);}} 3, in the Applicationcontext.xml configuration file, write the following configuration:<!--automatic scanning--><context:component-scan base-package= "com"/><!- -Spring Frame Scheduled timer--><task:scheduled-tasks><task:scheduled ref= "Bookscheduletask" method= " Findallbook "cron=" 0 0 12 * *? " /></task:scheduled-tasks> Description: (1) <context:component-scan base-package= "com"/> The configuration,         I believe you have used spring to know what it means, spring is used to scan the annotations (2) <task:scheduled-tasks> The purpose of this configuration is to define a time-scheduled task that defines a class under which a method executes at a certain time. <task:scheduled ref= "Bookscheduletask" method= "Findallbook" cron= "0 0 12 * *?" />,ref represents a class to be executed at a specified time, method means that the class to be executed under one of the methods, cron represents the time expression, detailed expression can be Baidu query. Second, note-based spring Timer//1, ibid, prepare the Spring jar//2, create a class in the project for the class that performs the scheduled task.     As follows:   @Component ("Bookscheduletask") public class Bookscheduletask {@Resourceprivate bookservice bookservice; @Scheduled ( cron= "0 50 14 * *?")   public void Findallbook () {System.out.println ("Bookscheduletask.findallbook ()"); Bookservice.findbook (1); }}//Description: An annotation-based approach, @Component ("Bookscheduletask") means that an alias is defined. @Scheduled (cron= "0 50 14 * *?") Means: The method under this note is a time task, in cron= "0 50 14 * *?" (14:50) Execute the Findallbook () method

3, the use of quartz, heavy-weight framework, the way to see

http://gong1208.iteye.com/blog/1773177

Several implementation methods of spring timed tasks

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.