ssm定时器

 web-xml配置访问启动quartz的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" version="3.0">
<display-name>TestPro</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>mybatis</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:mybatis-servlet.xml,classpath:quarzt-beans.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mybatis</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>SpringEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SpringEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<session-config>
<session-timeout>15</session-timeout>
</session-config>
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/jsp/error/404.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/WEB-INF/jsp/error/500.jsp</location>
</error-page>
</web-app>

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

quartz-beans.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">


<!-- 使用MethodInvokingJobDetailFactoryBean,任务类可以不实现Job接口,通过targetMethod指定调用方法-->

<!-- 要调用的工作类 -->
<bean id="taskJob" class="cn.mybatis.Util.JobTask"/>

<!-- 任务 -->
<bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="group" value="job_work"/>
<property name="name" value="job_work_name"/>
<!--false表示等上一个任务执行完后再开启新的任务-->
<property name="concurrent" value="false"/>

<!-- 调用的类 -->
<property name="targetObject">
<ref bean="taskJob"/>
</property>

<!-- 调用类中的方法 -->
<property name="targetMethod">
<value>run</value>
</property>
</bean>
<!-- 调度触发器   定义触发时间 -->
<bean id="myTrigger"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="name" value="work_default_name"/>
<property name="group" value="work_default"/>
<property name="jobDetail">
<ref bean="jobDetail" />
</property>
<property name="cronExpression">
<!-- <value>0 0 23 * * ?</value> --><!-- 每天23点执行一次 -->
<value>0/10 * * * * ?</value> <!-- 没10秒 -->
</property>
</bean>


<!-- 调度工厂  总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
<bean id="scheduler" lazy-init="false" autowire="no"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="myTrigger"/>
<!-- <ref bean="produceTrigger"/> -->
</list>
</property>
</bean>

</beans>

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

pom.xml解决jar包依赖   新增quartz的依赖

<!-- quartz -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<!-- quartz依赖包 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.6</version>
</dependency>
<!-- quartz end -->

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

job类

package cn.mybatis.Util;

public class JobTask{
private static int i=1;
public void run() throws Exception {
System.out.println("正在跑第"+i+"次job");
i++;
}
}

 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

第二种配置方式   注解配置定时任务

spring-timer.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

<task:executor id="executor" pool-size="5" />
<task:scheduler id="scheduler" pool-size="10" />
<task:annotation-driven executor="executor" scheduler="scheduler" />
</beans>



任务类 注解启动
@Component(把普通pojo实例化到spring容器中,相当于配置文件中的<bean id="" class=""/>)
public class ToTimer{

@Resource
RoleService roleService;
@Scheduled(cron = "0/20 * * * * ? ")
public void run() {
/**
* 调用存储过程,重新创建表,插入初始化数据。
*/
roleService.initData();
System.out.println(new Date().getTime());
}
}







-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

springboot定时器   总的来说   注解定时器最为方便 

在springboot核心类中加入@EnableScheduling即可,表示支持定时任务

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

定时器触发时间集锦

  1. "0/10 * * * * ?"         每十秒触发  
  2. "0 0/1 * * * ?"           每一分钟触发  
  3.   
  4. "0 0 12 * * ?"      每天中午12点触发  
  5. "0 15 10 ? * *"         每天上午10:15触发  
  6. "0 15 10 * * ?"         每天上午10:15触发  
  7. "0 15 10 * * ? *"       每天上午10:15触发  
  8. "0 15 10 * * ? 2005"        2005年的每天上午10:15触发  
  9. "0 * 14 * * ?"      在每天下午2点到下午2:59期间的每1分钟触发  
  10. "0 0/5 14 * * ?"        在每天下午2点到下午2:55期间的每5分钟触发  
  11. "0 0/5 14,18 * * ?"         在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发  
  12. "0 0-5 14 * * ?"        在每天下午2点到下午2:05期间的每1分钟触发  
  13. "0 10,44 14 ? 3 WED"        每年三月的星期三的下午2:10和2:44触发  
  14. "0 15 10 ? * MON-FRI"       周一至周五的上午10:15触发  
  15. "0 15 10 15 * ?"        每月15日上午10:15触发  
  16. "0 15 10 L * ?"         每月最后一日的上午10:15触发  
  17. "0 15 10 ? * 6L"        每月的最后一个星期五上午10:15触发   
  18. "0 15 10 ? * 6L 2002-2005"      2002年至2005年的每月的最后一个星期五上午10:15触发  
  19. "0 15 10 ? * 6#3"       每月的第三个星期五上午10:15触发 
当能力支撑不了野心时,就该静下心来学习!
原文地址:https://www.cnblogs.com/1234cjq/p/7488704.html