Implementation of Spring timed tasks < turn >

Source: Internet
Author: User

I temporarily used to achieve a scheduled task of 2 kinds of ways

First, the annotation method realizes, is simple and convenient

1: Add the following configuration to the Applicationcontext.xml

This is a spring component scan that guarantees classes with timed tasks that can be scanned by spring

<context:component-scan base-package= "Com.test.task" >

This is the configuration of the timed task

<task:executor id= "Executor" pool-size= "1"/>
<task:scheduler id= "Scheduler" pool-size= "1"/>
<task:annotation-driven executor= "Executor" scheduler= "Scheduler"/>

How to write a timed task class

1. Labeling classes with @component

2 use @lazy (value=false) to label the class (this write also line, do not write also can.) I did not write when I was doing the project.)

3 Write a specific method

@Scheduled (cron= "0/5 * * * * *")//indicates the time of the specific operation
public void Testtask () {
Specific methods
}

4. Configuration Complete

Ii. How to implement the configuration file

1. To implement a timed task class, according to the ordinary Java class to write, the class of the class of the timer task of the method name written work () can be

For example

    • public class Tmallpricestorechangefailedgetjob {
    • public void work () {
    • Concrete Method implementation
    • }
    • }

2. Copy a application.xml file, rename it to Application_tmallpricestorcechangefailed_task.xml, and add it to the spring profile

    • <import resource= "application_tmallpricestorcechangefailed"/>

The 3.application_tmallpricestorcechangefailed_task.xml file is written in the following notation

    • <!--Quartz Job config Begin--
    • <bean id= "Tmallpricestorechangefailedgetjob" class= "Com.shopin.third.job.TmallPriceStoreChangeFailedGetJob"/ >
    • <bean id= "gettmallpricestroechangefailed" class= " Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean ">
    • <property name= "TargetObject" >
    • <ref bean= "Tmallpricestorechangefailedgetjob"/>
    • </property>
    • <property name= "Targetmethod" >
    • <value>work</value>
    • </property>
    • </bean>
    • <bean id= "Gettmallpricestroechangefailedtask" class= "Org.springframework.scheduling.quartz.CronTriggerBean" >
    • <property name= "Jobdetail" >
    • <ref bean= "gettmallpricestroechangefailed"/>
    • </property>
    • <!--cron Expressions--
    • <property name= "Cronexpression" >
    • <value>0 * *?</value><!--done once a day at 9 points 30--
    • </property>
    • </bean>
    • <!--quertz Boot configuration--
    • <bean id= "Starttmallquertz" class= "Org.springframework.scheduling.quartz.SchedulerFactoryBean" >
    • <property name= "Triggers" >
    • <list>
    • <ref bean= "Gettmallpricestroechangefailedtask"/>
    • </list>
    • </property>
    • </bean>

Implementation of Spring timed tasks < turn >

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.