package com.ibm.scheduler;
import java.util.Timer;
import java.util.TimerTask;
public class TimerTest extends TimerTask {
private String jobName = "";
public TimerTest(String jobName) {
super();
this.jobName = jobName;
}
@Override
public void run() {
System.out.println("execute " + jobName);
}
public static void main(String[] args) {
Timer timer = new Timer();
long delay1 = 1 * 1000;
long period1 = 1000;
// 从现在开始 1 秒钟之后,每隔 1 秒钟执行一次 job1
timer.schedule(new TimerTest("job1"), delay1, period1);
long delay2 = 2 * 1000;
long period2 = 2000;
// 从现在开始 2 秒钟之后,每隔 2 秒钟执行一次 job2
timer.schedule(new TimerTest("job2"), delay2, period2);
}
}
execute job1 execute job1 execute job2 execute job1 execute job1 execute job2
package com.ibm.scheduler;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class ScheduledExecutorTest implements Runnable {
private String jobName = "";
public ScheduledExecutorTest(String jobName) {
super();
this.jobName = jobName;
}
@Override
public void run() {
System.out.println("execute " + jobName);
}
public static void main(String[] args) {
ScheduledExecutorService service = Executors.newScheduledThreadPool(10);
long initialDelay1 = 1;
long period1 = 1;
// 从现在开始1秒钟之后,每隔1秒钟执行一次job1
service.scheduleAtFixedRate(
new ScheduledExecutorTest("job1"), initialDelay1,
period1, TimeUnit.SECONDS);
long initialDelay2 = 1;
long delay2 = 1;
// 从现在开始2秒钟之后,每隔2秒钟执行一次job2
service.scheduleWithFixedDelay(
new ScheduledExecutorTest("job2"), initialDelay2,
delay2, TimeUnit.SECONDS);
}
}
execute job2 execute job1 execute job2 execute job1 execute job2 execute job1
package com.ibm.scheduler;
import java.util.Calendar;
import java.util.Date;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class ScheduledExceutorTest2 extends TimerTask {
private String jobName = "";
public ScheduledExceutorTest2(String jobName) {
super();
this.jobName = jobName;
}
@Override
public void run() {
System.out.println("Date = "+new Date()+", execute " + jobName);
}
/**
* 计算从当前时间currentDate开始,满足条件dayOfWeek, hourOfDay,
* minuteOfHour, secondOfMinite的最近时间
* @return
*/
public Calendar getEarliestDate(Calendar currentDate, int dayOfWeek,
int hourOfDay, int minuteOfHour, int secondOfMinite) {
//计算当前时间的WEEK_OF_YEAR,DAY_OF_WEEK, HOUR_OF_DAY, MINUTE,SECOND等各个字段值
int currentWeekOfYear = currentDate.get(Calendar.WEEK_OF_YEAR);
int currentDayOfWeek = currentDate.get(Calendar.DAY_OF_WEEK);
int currentHour = currentDate.get(Calendar.HOUR_OF_DAY);
int currentMinute = currentDate.get(Calendar.MINUTE);
int currentSecond = currentDate.get(Calendar.SECOND);
//如果输入条件中的dayOfWeek小于当前日期的dayOfWeek,则WEEK_OF_YEAR需要推迟一周
boolean weekLater = false;
if (dayOfWeek < currentDayOfWeek) {
weekLater = true;
} else if (dayOfWeek == currentDayOfWeek) {
//当输入条件与当前日期的dayOfWeek相等时,如果输入条件中的
//hourOfDay小于当前日期的
//currentHour,则WEEK_OF_YEAR需要推迟一周
if (hourOfDay < currentHour) {
weekLater = true;
} else if (hourOfDay == currentHour) {
//当输入条件与当前日期的dayOfWeek, hourOfDay相等时,
//如果输入条件中的minuteOfHour小于当前日期的
//currentMinute,则WEEK_OF_YEAR需要推迟一周
if (minuteOfHour < currentMinute) {
weekLater = true;
} else if (minuteOfHour == currentSecond) {
//当输入条件与当前日期的dayOfWeek, hourOfDay,
//minuteOfHour相等时,如果输入条件中的
//secondOfMinite小于当前日期的currentSecond,
//则WEEK_OF_YEAR需要推迟一周
if (secondOfMinite < currentSecond) {
weekLater = true;
}
}
}
}
if (weekLater) {
//设置当前日期中的WEEK_OF_YEAR为当前周推迟一周
currentDate.set(Calendar.WEEK_OF_YEAR, currentWeekOfYear + 1);
}
// 设置当前日期中的DAY_OF_WEEK,HOUR_OF_DAY,MINUTE,SECOND为输入条件中的值。
currentDate.set(Calendar.DAY_OF_WEEK, dayOfWeek);
currentDate.set(Calendar.HOUR_OF_DAY, hourOfDay);
currentDate.set(Calendar.MINUTE, minuteOfHour);
currentDate.set(Calendar.SECOND, secondOfMinite);
return currentDate;
}
public static void main(String[] args) throws Exception {
ScheduledExceutorTest2 test = new ScheduledExceutorTest2("job1");
//获取当前时间
Calendar currentDate = Calendar.getInstance();
long currentDateLong = currentDate.getTime().getTime();
System.out.println("Current Date = " + currentDate.getTime().toString());
//计算满足条件的最近一次执行时间
Calendar earliestDate = test
.getEarliestDate(currentDate, 3, 16, 38, 10);
long earliestDateLong = earliestDate.getTime().getTime();
System.out.println("Earliest Date = "
+ earliestDate.getTime().toString());
//计算从当前时间到最近一次执行时间的时间间隔
long delay = earliestDateLong - currentDateLong;
//计算执行周期为一星期
long period = 7 * 24 * 60 * 60 * 1000;
ScheduledExecutorService service = Executors.newScheduledThreadPool(10);
//从现在开始delay毫秒之后,每隔一星期执行一次job1
service.scheduleAtFixedRate(test, delay, period,
TimeUnit.MILLISECONDS);
}
}
Current Date = Wed Feb 02 17:32:01 CST 2011 Earliest Date = Tue Feb 8 16:38:10 CST 2011 Date = Tue Feb 8 16:38:10 CST 2011, execute job1 Date = Tue Feb 15 16:38:10 CST 2011, execute job1
package com.ibm.scheduler;
import java.util.Date;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.helpers.TriggerUtils;
public class QuartzTest implements Job {
@Override
//该方法实现需要执行的任务
public void execute(JobExecutionContext arg0) throws JobExecutionException {
System.out.println("Generating report - "
+ arg0.getJobDetail().getFullName() + ", type ="
+ arg0.getJobDetail().getJobDataMap().get("type"));
System.out.println(new Date().toString());
}
public static void main(String[] args) {
try {
// 创建一个Scheduler
SchedulerFactory schedFact =
new org.quartz.impl.StdSchedulerFactory();
Scheduler sched = schedFact.getScheduler();
sched.start();
// 创建一个JobDetail,指明name,groupname,以及具体的Job类名,
//该Job负责定义需要执行任务
JobDetail jobDetail = new JobDetail("myJob", "myJobGroup",
QuartzTest.class);
jobDetail.getJobDataMap().put("type", "FULL");
// 创建一个每周触发的Trigger,指明星期几几点几分执行
Trigger trigger = TriggerUtils.makeWeeklyTrigger(3, 16, 38);
trigger.setGroup("myTriggerGroup");
// 从当前时间的下一秒开始执行
trigger.setStartTime(TriggerUtils.getEvenSecondDate(new Date()));
// 指明trigger的name
trigger.setName("myTrigger");
// 用scheduler将JobDetail与Trigger关联在一起,开始调度任务
sched.scheduleJob(jobDetail, trigger);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Generating report - myJobGroup.myJob, type =FULL Tue Feb 8 16:38:00 CST 2011 Generating report - myJobGroup.myJob, type =FULL Tue Feb 15 16:38:00 CST 2011
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有