基于quartz-2.1.3.jar 的quartz调度表达式

来源:互联网 发布:王诺诺知乎女神扒皮 编辑:程序博客网 时间:2024/05/18 06:38

下载 quartz-2.1.3.jar 下载地址 http://download.csdn.net/download/chenshijie2011/10129927
1 . springmvc-servlet.xml 配置文件

  <!--定义定时执行QuartTimer这个bean中的quartTimerMethod()方法 --><bean id="doJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">        <!--你要执行的那个方法对应的bean -->        <property name="targetObject">            <ref bean="QuartTimer" />        </property>        <!--你要执行那个方法,注意方法不能有返回值,参数好像也不能有 -->        <property name="targetMethod">            <value>quartTimerMethod</value>        </property>        <property name="concurrent" value="false" />     </bean>    <!--触发器的bean的设置,在这里我们设置了我们要触发的jobDetail是哪个。    这里我们定义了要触发的jobDetail是searchEngerneTask,即触发器去触发哪个bean。    并且我们还定义了触发的时间。    spring版本<3.1,quartz版本为1.x,class使用CronTriggerBean;    spring版本>3.1,quartz版本为2.x,class使用CronTriggerFactoryBean;尽量按这两种方式使用-->    <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">        <property name="jobDetail">            <ref bean="doJob" />        </property>        <property name="cronExpression">            <!-- 执行周期的表达式;每小时:[0 0 * * * ?]; 每五钟:[0 0/5 * * * ?]-->            <value>0 10 0 * * ?</value><!-- 每天0点10分执行一次 -->        </property>    </bean>    <!--管理触发器的总设置,管理我们的触发器列表,可以在bean的list中放置多个触发器。 -->    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init="false" >        <property name="triggers">            <list>                <ref bean="cronTrigger" />            </list>        </property>        <property name="configLocation" value="classpath:config/quartz.properties"/>    </bean>
  1. quartz 配置文件 quartz.properties
org.quartz.scheduler.instanceName: DefaultQuartzSchedulerorg.quartz.scheduler.rmi.export: falseorg.quartz.scheduler.rmi.proxy: falseorg.quartz.scheduler.wrapJobExecutionInUserTransaction: falseorg.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPoolorg.quartz.threadPool.threadCount: 1org.quartz.threadPool.threadPriority: 5org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread: trueorg.quartz.jobStore.misfireThreshold: 60000org.quartz.jobStore.class: org.quartz.simpl.RAMJobStore
  1. package com.*.quartz 为调用包,QuartTimer 为定时器类
package com.***.quartz;import java.io.BufferedReader;import java.io.InputStreamReader;import org.springframework.context.annotation.Lazy;import org.springframework.stereotype.Component;import org.springframework.stereotype.Service;import com.cetiti.dim.common.ScheduleConfig;/** * 测试定时器 */@Component("QuartTimer")@Lazy(false)public class QuartTimer {    /**     * @Date 创建时间:2016年8月9日 下午4:47:05     * @Description :定时作业执行     */    public void quartTimerMethod(){        System.out.println("testQuartTimerMethod 1111111111 定时作业执行中...");        this.runShell();        System.out.println("testQuartTimerMethod 2222222222 定时作业执行中...");    }    public void runShell(){        try {                   String shpath=  Thread.currentThread().getContextClassLoader().getResource("").getPath()+"config/logStatistic.sh";             Process ps =Runtime.getRuntime().exec(" chmod -R 777 " + shpath);            ps.waitFor();              Process ps1 = Runtime.getRuntime().exec(" /bin/sh "+ shpath +" "+ScheduleConfig.getProperty("DAY"));              ps1.waitFor();              BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));              StringBuffer sb = new StringBuffer();              String line;              while ((line = br.readLine()) != null) {                  sb.append(line).append("\n");              }              String result = sb.toString();              System.out.println(result);              }           catch (Exception e) {              e.printStackTrace();              }      }}