Spring task instructions

Source: Internet
Author: User

Spring task instructions

Because the project needs to perform scheduled task operations, the previous knowledge of java timer task is not very good. Today, I found that the task in spring is quite good, so I learned it,

First, spring3 and later versions are required. Annotations and configurations are supported. Let's talk about the configuration format:

1. Add the red part 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. Configure the task

<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>

The three attributes cron, fixed-rate, and fixed-delay are supported.

3.1.cron Expression Form

I won't talk about the cron expression. You may have a lot of cron expressions on your own, but note that the task execution in this way has a fixed time point. For example, the above expression is executed, the task is executed every two hours, instead of restarting the server. Also pay attention to the month, and week, must there be a question mark?

3.2fixed-rate and fixed-delay are executed at intervals, in milliseconds. I don't know the difference between them. Let's talk about it.

4. the 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. accesen En + "-----");
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 ();
}

}


}

 

Related Article

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.