我目前正在使用Java EE(Eclipse Mars IDE)
该项目是一个动态网站,它使用Quartz自动触发作业来每天更新其内容。 (我在Quartz中使用了CronTrigger)
网站上没有用户发布的内容,我所拥有的是一个字符串列表,预定作业从中随机选择一个字符串。然后,它应该将此选择的String设置为网站JSP的内容。
工作原理:
什么行不通:
我目前正在尝试:
任何有关做得更好的建议都非常受欢迎和赞赏。 我不是一个非常有经验的程序员(也许你已经可以说了) 而我现在这样做的方式感觉非常......混乱。
package model;
import java.util.Date;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobKey;
import org.quartz.SchedulerContext;
import org.quartz.SchedulerException;
public class RefreshQuoteJob implements Job {
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("[INFO] Executing refreshJob");
JobKey jobKey = context.getJobDetail().getKey();
SchedulerContext schedulerContext = null;
System.out.println("[INFO] RefreshQuote says: " + jobKey + " executing at " + new Date());
try {
schedulerContext = context.getScheduler().getContext();
} catch (SchedulerException e1) {
e1.printStackTrace();
}
MediaHandler handler = (MediaHandler)schedulerContext.get("handler");
//This selects the new String from the list, that string should be displayed on the JSP
handler.refreshDB();
//Update the content of the JSP here
System.out.println("[INFO] refreshJob Executed");
}
}
package model;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;
import java.util.Random;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
public class RefreshQuoteTrigger {
public SchedulerFactory sf;
public Scheduler refreshSchedule;
public void run(MediaHandler handler) throws SchedulerException {
System.out.println("[INFO] Running RefreshTrigger");
sf = new StdSchedulerFactory();
Random randomPostingTime = new Random();
refreshSchedule = sf.getScheduler();
//sf.getScheduler().getContext().put("quoteDB", quoteDB);
//I give the MediaHandler object to the job via this
sf.getScheduler().getContext().put("handler", handler);
JobDetail refreshJob = newJob(RefreshQuoteJob.class)
.withIdentity("refreshJob", "group1")
.build();
//Handy?
//.withSchedule(dailyAtHourAndMinute(07, (15+randomPostingTime.nextInt(59)))
CronTrigger refreshTriggerDaily = newTrigger()
.withIdentity("quoteRefreshSchedule", "group1")
.withSchedule(CronScheduleBuilder.cronSchedule("0/"+randomPostingTime.nextInt(59)+" "+randomPostingTime.nextInt(7)+" 08 ? * *"))//This should mean it posts every day at 08:random minutes(between 0 and 7):random seconds(between 0 and 59)//0 15 10 ? * * //Every day at 10.15am
.build();
CronTrigger refreshTrigger = newTrigger()
.withIdentity("quoteRefreshSchedule", "group1")
.withSchedule(CronScheduleBuilder.cronSchedule("0/"+randomPostingTime.nextInt(59)+" * * * * ?"))//0 15 10 ? * * //Every day at 10.15am
.build();
refreshSchedule.scheduleJob(refreshJob, refreshTrigger);
refreshSchedule.start();
//This gives quartz 5 seconds to run its jobs and then sleeps the thread
//In the final program I should give it enough time to run
//Around Thread.sleep(300L * 1000L)
try {
Thread.sleep(30L * 1000L);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void forceShutdown() {
try {
refreshSchedule.shutdown(true);
} catch (SchedulerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
答案 0 :(得分:0)
您是如何尝试在JSP中获取字符串的?你能以某种方式访问Quartz上下文吗?即使你有,这似乎有点做作。
通常对于类似的东西,你会使用可以从Quartz作业和JSP访问的Singleton。您如何访问Singleton取决于您使用的技术。您可以使用Spring,CDI等。您也可以在某个地方使用静态变量来快速完成工作:
public class MediaHandler {
private static MediaHandler instance = new MediaHandler ();
public static MediaHandler getInstance() {return instance;}
}
然后从Quartz作业和Servlet中可以访问它:
MediaHandler handler=MediaHandler.getInstance();
注意:从长远来看,可能使用类似Spring的CDI这样的框架是一个更好的主意。