Quartz Scheduler和NextFireTime

时间:2013-08-20 01:18:19

标签: java datetime jsf-2 quartz-scheduler jodatime

我正在使用石英调度程序,它工作正常。我有以下cron工作:

<job-scheduling-data
    xmlns="http://www.quartz-scheduler.org/xml/JobSchedulingData"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.quartz-scheduler.org/xml/JobSchedulingData 
    http://www.quartz-scheduler.org/xml/job_scheduling_data_1_8.xsd"
    version="1.8">

    <schedule>
        <job>
            <name>SendSmsEveryMinute</name>
            <group>EveryMinuteGroup</group>
            <description>Run a Job Every Minute</description>
            <job-class>com.sk.model.SendSMSMain</job-class>
        </job>

        <trigger>
            <cron>
                <name>dummyTriggerName</name>
                <job-name>SendSmsEveryMinute</job-name>
                <job-group>EveryMinuteGroup</job-group>
                <!-- It will run every 1 minute -->
                <cron-expression>0 0/1 * * * ?</cron-expression>
            </cron>
        </trigger>
    </schedule>
</job-scheduling-data>

现在,我从现在开始每隔一分钟解雇一次工作。(如果我错了,请纠正我。)好吧,现在我想在石英调度程序中显示所有作业,我的代码是:

public SchedulerViewer() throws SchedulerException {
        ServletContext servletContext = (ServletContext) FacesContext
                .getCurrentInstance().getExternalContext().getContext();

        //Get QuartzInitializerListener 
        StdSchedulerFactory stdSchedulerFactory = (StdSchedulerFactory) servletContext.getAttribute(QuartzInitializerListener.QUARTZ_FACTORY_KEY);

        scheduler = stdSchedulerFactory.getScheduler();

        // loop jobs by group
        for (String groupName : scheduler.getJobGroupNames()) {

            // get jobkey
            for (JobKey jobKey : scheduler.getJobKeys(GroupMatcher.jobGroupEquals(groupName))) {

                String jobName = jobKey.getName();
                String jobGroup = jobKey.getGroup();

                // get job's trigger
                List<Trigger> triggers = (List<Trigger>) scheduler.getTriggersOfJob(jobKey);

                Date date = triggers.get(0).getNextFireTime();
                //converting to joda time
                DateTime dateTime = date == null ? null : new DateTime(triggers.get(0).getNextFireTime());
                DateTimeZone tz = DateTimeZone.forID("Asia/Kathmandu");
                System.out.println("TZ:0--------->"+tz.getID());
                quartzJobList.add(new QuartzJob(jobName, jobGroup, dateTime.withZone(DateTimeZone.forID("Asia/Kathmandu")).toDate()));

            }
        }
    }

嗯,这也有效,但当我在网页上查看时,我看到错误或不同的下一次开火时间。我用joda时间尝试将其转换为当地时间。我错过了什么?

**

  

我在控制台中打印了正确的时间:INFO:The   date2013-08-20T07:10:00.000 + 05:45

**

1 个答案:

答案 0 :(得分:1)

好的,这就是我最后所做的。我将datetime转换为字符串格式,并在JFS视图中使用它。

DateTime dateTime = date == null ? null : new DateTime(triggers.get(0).getNextFireTime());

                if (dateTime != null) {
                    quartzJobList.add(new QuartzJob(jobName, jobGroup, dateTime.toString("MM/dd/yyyy hh:mm:ss")));
            }