下载 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>
- quartz 配置文件 quartz.properties
org.quartz.scheduler.instanceName: DefaultQuartzScheduler
org.quartz.scheduler.rmi.export: false
org.quartz.scheduler.rmi.proxy: false
org.quartz.scheduler.wrapJobExecutionInUserTransaction: false
org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount: 1
org.quartz.threadPool.threadPriority: 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread: true
org.quartz.jobStore.misfireThreshold: 60000
org.quartz.jobStore.class: org.quartz.simpl.RAMJobStore
- 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();
}
}
}