您的位置:首页 > 其它

在项目中集成定时任务--->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)与项目就完全的集成了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: