Spring Task Usage Notes

Source: Internet
Author: User

Because the project needs to do a timed task operation, the knowledge of the previous Contact Java Timer task, the feeling is not very cool, today found that the spring comes with task tasks relatively cool, learning a bit,

First you need to spring3 the above version, support annotations and configuration two forms, I will say the configuration of the form:

1. Add a red section to the spring configuration file

<beans xmlns= "Http://www.springframework.org/schema/beans"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context"
Xmlns:mvc= "Http://www.springframework.org/schema/mvc"
xmlns:task= "Http://www.springframework.org/schema/task"
Xsi:schemalocation= "Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-3.2.xsd
Http://www.springframework.org/schema/mvc
Http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
Http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd ">

2. Annotation Scan package <context:component-scan base-package= "Com.lenovo.task"/>

3. Configuring tasks

<task:scheduled-tasks>
<!--<task:scheduled ref= "Taskjob" method= "getaccesstokenjob" cron= "0 27 23/2 * *?" />--
<task:scheduled ref= "Taskjob" method= "Getaccesstokenjob" fixed-rate= "#{1000*60*60*2}"/>
</task:scheduled-tasks>

Note that there are three properties cron,fixed-rate supported, and Fixed-delay

3.1.cron expression Form

Cron expressions I will not say, your own Baidu bar a lot of, but it is note that this way the task execution is a fixed point of time, such as my upper expression is 23:27 execution, and then every 2 hours after the execution, not restart the server, the task immediately executes. Also pay attention to the month, and the week, must have a question mark?

3.2fixed-rate and Fixed-delay the two are executed every several times, in milliseconds. I don't know the difference between the two of them. You know, say something.

The 4.java class is as follows

Package com.lenovo.task;

Import Java.util.Date;

Import Org.springframework.stereotype.Service;

Import com.lenovo.util.Constants;
Import Com.lenovo.util.WxPayHelper;

@Service
public class taskjob{

public void Getaccesstokenjob () {

Wxpayhelper wxpayhelper = new Wxpayhelper ();
Wxpayhelper.setappid (CONSTANTS.APPID);
Wxpayhelper.setappsecret (Constants.secret);
Wxpayhelper.seturl (Constants.accessurl);
System.out.println (constants.accesstoken+ "-----");
System.out.println (constants.jsapi_ticket+ "Nidaye");
System.out.println (New Date ());

try{
Constants.accesstoken = Wxpayhelper.getaccesstoken ();
Constants.jsapi_ticket=wxpayhelper.getjstoken (Constants.accesstoken);
}catch (Exception e) {
E.printstacktrace ();
}

}


}

Spring Task Usage Notes

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.