如何将 Quartz 与 QuartzInitializerListener 一起使用?

发布于 2024-11-01 20:04:45 字数 598 浏览 4 评论 0原文

我无法理解如何将 Quartz 与 QuartzInitializerListener 一起使用。

首先,我在部署描述符中声明该侦听器。那么,我该如何添加我的职位呢?看一下 QuartzInitializerListener 实现,我看到它创建了 SchedulerFactoryScheduler,但我没有看到任何添加作业的方法。工厂收到一个 配置文件,但同样没有与作业相关的内容那里。

我从搜索中只找到了非常简单的示例,所有这些都是关于在 main 方法中实例化所有内容。

谁能给我举一个更真实的例子吗?如果重要的话我正在使用 JBoss 5。谢谢。

I'm having trouble to understand how to use Quartz with QuartzInitializerListener.

First I declare that listener in deployment descriptor. But then, how to I add my jobs? Taking a look at QuartzInitializerListener implementation, I see it creates the SchedulerFactory and Scheduler, but I don't see any way to add jobs. The factory receives a configuration file, but again there's nothing related to the jobs there.

I only found very simples examples from my searches, all about instantiating everything in main method.

Can anyone point me to a more real example? I'm using JBoss 5 if that matters. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

违心° 2024-11-08 20:04:45

您好,这是您查询的答案:

1)第 1 步:编写作业:

package com.hitesh.quartz.test;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class QuartzJob implements Job {

    @Override
    public void execute(JobExecutionContext arg0) throws JobExecutionException {
        System.out.println("Hello");

    }

}

2)编写 web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>TestWebBasedQuartz</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
    <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>com.hitesh.quartz.test.QuartzJobListener</listener-class>
    </listener>
</web-app>

如您所见,有两个侦听器。一个属于 Quartz API,另一个属于您的 API。第一个 Quartz API 监听器将按顺序首先执行。此时我们就已经有了现成的调度器工厂。如果相应的属性“quartz:start-on-load”未指定或指定为 true,则此侦听器也将启动调度程序。

3) 编写你的 Quartz 监听器:

package com.hitesh.quartz.test;


import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.ee.servlet.QuartzInitializerListener;
import org.quartz.impl.StdSchedulerFactory;

public class QuartzJobListener implements ServletContextListener {

    private Scheduler scheduler;
    @Override
    public void contextDestroyed(ServletContextEvent arg0) {

    }

    @Override
    public void contextInitialized(ServletContextEvent ctx) {
        JobDetail job = JobBuilder.newJob(QuartzJob.class)
        .withIdentity("dummyJobName", "group1").build();

        Trigger trigger = TriggerBuilder
        .newTrigger()
        .withIdentity("dummyTriggerName", "group1")
        .withSchedule(
            SimpleScheduleBuilder.simpleSchedule()
                .withIntervalInSeconds(1).repeatForever())
        .build();
        try{
            scheduler = ((StdSchedulerFactory) ctx.getServletContext().getAttribute(QuartzInitializerListener.QUARTZ_FACTORY_KEY)).getScheduler();
            scheduler.scheduleJob(job, trigger);    
        }catch(SchedulerException e){

        }


    }



}

该监听器将在 Quartz 监听器执行后执行。这意味着我们已经准备好了 Scheduler Factory 和一个已启动的调度程序。所以你只需要将作业添加到调度程序即可。正如您所看到的,contextDestroyed 方法是空的,因为调度程序关闭工作将由 Quartz API schedluer 执行。

Hi Here is the answer to your query:

1) Step 1: Write Job:

package com.hitesh.quartz.test;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class QuartzJob implements Job {

    @Override
    public void execute(JobExecutionContext arg0) throws JobExecutionException {
        System.out.println("Hello");

    }

}

2) Write web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>TestWebBasedQuartz</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
    <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>com.hitesh.quartz.test.QuartzJobListener</listener-class>
    </listener>
</web-app>

As you can see there are two listeners. One belongs to Quartz API and other to your API. First Quartz API listener will execute as it comes first in order. At this moment we will have ready made scheduler factory. This listener will start scheduler also if corresponding property "quartz:start-on-load" is either not specified or specified as true.

3) Write your Quartz Listener :

package com.hitesh.quartz.test;


import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.ee.servlet.QuartzInitializerListener;
import org.quartz.impl.StdSchedulerFactory;

public class QuartzJobListener implements ServletContextListener {

    private Scheduler scheduler;
    @Override
    public void contextDestroyed(ServletContextEvent arg0) {

    }

    @Override
    public void contextInitialized(ServletContextEvent ctx) {
        JobDetail job = JobBuilder.newJob(QuartzJob.class)
        .withIdentity("dummyJobName", "group1").build();

        Trigger trigger = TriggerBuilder
        .newTrigger()
        .withIdentity("dummyTriggerName", "group1")
        .withSchedule(
            SimpleScheduleBuilder.simpleSchedule()
                .withIntervalInSeconds(1).repeatForever())
        .build();
        try{
            scheduler = ((StdSchedulerFactory) ctx.getServletContext().getAttribute(QuartzInitializerListener.QUARTZ_FACTORY_KEY)).getScheduler();
            scheduler.scheduleJob(job, trigger);    
        }catch(SchedulerException e){

        }


    }



}

This listener will execute after Quartz listener has executed. This means we have ready made Scheduler Factory with us and a started scheduler. So you only need to add job to scheduler. As you can see the contextDestroyed method is empty as scheduler shutdown work will be carried out by Quartz API schedluer.

自找没趣 2024-11-08 20:04:45

一切都在 源代码您引用的Javadoc:

StdSchedulerFactory 实例存储在 ServletContext 中。您可以获得访问权限
从 ServletContext 实例到工厂,如下所示:

StdSchedulerFactory factory = (StdSchedulerFactory) ctx
       .getAttribute(QuartzInitializerListener.QUARTZ_FACTORY_KEY);

编辑:这意味着当您使用此侦听器时,您可以在每个 servlet/Spring MVC 控制器/...内获取 SchedulerFactory通过运行:

StdSchedulerFactory factory = (StdSchedulerFactory)httpServletRequest
    .getServletContext()
    .getAttribute(QuartzInitializerListener.QUARTZ_FACTORY_KEY);

请注意,上下文侦听器保证在使用任何 servlet 处理传入请求之前执行。这意味着调度程序在使用之前始终会被正确初始化。

下面评论中的讨论摘要,讨论实际上回答了所提出的问题:
如果你想在应用程序启动时添加作业,请编写另一个侦听器(类似于 Quartz 提供的侦听器),查找 StdSchedulerFactory (ServletContext 很容易获得)并执行你想要的操作。监听器保证按照 web.xml 中声明的顺序执行,因此将监听器放在 Quartz 后面。

Everything is described in the source code Javadoc you quote:

A StdSchedulerFactory instance is stored into the ServletContext. You can gain access
to the factory from a ServletContext instance like this:

StdSchedulerFactory factory = (StdSchedulerFactory) ctx
       .getAttribute(QuartzInitializerListener.QUARTZ_FACTORY_KEY);

EDIT: This means that when you are using this listener you can obtain SchedulerFactory inside every servlet/Spring MVC controller/... by running:

StdSchedulerFactory factory = (StdSchedulerFactory)httpServletRequest
    .getServletContext()
    .getAttribute(QuartzInitializerListener.QUARTZ_FACTORY_KEY);

Note that context listeners are guaranteed to be executed before any servlet is used to handle incoming requests. This means that the scheduler will always be properly initialized prior to its usage.

Summary of the discussion in the comments below, the discussion actually answers the question being asked:
If you want to add jobs at application startup, write another listener (similar to the one provided by Quartz), lookup StdSchedulerFactory (ServletContext is easily available) and do what you want. The listeners are guaranteed to execute in the same order as they are declared in web.xml, so put your listener after Quartz one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文