在简单的Webapp中使用石英时间表的时间表作业

问题描述 投票:2回答:1

我有一个需要每10分钟安排一次作业的要求,该作业在简单的Web应用程序中运行,因此我们无法使用Spring。我遍历了一些教程,但无法理解我需要做什么,请让我知道我需要执行的步骤吗?

我有一些具体疑问:

  1. 我需要quartz.properties吗?
  2. 我需要quartz.xml吗?不能使用某些类/ servlet来做同样的事情吗?
  3. 调度程序将如何初始化,谁将触发Job?

我有一个独立的程序来执行作业,我可以在任何servlet的初始化中编写该程序,然后在容器<load-on-startup>1</load-on-startup>的开头启动该servlet。这是对的吗?

java web-applications quartz-scheduler
1个回答
4
投票
  1. 如果quartz没有找到quartz.properties文件,它将使用默认值。显然,如果要指定默认值以外的值,则确实需要它。
  2. 您不需要quartz.xml,可以通过代码配置作业(以下解决方案描述了两种方式)
  3. 以下是您的案例的代码示例,该案例每10分钟重复一次:
public class HelloJob implements Job {

    @Override
    public void execute(JobExecutionContext context)
            throws JobExecutionException {
        //put your code here
    }

}

由于您不想使用xml而是想要Java代码,因此可以从ServletContext获取StdSchedulerFactory实例来配置调度程序,并且为了在初始化时调用该代码,必须将其放在侦听器中:

public class HelloQuartzListener implements ServletContextListener {

    private Scheduler scheduler;

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {

    }

    @Override
    public void contextInitialized(ServletContextEvent ctx) {
        // define the job and tie it to our HelloJob class
        JobDetail job = JobBuilder.newJob(HelloJob.class)
                .withIdentity("myJob", "group1").build();

        // Trigger the job to run now, and then every 10 minutes
        Trigger trigger = TriggerBuilder
                .newTrigger()
                .withIdentity("myTrigger", "group1")
                .startNow()
                .withSchedule(
                        SimpleScheduleBuilder.simpleSchedule()
                                .withIntervalInMinutes(10).repeatForever())
                .build();
        // Tell quartz to schedule the job using our trigger

        try {
            scheduler = ((StdSchedulerFactory) ctx.getServletContext()
                    .getAttribute(
                            QuartzInitializerListener.QUARTZ_FACTORY_KEY))
                    .getScheduler();
            scheduler.scheduleJob(job, trigger);
        } catch (SchedulerException e) {

        }
    }
}

为了从您的Web应用程序初始化石英,您需要通过将以下内容添加到web.xml中来配置QuartzInitializerListener。请注意,最后我们还添加了我们先前创建的监听器,该监听器通过Java代码配置了作业。重要的是将它放在QuartzInitializerListener之后,因为需要首先调用QuartzInitializerListener以便将StdSchedulerFactory放入上下文中以便于HelloJobListener得到它:

<context-param>
    <param-name>quartz:shutdown-on-unload</param-name>
    <param-value>true</param-value>
</context-param>
<context-param>
    <param-name>quartz:wait-on-shutdown</param-name>
    <param-value>true</param-value>
</context-param>
<context-param>
    <param-name>quartz:start-on-load</param-name>
    <param-value>true</param-value>
</context-param>

<listener>
    <listener-class>org.quartz.ee.servlet.QuartzInitializerListener</listener-class>
</listener>

<listener>
    <listener-class>yourpackage.HelloQuartzListener</listener-class>
</listener>

******如果确实要设置自己的属性值,则可以通过将属性文件也添加到web.xml中来添加属性文件并包括其路径:

 <context-param>
        <param-name>quartz:config-file</param-name>
        <param-value>/WEB-INF/quartz.properties</param-value>
    </context-param>

****** ..并且,如果您决定更喜欢使用xml,则可以通过添加以下内容来在属性文件中指定该名称:

 org.quartz.plugin.jobInitializer.class =org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin 
org.quartz.plugin.jobInitializer.fileNames = quartz.xml 

其中“ quartz.xml”将包含作业详细信息(当然,在这种情况下,请删除HelloJobListener配置和类):

<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>HelloJob</name>
            <group>group1</group>
            <description>hello</description>
            <job-class>yourpackage.HelloJob</job-class>
        </job>

        <trigger>
            <cron>
                <name>myTrigger</name>
                <job-name>HelloJob</job-name>
                <job-group>group1</job-group>
                <!-- It will run every 10 minutes -->
                <cron-expression>0 0/10 * * * ?</cron-expression>
            </cron>
        </trigger>
    </schedule>
</job-scheduling-data>
© www.soinside.com 2019 - 2024. All rights reserved.