定时job

来源:互联网 发布:mysql数据库优化面试 编辑:程序博客网 时间:2024/05/04 16:25

1.testjob

public class TestJob implements Job {

public static int i=0;


public void execute(JobExecutionContext context) throws JobExecutionException {
JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
String name=(String) jobDataMap.get("name");
System.out.println(name);
this.doSomeThing(name);
}

/**
* 具体要执行的业务
* @param name
*/
private void doSomeThing(String name)
{
if(name!=null)
{
System.out.println(name+":其实我很在意你......");
}
}

2.QuartzUtils

public QuartzUtils(String name) throws SchedulerException, ParseException
{
Scheduler scheduler=StdSchedulerFactory.getDefaultScheduler();
scheduler.start();
JobDataMap jobDataMap = new JobDataMap();
jobDataMap.put("name", name);
JobDetail jobDetail=new JobDetail("simple",Scheduler.DEFAULT_GROUP,TestJob.class);
jobDetail.setJobDataMap(jobDataMap);
jobDetail.setRequestsRecovery(true);
//CronTrigger
CronTrigger newTriTime=new CronTrigger();
newTriTime.setCronExpression("* 1 * * * ?");
newTriTime.setName("TestTrigger");
newTriTime.setMisfireInstruction(CronTrigger.MISFIRE_INSTRUCTION_DO_NOTHING);
/*Trigger trigger=TriggerUtils.makeSecondlyTrigger(1);
trigger.setName("TestTrigger");
trigger.setStartTime(new Date());*/
scheduler.scheduleJob(jobDetail,newTriTime);
}

public static void main(String[] args) throws SchedulerException, ParseException {
QuartzUtils test=new QuartzUtils("suker");
}

}


jar包列表: commons-collections.jar

                   commons-logging.jar

                   quarzt-all-1.6.1.jar