0
我目前在Java EE中工作(Eclipse的IDE火星)更新JSP內容與石英
該項目是一個動態的網站,通過Quartz採用了自動觸發工作,以更新其每天的基礎上的內容。 (我在Quartz中使用CronTrigger)
在網站上沒有用戶發佈的內容,我擁有的是一個字符串列表,從中計劃的作業隨機選擇一個字符串。然後它應該將此選定的字符串設置爲網站JSP的內容。
什麼工作:
-
當WAR文件通過 的ServletContextListener部署
- 石英作業開始,作業也正確選擇一個新的String是爲網站
什麼不起作用:
- 當作業觸發時,網站的.JSP上的內容應更新爲最rec選擇字符串。我無法得到這個工作。
什麼我目前想:
- 我有一個正常的Servlet獲得doPost方法內部的最新選擇的字符串,並將此內容在POST請求的屬性。我試圖將這個請求返回到網站,並通過POST方法通過按鈕工作正常。但我不知道如何以編程方式執行此操作,即從Quartz Job中調度。
任何建議,爲更好地做到這一點非常歡迎和讚賞。 我不是一個非常有經驗的程序員(或許你已經知道了) 我現在這樣做的方式感覺非常混亂。
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();
}
}
}
好吧,我剛剛意識到,即使我有一個集中的類MediaHandler,我可以獲取我所有的數據。這是一個非常愚蠢的方式來處理這個問題,我將切換到Spring並從那裏開始。謝謝。 – SYUS 2014-10-27 15:36:41