天天看點

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());
   }
}




      
ssm定時器

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

springboot定時器   總的來說   注解定時器最為友善 

在springboot核心類中加入@EnableScheduling即可,表示支援定時任務

ssm定時器
ssm定時器

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

定時器觸發時間集錦

  1. "0/10 * * * * ?"         每十秒觸發  
  2. "0 0/1 * * * ?"           每一分鐘觸發  
  3. "0 0 12 * * ?"      每天中午12點觸發  
  4. "0 15 10 ? * *"         每天上午10:15觸發  
  5. "0 15 10 * * ?"         每天上午10:15觸發  
  6. "0 15 10 * * ? *"       每天上午10:15觸發  
  7. "0 15 10 * * ? 2005"        2005年的每天上午10:15觸發  
  8. "0 * 14 * * ?"      在每天下午2點到下午2:59期間的每1分鐘觸發  
  9. "0 0/5 14 * * ?"        在每天下午2點到下午2:55期間的每5分鐘觸發  
  10. "0 0/5 14,18 * * ?"         在每天下午2點到2:55期間和下午6點到6:55期間的每5分鐘觸發  
  11. "0 0-5 14 * * ?"        在每天下午2點到下午2:05期間的每1分鐘觸發  
  12. "0 10,44 14 ? 3 WED"        每年三月的星期三的下午2:10和2:44觸發  
  13. "0 15 10 ? * MON-FRI"       周一至周五的上午10:15觸發  
  14. "0 15 10 15 * ?"        每月15日上午10:15觸發  
  15. "0 15 10 L * ?"         每月最後一日的上午10:15觸發  
  16. "0 15 10 ? * 6L"        每月的最後一個星期五上午10:15觸發   
  17. "0 15 10 ? * 6L 2002-2005"      2002年至2005年的每月的最後一個星期五上午10:15觸發  
  18. "0 15 10 ? * 6#3"       每月的第三個星期五上午10:15觸發 

轉載于:https://www.cnblogs.com/1234cjq/p/7488704.html