gpt4 book ai didi

java - quartz 调度程序 : Pass database values from one class to another

转载 作者:行者123 更新时间:2023-12-01 09:36:35 26 4
gpt4 key购买 nike

我正在从另一个调度程序(每 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
}
}

最佳答案

您可以使用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");

关于java - quartz 调度程序 : Pass database values from one class to another,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38850610/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com