[size=medium][/size][color=blue]quartz 是用来进行作业调度开源框架,因公司要求,自己做了一个从java项目打包到最后进行自动测试的例子,我也是刚刚接触这个东西,讲的不好,大家别拍啊。。。。
不费话了,大家一起来看看这个例子吧,写的不好,大家提点意见哦~~~~~
因为我是通过调用一些bat文件来完成功能,所以这个将类命名:AutoRunBat.java
import org.quartz.CronExpression;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
public class AutoRunBat
{
public void taskList() throws SchedulerException
{
// Initiate a Schedule Factory (初始化一个调度器工厂)
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
// Retrieve a scheduler from schedule factory(这里是初始化一个调度器)
Scheduler scheduler = schedulerFactory.getScheduler();
// Initiate JobDetail with job name, job group, and executable job class(这里相当于是在定义具体的任务吧)
JobDetail jobDetail = new JobDetail("jobDetail", "jobDetailGroup", RunBatTask.class);
// Initiate CronTrigger with its name and group name
//通常我们常用的有两个Trigger(CronTrigger 和 SimpleTrigger)
CronTrigger cronTrigger = new CronTrigger("cronTrigger", "triggerGroup");
try
{
// setup CronExpression(定义作业被调度的时间,具体情况,设置不同)
CronExpression cexp = new CronExpression("0 54 14 * * ?");
// Assign the CronExpression to CronTrigger
cronTrigger.setCronExpression(cexp);
}
catch (Exception e)
{
e.printStackTrace();
}
// schedule a job with JobDetail and Trigger(传入设置的参数)
scheduler.scheduleJob(jobDetail, cronTrigger);
// start the scheduler(启动调动)
scheduler.start();
}
public static void main(String[] args)
{
try
{
AutoRunBat autoRunBat = new AutoRunBat();
autoRunBat.taskList();
}
catch (SchedulerException e)
{
e.printStackTrace();
}
}
}
上面是设置作业调度,从上面的代码可以看出,我们具体要完成的事项应该是在RunBatTask这个类里面,对的,这个类就是实现了quartz的job接口,这个接口只有一个方法execute,看起来和struts很相像吧。下面我们来看看这个类的编写:
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Date;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import com.sunny.common.service.CheckListManager;
import com.sunny.selenium.SunnyTestSuite;
public class RunBatTask implements Job
{
public void execute(JobExecutionContext context)
throws JobExecutionException
{
try
{
//这里是我自己写的一个去update CVS上文件的类哈,这个就暂不贴出来,呵呵
String idStr = "No use";
Method method = CheckListManager.class.getMethod("main", String[].class);
method.invoke(null, (Object) new String[] { idStr });
//因为是多个任务之间需要一定的时间间隔,所以这里设置睡眠时间,下同:
Thread.sleep(100 * 1000);
//下面是通过去执行bat文件来完成:将script跑到指定数据库,项目打包,启动server工作
//Run script into database
System.out.println("Run Script Into DataBase! - " + new Date());
Runtime.getRuntime().exec("cmd /k start d:\\batchFile\\RunScript.bat");
Thread.sleep(10 * 1000);
//Execute ant build.xml
System.out.println("Execute Ant build.xml! - " + new Date());
Runtime.getRuntime().exec("cmd /k start d:\\batchFile\\RunAntBuild.bat");
Thread.sleep(50 * 1000);
//start weblogic server
System.out.println("Start weblogic server! - " + new Date());
Runtime.getRuntime().exec("cmd /k start d:\\batchFile\\RunStartWeblogic.bat");
Thread.sleep(100 * 1000);
//start selenium server
System.out.println("Start selenium server! - " + new Date());
Runtime.getRuntime().exec("cmd /k start d:\\batchFile\\RunStartSelenium.bat");
Thread.sleep(50 * 1000);
//invoke class call method
//这里是我的自动测试类,具体类也不给出来了,大家可以去看看Selenium,很好玩的。
SunnyTestSuite.suite();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
到这里一个简单的quartz的例子写完了,一起来测试一下,呵呵。程序会在自己设定的时候自动执行哦。。。
谢谢大家,讲的不好,多给点意见,偶会改进的。[/color]
不费话了,大家一起来看看这个例子吧,写的不好,大家提点意见哦~~~~~
因为我是通过调用一些bat文件来完成功能,所以这个将类命名:AutoRunBat.java
import org.quartz.CronExpression;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
public class AutoRunBat
{
public void taskList() throws SchedulerException
{
// Initiate a Schedule Factory (初始化一个调度器工厂)
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
// Retrieve a scheduler from schedule factory(这里是初始化一个调度器)
Scheduler scheduler = schedulerFactory.getScheduler();
// Initiate JobDetail with job name, job group, and executable job class(这里相当于是在定义具体的任务吧)
JobDetail jobDetail = new JobDetail("jobDetail", "jobDetailGroup", RunBatTask.class);
// Initiate CronTrigger with its name and group name
//通常我们常用的有两个Trigger(CronTrigger 和 SimpleTrigger)
CronTrigger cronTrigger = new CronTrigger("cronTrigger", "triggerGroup");
try
{
// setup CronExpression(定义作业被调度的时间,具体情况,设置不同)
CronExpression cexp = new CronExpression("0 54 14 * * ?");
// Assign the CronExpression to CronTrigger
cronTrigger.setCronExpression(cexp);
}
catch (Exception e)
{
e.printStackTrace();
}
// schedule a job with JobDetail and Trigger(传入设置的参数)
scheduler.scheduleJob(jobDetail, cronTrigger);
// start the scheduler(启动调动)
scheduler.start();
}
public static void main(String[] args)
{
try
{
AutoRunBat autoRunBat = new AutoRunBat();
autoRunBat.taskList();
}
catch (SchedulerException e)
{
e.printStackTrace();
}
}
}
上面是设置作业调度,从上面的代码可以看出,我们具体要完成的事项应该是在RunBatTask这个类里面,对的,这个类就是实现了quartz的job接口,这个接口只有一个方法execute,看起来和struts很相像吧。下面我们来看看这个类的编写:
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Date;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import com.sunny.common.service.CheckListManager;
import com.sunny.selenium.SunnyTestSuite;
public class RunBatTask implements Job
{
public void execute(JobExecutionContext context)
throws JobExecutionException
{
try
{
//这里是我自己写的一个去update CVS上文件的类哈,这个就暂不贴出来,呵呵
String idStr = "No use";
Method method = CheckListManager.class.getMethod("main", String[].class);
method.invoke(null, (Object) new String[] { idStr });
//因为是多个任务之间需要一定的时间间隔,所以这里设置睡眠时间,下同:
Thread.sleep(100 * 1000);
//下面是通过去执行bat文件来完成:将script跑到指定数据库,项目打包,启动server工作
//Run script into database
System.out.println("Run Script Into DataBase! - " + new Date());
Runtime.getRuntime().exec("cmd /k start d:\\batchFile\\RunScript.bat");
Thread.sleep(10 * 1000);
//Execute ant build.xml
System.out.println("Execute Ant build.xml! - " + new Date());
Runtime.getRuntime().exec("cmd /k start d:\\batchFile\\RunAntBuild.bat");
Thread.sleep(50 * 1000);
//start weblogic server
System.out.println("Start weblogic server! - " + new Date());
Runtime.getRuntime().exec("cmd /k start d:\\batchFile\\RunStartWeblogic.bat");
Thread.sleep(100 * 1000);
//start selenium server
System.out.println("Start selenium server! - " + new Date());
Runtime.getRuntime().exec("cmd /k start d:\\batchFile\\RunStartSelenium.bat");
Thread.sleep(50 * 1000);
//invoke class call method
//这里是我的自动测试类,具体类也不给出来了,大家可以去看看Selenium,很好玩的。
SunnyTestSuite.suite();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
到这里一个简单的quartz的例子写完了,一起来测试一下,呵呵。程序会在自己设定的时候自动执行哦。。。
谢谢大家,讲的不好,多给点意见,偶会改进的。[/color]