gpt4 book ai didi

java - 如何将 Quartz 与 QuartzInitializerListener 一起使用?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:16:32 25 4
gpt4 key购买 nike

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

首先,我在部署描述符中声明了该监听器。但是,如何添加我的工作?看看QuartzInitializerListener implementation ,我看到它创建了 SchedulerFactoryScheduler,但我看不到任何添加作业的方法。工厂收到configuration file ,但同样与那里的工作无关。

我只从我的搜索中找到了非常简单的示例,都是关于在 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 监听器:

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 执行。

关于java - 如何将 Quartz 与 QuartzInitializerListener 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5663640/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com