简体   繁体   English

用石英更新JSP内容

[英]updating JSP content with quartz

I'm currently working in Java EE (Eclipse Mars IDE) 我目前正在Java EE(Eclipse Mars IDE)中工作

The project is a dynamic website that uses an automatically triggering Job via Quartz to update its content on a daily basis. 该项目是一个动态网站,每天通过Quartz使用自动触发的Job来更新其内容。 (I'm using a CronTrigger for this in Quartz) (我正在Quartz中使用CronTrigger)

There is no user posted content on the website, all I have is a list of Strings from which the scheduled Job chooses one String randomly. 网站上没有用户发布的内容,我所拥有的只是一个字符串列表,计划的作业从列表中随机选择一个字符串。 It should then set this chosen String as the content for the website's JSP. 然后,应将此选择的String设置为网站JSP的内容。

What does work: 什么有效:

  • The Quartz Job starts when the WAR file is deployed through a ServletContextListener, the Job also correctly chooses a new String to be the current content for the website 当通过ServletContextListener部署WAR文件时,Quartz作业开始,该作业还正确地选择了一个新的String作为网站的当前内容。

What does not work: 什么不起作用:

  • When the Job triggers, the content on the website's .JSP should be updated to the most recently chosen String. 作业触发时,网站.JSP上的内容应更新为最新选择的字符串。 I cannot get this to work. 我无法使它正常工作。

What I am currently trying: 我目前正在尝试的是:

  • I have a normal Servlet that gets the latest chosen String inside of the doPost method and sets this content as the attribute in the POST's request. 我有一个普通的Servlet,它在doPost方法中获取最新选择的String并将此内容设置为POST请求中的属性。 I tried to return this request to the website and that works fine through a button with POST method. 我试图将此请求返回到网站,并且通过带有POST方法的按钮可以正常工作。 But I can't figure out how to do this programatically, aka from the scheduled Quartz Job. 但是我无法从计划的Quartz Job中找出如何以编程方式执行此操作。

Any suggestions for doing this better are very welcome and appreciated. 任何对此做得更好的建议都将受到欢迎和赞赏。 I'm not a very experienced programmer (perhaps you could tell already) and the way I'm doing it now feels very... disorganized. 我不是一个非常有经验的程序员(也许您已经知道了),现在我的工作方式感觉非常……杂乱无章。

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();
        }
    }
}

How are you trying to get the strings in your JSP? 您如何尝试在JSP中获取字符串? Are you able to access the Quartz context somehow? 您能以某种方式访问​​Quartz上下文吗? Even if you have, that would seem a bit contrived. 即使您有,这似乎也有些人为。

Normally for something like this you would use a Singleton that can be accessed from the Quartz job and from the JSP. 通常,对于这样的事情,您将使用可从Quartz作业和JSP访问的Singleton。 How you access your Singleton depends on what technology you are using. 您如何访问Singleton取决于您所使用的技术。 You can use Spring, CDI, etc. You can also just have a static variable somewhere for a quick job: 您可以使用Spring,CDI等。也可以在某处使用静态变量来快速完成工作:

public class MediaHandler {
    private static MediaHandler  instance = new MediaHandler ();
    public static MediaHandler getInstance() {return instance;}
}

Then from your Quartz job and Servlet you can access it: 然后从Quartz作业和Servlet中可以访问它:

MediaHandler handler=MediaHandler.getInstance();

Note: probably using a framework like Spring of CDI is a better idea in the long term. 注意:从长远来看,可能使用像Spring CDI这样的框架是一个更好的主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM