如何在QuartzInitializerListener中使用Quartz?

| 我很难理解如何将Quartz与ѭ0一起使用。 首先,我在部署描述符中声明该侦听器。但是,然后如何添加我的工作呢?看一下QuartzInitializerListener的实现,我看到它创建了
SchedulerFactory
和ѭ2but,但是我看不到添加作业的任何方法。工厂接收到一个配置文件,但同样也没有与那里的作业相关的信息。 我从搜索中仅发现了非常简单的示例,有关实例化main方法中的所有内容。 谁能指出我一个更真实的例子?如果这很重要,我正在使用JBoss 5。谢谢。     
已邀请:
您引用的源代码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之后。     
您好,这是您查询的答案: 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,并启动了Scheduler。因此,您只需要将作业添加到调度程序。如您所见,contextDestroyed方法为空,因为调度程序关闭工作将由Quartz API schedluer进行。     

要回复问题请先登录注册