In the application background of development and testing tools, I often hear colleagues say that they need to do a timed task to do log processing or data cleaning, including doing some complex business calculation logic. When choosing a timed task, how can they quickly implement it and choose a more suitable method for you? I have collected and organized some methods to implement scheduled tasks here. I hope it can help students who are just starting to do scheduled tasks. Please correct any wrong writing.
There are three ways to summarize Java basic timing tasks:
1.1 Create a thread, and then let it run in the while loop, and achieve the effect of the timing task through the sleep method;
1.2 The use of Timer and TimerTask has the following benefits compared to the first method:
1.3 The ScheduledExecutorService is introduced as a concurrent tool class from java.util.concurrent. This is the ideal timed task implementation method. Compared with the previous two methods, it has the following benefits:
There are three ways to summarize Spring timed tasks:
2.1 ScheduledTimerTask: Spring's ScheduledTimerTask defines the running cycle of a timer task. Unfortunately, you can specify the frequency of the task execution, but you cannot specify exactly when it runs, which requires the use of the second Quartz for task scheduling;
Create a business task, declared in Spring configuration file;
In the Spring configuration file, configure ScheduledTimerTask and associate the custom task instance;
Start the timer, Spring's TimerFactoryBean is responsible for starting the timing task
2.2 Using Quartz:
First of all, define business logic tasks as usual:
Declare and configure the trigger method of job scheduling in Spring
Here, there are two types of job triggers for Quartz, namely
org.springframework.scheduling.quartz.SimpleTriggerBean
org.springframework.scheduling.quartz.CronTriggerBean
The first SimpleTriggerBean only supports calling tasks at a certain frequency, such as running once every once a while.
<bean id="simpleTrigger"> <property name="jobDetail" ref="myJob" /> <property name="startDelay" value="0" /><!-- After the dispatch factory is instantiated, the dispatching starts after 0 seconds--> <property name="repeatInterval" value="2000" /><!-- Scheduled every 2 seconds--> </bean>
The second type of CronTriggerBean supports running once at a specified time, such as running once at 12:00 every day, as configured above;
Configure the dispatch factory
org.springframework.scheduling.quartz.SchedulerFactoryBean, the code is as above;
Just launch your application
2.3 Using Spring-Task
Spring's own timed task tool, spring task, can compare it to a lightweight Quartz, and it is very simple to use. It does not require additional packages except for spring-related packages, and supports two types of annotations and configuration files:
Step 1: Write the task class; TaskJob, method job1 -- The second step of code omitting: Add namespace and description 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 3: Set specific tasks in 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, the method specifies the method that needs to be run, the cron and cronExpression expressions, the specific writing method will not be introduced here, and the <context:component-scan base-package="com.alibaba.mytask" />spring scan annotation is used.
The above is all about this article, I hope it will be helpful to everyone's learning.