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工程,查看控制台,出现如下效果:

在项目中集成定时任务--->Quartz

6、到这里,定时任务(Quartz)与项目就完全的集成了。


相关文章:

  • 2021-09-13
  • 2022-12-23
  • 2021-11-24
  • 2021-05-01
  • 2021-07-10
  • 2021-12-17
猜你喜欢
  • 2021-11-30
  • 2022-12-23
  • 2021-12-01
  • 2021-12-22
  • 2022-01-28
相关资源
相似解决方案