java读取quartz脚本_Java Quartz 自动化框架 定时任务(执行BAT脚本进行构建)

代码分享->

定时任务代码:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public class QuartzJob implements Job{

static Logger log=Logger.getLogger(QuartzJob.class);

public void killProcess() {

Runtime rt = Runtime.getRuntime();

Process p = null;

try {

rt.exec("cmd.exe /C start wmic process where name='cmd.exe' call terminate");

} catch (IOException e) {

e.printStackTrace();

}

}

@Override

public void execute(JobExecutionContext arg0) throws JobExecutionException {

String[] cmd={"cmd.exe","/C","start","/b",System.getProperty("user.dir")+File.separator+"build.bat"};

Process process = null;

try {

process = Runtime.getRuntime().exec(cmd);

InputStream in=process.getInputStream();

int c=0;

while(c!=-1)

{

try {

c=in.read();

} catch (IOException e) {

e.printStackTrace();

}

}

in.close();

process.waitFor();

int i = process.exitValue();

if (i == 0) {

System.out.println("执行完成.");

TestReport report=new TestReport();

System.out.println(report.getFailedTests().size()+""+report.getSkippedTests().size()+""+report.getPassedTests().size());

} else {

System.out.println("执行失败.");

}

process.destroy();

process = null;

new QuartzJob().killProcess();

} catch (IOException e1) {

e1.printStackTrace();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

执行任务代码:

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;

import org.quartz.CronTrigger;

import org.quartz.JobDetail;

import org.quartz.Scheduler;

import org.quartz.SchedulerException;

import org.quartz.SchedulerFactory;

import org.quartz.impl.JobDetailImpl;

import org.quartz.impl.StdSchedulerFactory;

import org.quartz.impl.triggers.CronTriggerImpl;

public class Runjob {

public static void RunJob(String time) throws SchedulerException, ParseException {

SchedulerFactory sf = new StdSchedulerFactory();

Scheduler sched = sf.getScheduler();

@SuppressWarnings("deprecation")

JobDetail job =new JobDetailImpl("job1", "group1", QuartzJob.class);

@SuppressWarnings("deprecation")

CronTrigger trigger = new CronTriggerImpl("trigger1", "group1");

((CronTriggerImpl) trigger).setCronExpression(time);

Date ft = sched.scheduleJob(job, trigger);

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");

System.out.println(

job.getKey() + " 已被安排执行于: " + sdf.format(ft) + ",并且以如下重复规则重复执行: " + trigger.getCronExpression());

// 开始执行,start()方法被调用后,计时器就开始工作,计时调度中允许放入N个Job

sched.start();

try {

// 主线程等待一分钟

Thread.sleep(60L * 1000L);

} catch (Exception e) {

}

// 关闭定时调度,定时器不再工作

//sched.shutdown(true);

}

public static void main(String[] args) throws Exception{

RunJob("0 07 22 ? * *");

}

}

小奋斗文章

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值