Quartz

Quartz

Quartz是OpenSymphony開源組織在Job scheduling領域又一個開源項目,

它可以與J2EE與J2SE應用程序相結合也可以單獨使用。

Quartz可以用來創建簡單或爲運行十個,百個,甚至是好幾萬個Jobs這樣複雜的程序。

Jobs可以做成標準的Java組件或 EJBs。



MAVEN 

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

和shiro 一起使用的時候會有問題 需要再shiro中排除  

  創建作業類

  

package com.stevezong.ice.opc.utils;
import java.util.Date;
/**
 * 自定義作業類
 * @author steve
 *
 */
public class MyJob {
    public void run() {
        System.out.println(new Date());
    }
}



spring 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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/util
       http://www.springframework.org/schema/util/spring-util-2.0.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd
       ">

    <!-- 配置JobDetail -->
    <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <!-- 注入目標對象 -->
        <property name="targetObject" ref="myJob" />
        <!-- 注入目標方法 -->
        <property name="targetMethod" value="run" />
    </bean>
    <!-- 配置觸發器 -->
    <bean id="myTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <!-- 注入任務詳情對象 -->
        <property name="jobDetail" ref="jobDetail" />
        <!-- 注入cron表達式,通過這個表達式指定觸發的時間點 -->
        <property name="cronExpression">
            <value>0/5 * * * * ?</value>
        </property>
    </bean>
    <!-- 配置調度工廠 -->
    <bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <!-- 注入觸發器 -->
        <property name="triggers">
            <list>
                <!-- 可以加入多個 -->
                <ref bean="myTrigger" />
            </list>
        </property>
    </bean>
</beans>



cron表達式在線生成器

http://cron.qqe2.com/


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章