The Spring-task of spring task scheduling

Source: Internet
Author: User
Tags aop

I. Preface

The above two introduces the use of timer and quartz in spring, this article will introduce the Spring3.0 self-developed Time Task tool, spring task, can compare it to a lightweight quartz, and it is very simple to use. There is no need for additional packages outside of spring-related packages, and there are two forms of annotations and configuration files, both of which are described below.

Second, the first type: Configuration file mode

First step: Writing Job classes

That is, the ordinary Pojo, as follows:

12345678 import  < Code class= "Java plain" >ORG.SPRINGFRAMEWORK.STEREOTYPE.SERVICE;&NBSP; @Service  public  class  taskjob {  &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;  &NBSP;&NBSP;&NBSP;&NBSP; public  < Code class= "Java keyword" >void  job1 () {  &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; system.out.println ("task in progress ... ");   &NBSP;&NBSP;&NBSP;&NBSP; }&NBSP;

Step Two: Spring configuration

<beans xmlns= "Http://www.springframework.org/schema/beans"
xmlns:task= "Http://www.springframework.org/schema/task"
......
xsi:schemalocation= "Http://www.springframework.org/schema/task http://www.springframework.org/schema/task/ Spring-task-3.0.xsd ">


<task:scheduled-tasks>
<task:scheduled ref= "Taskjob" method= "job1" cron= "0 * * * * *?" />
</task:scheduled-tasks>
<context:component-scan base-package= "Com.gy.mytask"/>


</beans>

Description: The ref parameter specifies the task class, method specified, which is required to run the methods, cron, and cronexpression expressions. <context:component-scan base-package= "Com.gy.mytask"/> This configuration is not much said, spring scanning annotations used.

Second: Use annotations as a form of

Maybe we don't want to write a task class to be configured in an XML file, we can use the annotation @scheduled, and we'll look at the definition of the annotation in the source file:

1234567891011 @Target({java.lang.annotation.ElementType.METHOD, java.lang.annotation.ElementType.ANNOTATION_TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documentedpublic@interfaceScheduled   publicabstractString cron();     publicabstractlongfixedDelay();     public abstractlongfixedRate(); }

It can be seen that the note has three methods or parameters, respectively, that means:

Cron: Specifies the cron expression.

Fixeddelay: Official documentation explains: 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. That is, the interval from the start of the previous task to the start of the next task, in milliseconds.

Fixedrate: Official documentation explains: An interval-based trigger where the interval are measured from the start time of the previous task. The time unit value is measured in milliseconds. The interval from the start of the previous task to the start of the next task, in milliseconds.

First step: Writing Pojo

123456789 importorg.springframework.stereotype.Component;   @Component(“taskJob”) publicclassTaskJob {     @Scheduled(cron = "0 0 3 * * ?"    publicvoidjob1() {         System.out.println(“任务进行中。。。”);     }

Step Two: Add the task-related configuration:

1234567891011121314151617181920212223 <?xml version="1.0"encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:tx="http://www.springframework.org/schema/tx"    xmlns:task="http://www.springframework.org/schema/task"    xsi:schemaLocation="         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd         http://www.springframework.org/schema/context  http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd         http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"     default-lazy-init="false"        <context:annotation-config />     <!—spring扫描注解的配置   —>     <context:component-scan base-package="com.gy.mytask"/>       <!—开启这个配置,spring才能识别@Scheduled注解   —>     <task:annotation-driven scheduler="qbScheduler"mode="proxy"/>     <task:scheduler id="qbScheduler"pool-size="10"/> 

Description: Theoretically only need to add <task:annotation-driven/> This sentence configuration can be, these parameters are not necessary.

After configuration, spring task has many parameters, refer to XSD document http://www.springframework.org/schema/task/spring-task-3.0.xsd

Quote Original: http://www.cnblogs.com/hongwz/p/5642497.html

Blog is to remember that they are easy to forget things, but also a summary of their work, the article can be reproduced, without copyright. Hope to do their own efforts to do better, we work together to improve!

If there is any problem, welcome to discuss together, code if there is a problem, you are welcome to the great God!

The Spring-task of spring task scheduling

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.