簡體   English   中英

如何在QuartzInitializerListener中使用Quartz?

[英]How to use Quartz with QuartzInitializerListener?

我很難理解如何在QuartzInitializerListener使用Quartz。

首先,我在部署描述符中聲明該偵聽器。 但是,如何添加我的工作呢? 看一下QuartzInitializerListener實現 ,我看到它創建了SchedulerFactoryScheduler ,但我沒有看到任何添加作業的方法。 工廠收到一個配置文件 ,但是那里的工作沒有任何相關性。

我只從我的搜索中找到了很簡單的例子,都是關於在main方法中實例化所有東西。

有人能指出一個更真實的例子嗎? 如果重要的話,我正在使用JBoss 5。 謝謝。

您好以下是您的查詢的答案:

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 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){

        }


    }



}

這個監聽器將在Quartz監聽器執行后執行。 這意味着我們已經准備好了Scheduler Factory和一個啟動調度程序。 所以你只需要向調度程序添加作業。 正如您所看到的,contextDestroyed方法為空,因為調度程序關閉工作將由Quartz API schedluer執行。

您引用的源代碼 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之后。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM