In the development of testing tools in the background, often heard colleagues said to do a timed task to do log processing, or data cleaning, including doing some complex business computing logic, in the choice of timing tasks, how can quickly achieve, and choose a more suitable for their own way? I am here to implement the timing of the task of collecting a number of methods, hoping to help the beginning of the time to do the task of the students, write the wrong place please correct me.
a Java Basic timing task, the summary method has three kinds:
1.1 Create a thread, and then let it run in the while loop, through the sleep method to achieve the effect of timed tasks;
1.2 Using timer and TimerTask have the following benefits compared to the first method:
- You can control when you start and cancel a task.
- The first time you perform a task, you can specify the delay time you want.
1.3 In the java.util.concurrent of Scheduledexecutorservice, which is introduced as a concurrency tool class, is the ideal timing task implementation, and it has the following benefits compared to the previous two methods:
- It's a thread-pool way to perform a task, compared to a timer's single-threaded
- Can be very flexible to set the first time to perform a task delay time
- A good convention is provided to set the time interval for execution
The two Spring timed tasks are summarized in three different ways:
2.1 Scheduledtimertask:spring's scheduledtimertask defines the running cycle of a timer task, but unfortunately you can specify how often the task executes, but you cannot specify exactly when it will run, This requires the use of a second quartz for task scheduling;
Create a business task that is declared in the spring configuration file;
In the spring configuration file, configure the Scheduledtimertask and the custom task instance on the association;
Start Timer, Spring's Timerfactorybean is responsible for starting timed tasks
2.2 Use Quartz:
First or the same. Define Business logic tasks:
To declare and configure the firing mode of a job schedule in spring
There are two types of job triggers quartz here, respectively
Org.springframework.scheduling.quartz.SimpleTriggerBean
Org.springframework.scheduling.quartz.CronTriggerBean
The first type of Simpletriggerbeanonly supports calling a task at a certain frequency, such as running every once in a while.
<bean id= "Simpletrigger" class= "Org.springframework.scheduling.quartz.SimpleTriggerBean" >
<property Name= "Jobdetail" ref= "Myjob"/> <property name= "Startdelay"
value= "0"/><!--dispatch factory instantiation, after 0 seconds to start execution >
<property name= "Repeatinterval" value= "/><"!--dispatch once every 2 seconds-->
</bean>
The second type of Crontriggerbean, support to the specified time to run once, such as 12:00 every day to run once, such as on the configuration;
Configure the Dispatch factory
Org.springframework.scheduling.quartz.SchedulerFactoryBean, code as above;
You can start your application.
2.3 Using Spring-task
Spring task, which is brought with it, can be likened to a lightweight quartz, and is simple to use, with no additional packages outside of spring-related packages, and two kinds of annotations and configuration files are supported:
The first step: write the task class; Taskjob,method JOB1--code omitted
Step Two: Add namespaces and descriptions in the spring configuration file header
<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 ">
Step three: Set specific tasks in the spring configuration file
<task:scheduled-tasks>
<task:scheduled ref= "Taskjob" method= "job1" cron= "0 * * * * *?" />
</task:scheduled-tasks>
<context:component-scan base-package= "Com.alibaba.mytask"/>
Description: The ref parameter specifies the task class, method specifies the methods that need to be run, cron and cronexpression expressions, specific writing is not introduced here, <context:component-scan base-package= " Com.alibaba.mytask "/>spring scan annotation used.
The above is the entire content of this article, I hope to help you learn.