我正在从另一个调度程序调用调度程序(应该根据某些条件运行)(每1小时运行一次)。当满足'if(diffMinutes> = freq)'条件时,将调用QSchedule3类的execute(JobExecutionContext context)方法。如何将从QSchedule2类获取的数据库的内容传递给该执行方法?可以吗?我可以提一些建议吗?
QSchedule1班级
public class QSchedule1 {
public static void main(String[] args) throws SchedulerException {
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
JobDetail job = newJob(Q2.class).withIdentity("cronJob", "q2Job").build();
CronTrigger cronTrigger = newTrigger().withIdentity("trigger1", "q2Job")
.withSchedule(CronScheduleBuilder.cronSchedule("0 0/1 * 1/1 * ? *")).build();
scheduler.scheduleJob(job, cronTrigger);
scheduler.start();
}
}
QSchedule2班级
public class QSchedule2 implements Job {
private static Date date1 = null;
private static Date date2 = null;
private static String dateStart = "";
private static String dateStop = "";
public void execute(JobExecutionContext context) throws JobExecutionException {
q2();
}
public void q2() {
String exp = "";
try {
Class.forName(driverClassName);
con = DriverManager.getConnection(url, dbUsername, dbPassword);
dateStop = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(System.currentTimeMillis());
ps = con.prepareStatement(
"select report_name,frequency,emailDate from ErrorReport where sendReport='Yes'");
rs = ps.executeQuery();
while (rs.next()) {
dateStart = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(rs.getTimestamp(3));
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
date1 = format.parse(dateStart);
date2 = format.parse(dateStop);
long diff = date2.getTime() - date1.getTime();
long diffMinutes = diff / (60 * 1000) % 60;
String frequency = rs.getString(2);
int freq = Integer.parseInt(frequency);
if (diffMinutes >= freq) {
exp = "0 0/" + freq + " * 1/1 * ? *";
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
JobDetail job = newJob(Q3.class).withIdentity("cronJob", "q3Job").build();
CronTrigger cronTrigger = newTrigger().withIdentity("trigger1", "q3Job")
.withSchedule(CronScheduleBuilder.cronSchedule(exp)).build();
scheduler.scheduleJob(job, cronTrigger);
scheduler.start();
} else {
System.err.println("No email sent");
}
}
} catch (Exception e) {
System.out.println(e);
e.printStackTrace();
} finally {
try {
rs.close();
ps.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
QSchedule3班级
public class QSchedule3 implements Job{
public void execute(JobExecutionContext context) throws JobExecutionException {
//Get database contents here
}
}
答案 0 :(得分:1)
您可以使用JobExecutionContext
将数据传递给作业。
在QSchedule2
:
JobDetail job = newJob(QSchedule2.class).withIdentity("cronJob", "q2Job")
.usingJobData("name", name)
.usingJobData("frequency", frequency)
.usingJobData("date", date)
.build();
在QSchedule3.execute()
:
JobDataMap dataMap = context.getJobDetail().getJobDataMap();
String name = dataMap.getString("name");
String frequency = dataMap.getString("frequency");
String date= dataMap.getString("date");