在项目中集成定时任务--->Quartz
2017-09-21 11:00
429 查看
1、在Maven工程的/src/main/resources目录下创建文件spring-quartz.xml;spring-quartz.xml中的内容如下:
<?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:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.1.xsd">
<!-- 配置定时任务 -->
<task:annotation-driven/>
</beans>
2、在Maven工程的/src/main/webapp/WEB-INF/web.xml文件中进行如下配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0" >
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring-*.xml</param-value>
</context-param>
</web-app>
3、写测试类进行测试,测试类如下:
package com.***.***.test;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* 定时任务测试类
*/
@Component
public class QuartzTest {
//每十秒执行一次
@Scheduled(cron="0/10 * * * * ?")
public void test1() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
System.out.println("当前时间是:" +df.format(new Date()));// new Date()为获取当前系统时间
}
}
4、启动tomcat运行Maven工程,查看控制台,出现如下效果:
6、到这里,定时任务(Quartz)与项目就完全的集成了。
<?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:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.1.xsd">
<!-- 配置定时任务 -->
<task:annotation-driven/>
</beans>
2、在Maven工程的/src/main/webapp/WEB-INF/web.xml文件中进行如下配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0" >
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring-*.xml</param-value>
</context-param>
</web-app>
3、写测试类进行测试,测试类如下:
package com.***.***.test;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* 定时任务测试类
*/
@Component
public class QuartzTest {
//每十秒执行一次
@Scheduled(cron="0/10 * * * * ?")
public void test1() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
System.out.println("当前时间是:" +df.format(new Date()));// new Date()为获取当前系统时间
}
}
4、启动tomcat运行Maven工程,查看控制台,出现如下效果:
6、到这里,定时任务(Quartz)与项目就完全的集成了。
相关文章推荐
- Quartz1.7 动态添加、修改和删除定时任务(非spring集成版)
- Spring集成Quartz定时任务框架介绍和Cron表达式详解
- Spring集成Quartz定时任务框架学习
- 项目启动后开启定时任务方法-->ServletContextListener
- Spring集成Quartz定时任务框架介绍和Cron表达式详解
- 【JAVA秒会技术之搞定Quartz定时任务】Quartz在Spring中的集成与应用
- Spring集成Quartz定时任务框架介绍和Cron表达式详解
- Spring集成quartz框架实现定时任务(一)
- kettle整合到web项目并运用quartz实现定时任务
- Spring集成Quartz定时任务框架介绍和Cron表达式详解
- 基于quartz的任务调度插件封装,快速集成项目
- Spring集成Quartz定时任务框架介绍和Cron表达式详解
- Spring web项目使用Quartz定时任务
- 工作总结---梳理下项目中 的 定时任务 quartz
- Spring集成Quartz1.8.4版定时任务框架介绍
- Spring集成Quartz定时任务框架介绍和Cron表达式详解
- Spring集成Quartz定时任务框架介绍和Cron表达式详解
- spring集成定时任务框架 Quartz的两种方式
- Spring Boot集成持久化Quartz定时任务管理和界面展示(转)
- cronExpression 的写法,spring中执行定时任务,spring 集成Quartz