最近工做遇到一个需求:天天都要在某个时间执行一个任务 这个时间是 动态的 从数据库取的
好比今天是要在22点45分15秒执行一个任务 明天的这个时间是 23点45分11秒 以此类推 天天都会有一个动态的时间任务
网上找了不少资料 看了不少博客 ,此时深深感到国内it技术界的丑恶,好多博客都写的残全不全,浪费了我一天多的时间一直网上看资料 下demo跑起来都不是我想要的,通过思考终于解决这个需求,就是经过监听器实现天天固定时间的周期任务,而后经过quartz实如今指定的时间执行业务逻辑
核心代码:
package com.task;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import org.quartz.CronExpression;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory;
import com.db.DBUtil;
import com.quartz.CountJob;
import com.util.CommonUtil;
public class TestTimer {
static int count = 0;
public static void showTimer() throws ClassNotFoundException, SQLException,
ParseException {
TimerTask task = new TimerTask() {
@Override
public void run() {
++count;
System.out.println("时间=" + new Date() + " 执行了" + count + "次"); // 1次
// System.out.println("这里作业务逻辑");
String times = "";
try {
times = DBUtil.getTimes(); //这个值就是数据库取的 很简单的取一个表 大家本身能够在本地建一个表来作测试 固然天天都会有不一样的值 经过另外的项目生成 为了测试 我是本身手动改的数据库值 大家也能够这么作 若是只是想看效果的话
System.out.println("下次任务时间:" + times);
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Scheduler scheduler = null;
try {
scheduler = StdSchedulerFactory.getDefaultScheduler();
} catch (SchedulerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
scheduler.start();
} catch (SchedulerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (count >= 2) {
System.out.println("说明这里是第二次以上的的定时任务:"+count);
try {
scheduler.shutdown(); //关闭以前的调度任务
} catch (SchedulerException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//从新开启任务
try {
scheduler = StdSchedulerFactory.getDefaultScheduler();
} catch (SchedulerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
scheduler.start();
} catch (SchedulerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//开启新的调度任务
JobDetail jd = new JobDetail("test"+count, "test_group"+count,
CountJob.class);
CronTrigger ct = new CronTrigger("test"+count, "test_group"+count);
// 你修改下面的 表达式 就能知足了
// 时间格式: <!-- s m h d m w(?) y(?) -->, 分别对应: 秒>分>小时>日>月>周>年
try {
// SimpleDateFormat sdf = new
// SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// Date date = sdf.parse(times);
SimpleDateFormat sdf = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
Date dateSFM = sdf.parse(times);
Calendar calendarSFM = Calendar.getInstance();
calendarSFM.setTime(dateSFM);
// System.out.println("时:"+calendar.get(Calendar.HOUR_OF_DAY)+"分:"+calendar.get(Calendar.MINUTE)
// +"秒:"+calendar.get(Calendar.SECOND));
String cron = CommonUtil.makeDailyTriggerCron(
calendarSFM.get(Calendar.HOUR_OF_DAY),
calendarSFM.get(Calendar.MINUTE),
calendarSFM.get(Calendar.SECOND));
ct.setCronExpression(cron);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
scheduler.scheduleJob(jd, ct);
} catch (SchedulerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
JobDetail jd = new JobDetail("test", "test_group",
CountJob.class);
CronTrigger ct = new CronTrigger("test", "test_group");
// 你修改下面的 表达式 就能知足了
// 时间格式: <!-- s m h d m w(?) y(?) -->, 分别对应: 秒>分>小时>日>月>周>年
try {
// SimpleDateFormat sdf = new
// SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// Date date = sdf.parse(times);
SimpleDateFormat sdf = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
Date dateSFM = sdf.parse(times);
Calendar calendarSFM = Calendar.getInstance();
calendarSFM.setTime(dateSFM);
// System.out.println("时:"+calendar.get(Calendar.HOUR_OF_DAY)+"分:"+calendar.get(Calendar.MINUTE)
// +"秒:"+calendar.get(Calendar.SECOND));
String cron = CommonUtil.makeDailyTriggerCron(
calendarSFM.get(Calendar.HOUR_OF_DAY),
calendarSFM.get(Calendar.MINUTE),
calendarSFM.get(Calendar.SECOND));
ct.setCronExpression(cron);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
scheduler.scheduleJob(jd, ct);
} catch (SchedulerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
// long delayTime = 24 * 60 * 60 * 1000; // 每24个小时执行一次
long delayTime = 3 * 60 * 1000; // 每分钟执行一次
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);// 天天
calendar.set(year, month, day, 19, 35, 30); // 天天中午12点执行
// calendar.set(year, month, day, calendar.get(Calendar.HOUR_OF_DAY),
// calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND));
Date date = calendar.getTime();
Timer timer = new Timer();
System.out.println(date);
// int period = 2 * 1000;
// 天天的date时刻执行task,每隔2秒重复执行
// timer.schedule(task, date, period);
// 天天的date时刻执行task, 仅执行一次
timer.schedule(task, date, delayTime);
}
}
package com.quartz;
import java.util.Date;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class CountJob implements Job
{
public void execute(JobExecutionContext arg0) throws JobExecutionException
{
System.out.println("我就是具体业务任务啊 ,看到我说明任务执行了!"+ new Date());
}
}
为了看测试效果 我设置的监听器是每3分钟执行一次 生产中只要改为天天就行了

这是测试效果 demo 代码 完整项目我已经放到附件中 供你们下载