自動化框架---定時任務(執行BAT腳本進行構建)

定時任務代碼:java

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();
        }       

        
    }  
    
}

執行任務代碼:ide

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 ? * *");
    }    
     
}
相關文章
相關標籤/搜索