用quartz更新JSP内容

时间:2014-10-27 14:53:01

标签: java eclipse jsp servlets quartz-scheduler

我目前正在使用Java EE(Eclipse Mars IDE)

该项目是一个动态网站,它使用Quartz自动触发作业来每天更新其内容。 (我在Quartz中使用了CronTrigger)

网站上没有用户发布的内容,我所拥有的是一个字符串列表,预定作业从中随机选择一个字符串。然后,它应该将此选择的String设置为网站JSP的内容。

工作原理:

  • 当通过a部署WAR文件时,Quartz作业启动 ServletContextListener,Job也正确地选择一个新的String作为网站的当前内容

什么行不通:

  • 当作业触发时,网站的.JSP上的内容应更新为最近选择的字符串。我无法让这个工作。

我目前正在尝试:

  • 我有一个普通的Servlet,它在doPost方法中获取最新选择的String,并将此内容设置为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();
        }
    }
}

1 个答案:

答案 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这样的框架是一个更好的主意。