使用init()在tomcat启动时扩展HttpServlet调用方法时的NoClassDefFoundError

时间:2018-02-21 14:26:19

标签: tomcat jersey noclassdeffounderror init

我有一个在Tomcat 9上运行的Jersey Restful webservice。 我的要求是每2分钟定期执行一次方法;因为我有一个名为“LoadModels”的类,它扩展了TimerTask,并且当我执行下面的操作时每2分钟运行一次

timer.schedule(new LoadModels(), date.getTime(), 1000 * 2 * 60);

我正在尝试在tomcat启动时触发此操作,以便我的任务每2分钟执行一次。为此我编写了一个扩展HttpServlet和编写init()的类,它在tomcat启动时完美地调用。

但问题是我尽快放置

timer.schedule(new LoadModels(), date.getTime(), 1000 * 2 * 60);

在init()方法中;它是荆棘

java.lang.NoClassDefFoundError:hex / genmodel / GenModel

我理解的是;当tomcat第一次开始时;它试图调用init()方法;但那与当时没有加载的一些罐子有关。

如何解决这个问题。我正在粘贴下面的代码。

  public class InitializeListner extends HttpServlet {
       public void init() {
        System.out.println(" INIT METHOD START");
        Timer timer = new Timer();
        Calendar date = Calendar.getInstance();
        timer.schedule(new LoadModels(), date.getTime(), 1000 * 10* 60);
        System.out.println(" INIT METHOD END");
        }
       }

我的Web.xml

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
         http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <display-name>FlightAvailabilityPredictor</display-name>
    <servlet>
        <servlet-name>jersey-serlvet</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>com.travelport.h2o.availabilitypredictor</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>jersey-serlvet</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>InitializeListner</servlet-name>
        <servlet-class>com.travelport.h2o.availabilitypredictor.listener.InitializeListner</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

</web-app>

以下是例外: -

INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
 INIT METHOD START
Feb 21, 2018 7:18:17 AM org.apache.catalina.core.ApplicationContext log
SEVERE: StandardWrapper.Throwable
java.lang.NoClassDefFoundError: hex/genmodel/GenModel
    at com.travelport.h2o.availabilitypredictor.listener.InitializeListner.init(InitializeListner.java:18)
    at javax.servlet.GenericServlet.init(GenericServlet.java:158)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1174)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1090)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:980)

让我知道如何解决这个问题.. 提前谢谢大家

0 个答案:

没有答案