Goofy  O_o Goofy O_o - 3 months ago 12
Java Question

QuartzScheduler: Pass database values from one class to another

I'm calling a scheduler(should run depending on certain conditions) from another scheduler(runs every 1 hour). When 'if (diffMinutes >= freq)' condition is satisfied, the execute(JobExecutionContext context) method from QSchedule3 Class will be called. How to pass the contents of the database obtained from QSchedule2 Class to that execute method? Can it be done? May I have some suggestions?

QSchedule1 Class

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 Class

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 Class

public class QSchedule3 implements Job{
public void execute(JobExecutionContext context) throws JobExecutionException {
//Get database contents here
}
}

Answer

You can use the JobExecutionContext to pass data to a Job.

In QSchedule2:

JobDetail job = newJob(QSchedule2.class).withIdentity("cronJob", "q2Job")
            .usingJobData("name", name)
            .usingJobData("frequency", frequency)
            .usingJobData("date", date)
            .build();

In QSchedule3.execute():

JobDataMap dataMap = context.getJobDetail().getJobDataMap();
String name = dataMap.getString("name");
String frequency = dataMap.getString("frequency");
String date= dataMap.getString("date");
Comments