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/


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