Java 如何将石英与石英石一起使用?

Java 如何将石英与石英石一起使用?,java,quartz-scheduler,Java,Quartz Scheduler,我很难理解如何将Quartz与QuartzInitializerListener一起使用 首先,我在部署描述符中声明该侦听器。但是,我如何添加我的工作?看看,我看到它创建了SchedulerFactory和Scheduler,但我看不到任何添加作业的方法。工厂收到了一份工作通知,但同样与那里的工作无关 我在搜索中只找到了非常简单的示例,都是关于在main方法中实例化所有内容的 有谁能给我举个更真实的例子吗?如果有必要的话,我正在使用JBoss5。谢谢。您引用的Javadoc中描述了所有内容: S

我很难理解如何将Quartz与
QuartzInitializerListener一起使用

首先,我在部署描述符中声明该侦听器。但是,我如何添加我的工作?看看,我看到它创建了
SchedulerFactory
Scheduler
,但我看不到任何添加作业的方法。工厂收到了一份工作通知,但同样与那里的工作无关

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


有谁能给我举个更真实的例子吗?如果有必要的话,我正在使用JBoss5。谢谢。

您引用的Javadoc中描述了所有内容:

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

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

StdSchedulerFactory factory = (StdSchedulerFactory)httpServletRequest
    .getServletContext()
    .getAttribute(QuartzInitializerListener.QUARTZ_FACTORY_KEY);
注意,上下文侦听器保证在使用任何servlet处理传入请求之前执行。这意味着调度程序在使用之前将始终正确初始化


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

您好,以下是您的查询答案:

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>

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

但我应该在何时何地执行此操作?在执行侦听器时,已经调用了scheduler.start()。编辑:确定,在调用start之后可以添加作业。问题仍然存在。因此我应该将该代码放在servlet中,该servlet保证在侦听器启动调度程序后运行?如果您想在应用程序启动时添加作业,请编写另一个侦听器(类似于Quartz提供的侦听器),lookup
StdSchedulerFactory
ServletContext
很容易获得)做你想做的事。监听器的执行顺序保证与
web.xml
中声明的顺序相同,因此请将您的监听器放在Quartz one之后。啊,太好了。我以为他们是独立的。很高兴知道他们尊重这个命令。当然,我认为它们必须是同一类型的。谢谢,应该可以解决了。谢谢@Tomasz,回答得很好。也解决了我的问题。我爱死你了
<?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>
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){

        }


    }



}