1、引入定时器需要引入的maven依赖
<!-- 关于quartz 建议1.8<版本 -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>1.8.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
2、书写定时器对应的定时器的方法类
package com.zero.utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Date;
import java.util.Properties;
import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
/**
*
* @author haijie.peng
* @time 2019年5月6日09:39:07
* @deprecated 定时器定时调用第三方程序
*/
public class TaskOne extends QuartzJobBean {
private static Logger log = Logger.getLogger(TaskOne.class.getClass());
private int timeout;
public void setTimeout(int timeout) {
this.timeout = timeout;
}
@Override
protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
Properties properties = new Properties();
ClassLoader classLoader = TaskOne.class.getClassLoader();
URL resource = classLoader.getResource("db.properties");
String path = resource.getPath();
InputStream resourceAsStream = classLoader.getResourceAsStream("db.properties");
try {
properties.load(resourceAsStream);
} catch (IOException e) {
e.printStackTrace();
}
String urlPath = properties.getProperty("thridApp");
log.info("-----upLoad video-----"+new Date()+"------------------------"+properties.getProperty("thirdApp"));
runThirdApp(urlPath);
}
//调用指令方法,没有用到的话请自觉注解
public static Integer runThirdApp(String code){
Integer num = null;
StringBuilder sb = new StringBuilder();
try {
String[] cmds = {code};
Process pro = Runtime.getRuntime().exec(cmds);
pro.waitFor();
InputStream in = pro.getInputStream();
BufferedReader reade = new BufferedReader(new InputStreamReader(in));
String lin = null;
while ((lin = reade.readLine()) != null) {
if(lin!=null) {
sb.append(lin);
}
}
pro.waitFor();
in.close();
reade.close();
log.info("The ThirdApp run result is"+sb.toString()+"----------"+new Date());
}catch (Exception e) {
e.printStackTrace();
}
return num;
}
}
3、spring配置对应的环境变量
<!--定时器配置文件 -->
<!-- 方式一-->
<!-- <bean name="exampleJob" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="com.zero.utils.TaskOne" />
<property name="jobDataAsMap">
<map>
<entry key="timeout" value="5" />
</map>
</property>
</bean> -->
<!-- 对于下列不理解请自行百度“定时器表达式”-->
<bean id="cronTrigger"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="exampleJob" />
<property name="cronExpression" value="0 0 6 * * ?" />
<property name="cronExpression" value="0 0/10 * * * ?" />
<property name="cronExpression" value="0/120 * * * * ?" />
</bean>
<!-- 方式2 -->
<!-- <bean id="exampleBusinessObject" class="com.zero.utils.QuartzJob" />
<bean id="jobDetail"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="exampleBusinessObject" />
<property name="targetMethod" value="doIt" />
<property name="concurrent" value="false" />
</bean>
<bean id="simpleTrigger"
class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
see the example of method invoking job above
<property name="jobDetail" ref="jobDetail" />
10 seconds
<property name="startDelay" value="5000" />
repeat every 50 seconds
<property name="repeatInterval" value="3000" />
</bean> -->
<!-- 总调度用于启动Spring定时器 -->
<!-- <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTrigger" />
<ref bean="simpleTrigger" />
</list>
</property>
</bean> -->