Spring 定时任务 Task

使用Xml配置实现定时任务

定时任务业务类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

package org.example.job;

import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class TaskJob {
public void Job1(){
System.out.println("任务1:" + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
}

public void Job2(){
System.out.println("任务2:" + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
}
}
spring配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?xml version="1.0" encoding="UTF-8"?>
<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:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/task
https://www.springframework.org/schema/task/spring-task.xsd
">

<!--Spring扫描注解 -->
<context:component-scan base-package="org.example"/>

<!-- 定时任务配置 -->
<task:scheduled-tasks>
<!-- 每2秒执行一次 -->
<task:scheduled ref="taskJob" method="Job1" cron="0/2 * * * * ?" />

<!-- 每5秒执行一次 -->
<task:scheduled ref="taskJob" method="Job2" cron="0/5 * * * * ?"/>
</task:scheduled-tasks>

</beans>
执行定时任务
1
2
3
4
5
6
7
8
9
10
11
package org.example;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App
{
public static void main( String[] args ){
BeanFactory factory = new ClassPathXmlApplicationContext("spring.xml");
}
}

使用注解实现定时任务

使用 @Scheduled 注解可以在任务类中直接配置定时任务,spring配置中只需要开启定时任务即可。

定时任务业务类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package org.example.job;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class TaskJob {
@Scheduled(cron = "0/2 * * * * ?")
public void Job1(){
System.out.println("任务1:" + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
}

@Scheduled(cron = "0/5 * * * * ?")
public void Job2(){
System.out.println("任务2:" + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
}
}
spring配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="UTF-8"?>
<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:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/task
https://www.springframework.org/schema/task/spring-task.xsd
">

<!--Spring扫描注解 -->
<context:component-scan base-package="org.example"/>

<!-- 使用注解配置定时任务,配置定时任务驱动, spring才能够识别 @Scheduled注解 -->
<task:annotation-driven />

</beans>
执行定时任务
1
2
3
4
5
6
7
8
9
10
11
package org.example;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App
{
public static void main( String[] args ){
BeanFactory factory = new ClassPathXmlApplicationContext("spring.xml");
}
}