如果您正在使用Spring Boot,可以使用TaskExecutor bean并配置池大小
(http://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html#scheduling-task-executor-usage),然后使用TaskScheduler来定义任务何时运行。事件参数的值取自数据库(即Object)。
我曾经创建过调度程序,但我使用了Quartz。我创建了一个每隔1分钟触发一次的新工作,并在数据库中搜索EmailQueue,如果找到电子邮件,则尝试发送它,当发送出现错误时,它不会删除队列中的电子邮件,并在日志表中写入有关错误的详细信息。调度程序(1分钟)是从数据库设置的。对于您的情况,您应该使用: newJob(QuartzJob.class).withIntervalInMilliseconds。
http://quartz-scheduler.org/api/2.2.0/org/quartz/SimpleScheduleBuilder.html#withIntervalInMilliseconds(long)
@Service
public class MyQuartz implements InitializingBean, DisposableBean {
@Autowired
StdSchedulerFactory stdSchedulerFactory;
@Autowired
TableConfiguration tableConfiguration;
Scheduler sched = null;
JobDetail job, job2;
JobDetail[] jobs = new JobDetail[2];
public void initIt() throws Exception {
try {
System.out.println("Shedulling a job...");
sched = stdSchedulerFactory.getScheduler();
} catch (SchedulerException e) {
System.err.println("Could not create scheduler.");
e.printStackTrace();
}
try {
System.out.println("Starting scheduler");
sched.start();
} catch (SchedulerException e) {
System.err.println("Could not start scheduler.");
e.printStackTrace();
}
job = newJob(QuartzJob.class).withIdentity("myJob", "group1").build();
job2 = newJob(QuartzCronJob.class).withIdentity("myCronJob", "group2")
.build();
jobs[0] = job;
Trigger trigger = newTrigger()
.withIdentity("myTrigger", "group1")
.startNow()
.withSchedule(
simpleSchedule().withIntervalInMinutes(
tableConfiguration.getTimeInterval())
.repeatForever()).build();
try {
sched.scheduleJob(job, trigger);
System.out.println("..job schedulled.");
} catch (SchedulerException e) {
System.err.println("Could not schedulle a job.");
e.printStackTrace();
}
}
@Override
public void destroy() throws Exception {
try {
System.out.println("Stopping scheduler...");
for (int i = 0; i < jobs.length; i++) {
sched.deleteJob(jobs[i].getKey());
}
sched.shutdown(true);
System.out.println("...scheduler Terminated. Good Bye.");
} catch (SchedulerException e) {
System.err.println("ERROR, scheduler cannot be stopped.");
e.printStackTrace();
}
}
@Override
public void afterPropertiesSet() throws Exception {
}
}
并且
@Service
@Transactional
public class QuartzCronJob implements Job {
@Override
public void execute(JobExecutionContext context)
throws JobExecutionException {
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = new GregorianCalendar();
String schedulled = sdf.format(calendar.getTime());
System.out.println("LIST now " + schedulled);
List<EmailQueue> emails = emailQueueDao.findAllForToday(schedulled);
if (emails != null) {
for (EmailQueue email : emails) {
try {
sendmail.sendNotification(email.getFrom(), email.getTo(),
email.getSubject(), email.getMessage(), "Sched.");
emailQueueDao.delete(email.getId());
System.out.println("Email sucessfully sent and deleted.");
} catch (Exception e) {
sendmail.logEmail(LoggerSeverity.ERROR,
"Could not send schedulled email", "Sched.");
System.err.println("Could not send schedulled email");
}
}
}
}
}
我的代码没有使用连接池,但是由于每分钟只运行一次,所以我从数据库中检索不到超过3封电子邮件 :)