迁移Java 8至17。Java.lang.noclassdeffounderror:Javax/servlet/servletContextListListener使用Quartz
我正在将Java 8迁移到Java 17,Javax依赖性更改为Jakarta。我还更新了依赖Javax的其他依赖关系。
问题,我们已经使用了石英调度程序来调度作业。 Quartzinitializer -listener用于获取调度程序对象。 web.xml中的QuartzinitializerListener也添加为侦听器。 QuartzinitializerListener使用Javax库
Eclipse Glassfish 6.2.3
OpenJDK 17
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.2</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.3.2</version>
</dependency>
调度程序代码
private Scheduler getScheduler() throws SchedulerException {
StdSchedulerFactory factory = (StdSchedulerFactory) getServletContext().getAttribute(QuartzInitializerListener.QUARTZ_FACTORY_KEY);
return factory.getScheduler();
}
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package org.quartz.ee.servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class QuartzInitializerListener implements ServletContextListener {
public static final String QUARTZ_FACTORY_KEY = "org.quartz.impl.StdSchedulerFactory.KEY";
private boolean performShutdown = true;
private boolean waitOnShutdown = false;
private Scheduler scheduler = null;
private final Logger log = LoggerFactory.getLogger(this.getClass());
public QuartzInitializerListener() {
}
public void contextInitialized(ServletContextEvent sce) {
this.log.info("Quartz Initializer Servlet loaded, initializing Scheduler...");
ServletContext servletContext = sce.getServletContext();
try {
String configFile = servletContext.getInitParameter("quartz:config-file");
if (configFile == null) {
configFile = servletContext.getInitParameter("config-file");
}
String shutdownPref = servletContext.getInitParameter("quartz:shutdown-on-unload");
if (shutdownPref == null) {
shutdownPref = servletContext.getInitParameter("shutdown-on-unload");
}
if (shutdownPref != null) {
this.performShutdown = Boolean.valueOf(shutdownPref);
}
String shutdownWaitPref = servletContext.getInitParameter("quartz:wait-on-shutdown");
if (shutdownWaitPref != null) {
this.waitOnShutdown = Boolean.valueOf(shutdownWaitPref);
}
StdSchedulerFactory factory = this.getSchedulerFactory(configFile);
this.scheduler = factory.getScheduler();
String startOnLoad = servletContext.getInitParameter("quartz:start-on-load");
if (startOnLoad == null) {
startOnLoad = servletContext.getInitParameter("start-scheduler-on-load");
}
int startDelay = 0;
String startDelayS = servletContext.getInitParameter("quartz:start-delay-seconds");
if (startDelayS == null) {
startDelayS = servletContext.getInitParameter("start-delay-seconds");
}
try {
if (startDelayS != null && startDelayS.trim().length() > 0) {
startDelay = Integer.parseInt(startDelayS);
}
} catch (Exception var12) {
this.log.error("Cannot parse value of 'start-delay-seconds' to an integer: " + startDelayS + ", defaulting to 5 seconds.");
startDelay = 5;
}
if (startOnLoad != null && !Boolean.valueOf(startOnLoad)) {
this.log.info("Scheduler has not been started. Use scheduler.start()");
} else if (startDelay <= 0) {
this.scheduler.start();
this.log.info("Scheduler has been started...");
} else {
this.scheduler.startDelayed(startDelay);
this.log.info("Scheduler will start in " + startDelay + " seconds.");
}
String factoryKey = servletContext.getInitParameter("quartz:servlet-context-factory-key");
if (factoryKey == null) {
factoryKey = servletContext.getInitParameter("servlet-context-factory-key");
}
if (factoryKey == null) {
factoryKey = "org.quartz.impl.StdSchedulerFactory.KEY";
}
this.log.info("Storing the Quartz Scheduler Factory in the servlet context at key: " + factoryKey);
servletContext.setAttribute(factoryKey, factory);
String servletCtxtKey = servletContext.getInitParameter("quartz:scheduler-context-servlet-context-key");
if (servletCtxtKey == null) {
servletCtxtKey = servletContext.getInitParameter("scheduler-context-servlet-context-key");
}
if (servletCtxtKey != null) {
this.log.info("Storing the ServletContext in the scheduler context at key: " + servletCtxtKey);
this.scheduler.getContext().put(servletCtxtKey, servletContext);
}
} catch (Exception var13) {
this.log.error("Quartz Scheduler failed to initialize: " + var13.toString());
var13.printStackTrace();
}
}
protected StdSchedulerFactory getSchedulerFactory(String configFile) throws SchedulerException {
StdSchedulerFactory factory;
if (configFile != null) {
factory = new StdSchedulerFactory(configFile);
} else {
factory = new StdSchedulerFactory();
}
return factory;
}
public void contextDestroyed(ServletContextEvent sce) {
if (this.performShutdown) {
try {
if (this.scheduler != null) {
this.scheduler.shutdown(this.waitOnShutdown);
}
} catch (Exception var3) {
this.log.error("Quartz Scheduler failed to shutdown cleanly: " + var3.toString());
var3.printStackTrace();
}
this.log.info("Quartz Scheduler successful shutdown.");
}
}
}
I am migrating an application from Java 8 to Java 17, and javax dependencies changed to Jakarta ones. I also updated other dependencies which were dependent on javax.
The problem, we have used Quartz scheduler for scheduling jobs. QuartzInitializerListener is used to get Scheduler object. Also QuartzInitializerListener is added in web.xml as listener. QuartzInitializerListener uses javax library
Eclipse GlassFish 6.2.3
OpenJDK 17
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.2</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.3.2</version>
</dependency>
Scheduler Code
private Scheduler getScheduler() throws SchedulerException {
StdSchedulerFactory factory = (StdSchedulerFactory) getServletContext().getAttribute(QuartzInitializerListener.QUARTZ_FACTORY_KEY);
return factory.getScheduler();
}
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package org.quartz.ee.servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class QuartzInitializerListener implements ServletContextListener {
public static final String QUARTZ_FACTORY_KEY = "org.quartz.impl.StdSchedulerFactory.KEY";
private boolean performShutdown = true;
private boolean waitOnShutdown = false;
private Scheduler scheduler = null;
private final Logger log = LoggerFactory.getLogger(this.getClass());
public QuartzInitializerListener() {
}
public void contextInitialized(ServletContextEvent sce) {
this.log.info("Quartz Initializer Servlet loaded, initializing Scheduler...");
ServletContext servletContext = sce.getServletContext();
try {
String configFile = servletContext.getInitParameter("quartz:config-file");
if (configFile == null) {
configFile = servletContext.getInitParameter("config-file");
}
String shutdownPref = servletContext.getInitParameter("quartz:shutdown-on-unload");
if (shutdownPref == null) {
shutdownPref = servletContext.getInitParameter("shutdown-on-unload");
}
if (shutdownPref != null) {
this.performShutdown = Boolean.valueOf(shutdownPref);
}
String shutdownWaitPref = servletContext.getInitParameter("quartz:wait-on-shutdown");
if (shutdownWaitPref != null) {
this.waitOnShutdown = Boolean.valueOf(shutdownWaitPref);
}
StdSchedulerFactory factory = this.getSchedulerFactory(configFile);
this.scheduler = factory.getScheduler();
String startOnLoad = servletContext.getInitParameter("quartz:start-on-load");
if (startOnLoad == null) {
startOnLoad = servletContext.getInitParameter("start-scheduler-on-load");
}
int startDelay = 0;
String startDelayS = servletContext.getInitParameter("quartz:start-delay-seconds");
if (startDelayS == null) {
startDelayS = servletContext.getInitParameter("start-delay-seconds");
}
try {
if (startDelayS != null && startDelayS.trim().length() > 0) {
startDelay = Integer.parseInt(startDelayS);
}
} catch (Exception var12) {
this.log.error("Cannot parse value of 'start-delay-seconds' to an integer: " + startDelayS + ", defaulting to 5 seconds.");
startDelay = 5;
}
if (startOnLoad != null && !Boolean.valueOf(startOnLoad)) {
this.log.info("Scheduler has not been started. Use scheduler.start()");
} else if (startDelay <= 0) {
this.scheduler.start();
this.log.info("Scheduler has been started...");
} else {
this.scheduler.startDelayed(startDelay);
this.log.info("Scheduler will start in " + startDelay + " seconds.");
}
String factoryKey = servletContext.getInitParameter("quartz:servlet-context-factory-key");
if (factoryKey == null) {
factoryKey = servletContext.getInitParameter("servlet-context-factory-key");
}
if (factoryKey == null) {
factoryKey = "org.quartz.impl.StdSchedulerFactory.KEY";
}
this.log.info("Storing the Quartz Scheduler Factory in the servlet context at key: " + factoryKey);
servletContext.setAttribute(factoryKey, factory);
String servletCtxtKey = servletContext.getInitParameter("quartz:scheduler-context-servlet-context-key");
if (servletCtxtKey == null) {
servletCtxtKey = servletContext.getInitParameter("scheduler-context-servlet-context-key");
}
if (servletCtxtKey != null) {
this.log.info("Storing the ServletContext in the scheduler context at key: " + servletCtxtKey);
this.scheduler.getContext().put(servletCtxtKey, servletContext);
}
} catch (Exception var13) {
this.log.error("Quartz Scheduler failed to initialize: " + var13.toString());
var13.printStackTrace();
}
}
protected StdSchedulerFactory getSchedulerFactory(String configFile) throws SchedulerException {
StdSchedulerFactory factory;
if (configFile != null) {
factory = new StdSchedulerFactory(configFile);
} else {
factory = new StdSchedulerFactory();
}
return factory;
}
public void contextDestroyed(ServletContextEvent sce) {
if (this.performShutdown) {
try {
if (this.scheduler != null) {
this.scheduler.shutdown(this.waitOnShutdown);
}
} catch (Exception var3) {
this.log.error("Quartz Scheduler failed to shutdown cleanly: " + var3.toString());
var3.printStackTrace();
}
this.log.info("Quartz Scheduler successful shutdown.");
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在下面提到的是,总的来说,永远不应该做;但是,由于您正在解决问题,因此您可以尝试这个想法。
通常,这个想法很简单,“包装器”:
然后启动玻璃鱼...谁知道接下来会发生什么...它可能只是工作或失败了...:D
What I mention below, in general should never be done; however, since you are in a fix, you could experiment with the idea.
The idea in general is simple, 'wrapper':
Then start the Glassfish ... who knows what happens next ... it might just work or fail spectacularly ... :D