迁移Java 8至17。Java.lang.noclassdeffounderror:Javax/servlet/servletContextListListener使用Quartz

发布于 2025-01-30 13:05:54 字数 6632 浏览 3 评论 0原文

我正在将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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

养猫人 2025-02-06 13:05:54

我在下面提到的是,总的来说,永远不应该做;但是,由于您正在解决问题,因此您可以尝试这个想法。

通常,这个想法很简单,“包装器”:

  1. 创建您自己的上下文侦听器,注册Quartz listerer(如上所述)
  2. 编写 servletContext 包装器,将所有方法调用到包裹的'jakarta'servlet contect
  3. servletContContextExtevent 是一个类,使用一种方法,使用Javax类本身,
  4. 从您的听众中拨打相应的上下文初始化/销毁的上下文初始销毁/销毁的
  5. 人javax.servlet jar in classpath in the class path

然后启动玻璃鱼...谁知道接下来会发生什么...它可能只是工作或失败了...: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':

  1. Create your own context listener, register in place of Quartz listener (as suggested above)
  2. Write a ServletContext wrapper which dispatches all method calls to the wrapped 'jakarta' servlet context
  3. ServletContextEvent is a class, with one method, use the javax class itself
  4. From your listener call the corresponding context initialized/destroyed
  5. Have the javax.servlet jar in the classpath

Then start the Glassfish ... who knows what happens next ... it might just work or fail spectacularly ... :D

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文