Tomcat-Servlet init()在启动时被调用了两次 [英] Tomcat - Servlet init() called twice upon startup

查看:484
本文介绍了Tomcat-Servlet init()在启动时被调用了两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在独立的Tomcat服务器(未与Apache链接)上遇到问题.

I have an issue with a standalone Tomcat server (not linked with Apache).

Tomcat启动时,该servlet的init()方法被调用两次,即两个servlet正在启动.更令人担忧的是,这些似乎是由不同的类加载器加载的-命令行上仅运行一个Java进程,因此它不是多个Tomcat.

When Tomcat starts up, the init() method of the servlet is getting called twice, i.e., two servlets are starting up. Even more worrying is that these appear to be loaded by different classloaders - there is only one Java process running on the command line so it's not multiple Tomcats.

web.xml代码段(该servlet仅配置一次,并且仅在webapp web.xml中配置):

web.xml snippet (the servlet is only configured once, and only configured in the webapp web.xml):

<servlet>
  <servlet-name>LenderInterfaceServlet</servlet-name>
  <display-name>Lender Interface Servlet</display-name>
  <servlet-class>com.foobar.lender.webservice.server.LenderInterfaceServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
</servlet>

我在init方法中添加了一些日志记录,并创建了一个名为RatesPoller的单例类,该类试图通过init方法获取实例.从日志中我们可以看到单例实例是不同的:

I put some logging in the init method, and created a singleton class called RatesPoller that the init tries to get an instance of. From the logs we can see that the singleton instances are different:

LenderInterfaceServlet - [init] Start: com.foobar.lender.webservice.server.LenderInterfaceServlet@56d90453
LenderInterfaceServlet - [init] Starting up the Rates pollers.
LenderInterfaceServlet - [init] Got com.foobar.lender.framework.rates.RatesPoller@ae0e515
LenderInterfaceServlet - [init] Start: com.foobar.lender.webservice.server.LenderInterfaceServlet@1b0c6cfc
LenderInterfaceServlet - [init] Starting up the Rates pollers.
LenderInterfaceServlet - [init] Got com.foobar.lender.framework.rates.RatesPoller@5759780d

因此,我们有两个不同的servlet和两个不同的单例轮询器.

So we have two distince servlets, and two distinct singleton pollers.

我怀疑这意味着Tomcat正在启动存储在Tomcat webapps文件夹中的webapp的两个实例.显然,当您要启动只能运行的程序时,这是一个问题,我需要弄清楚如何确保Tomcat不会两次运行webapp!

I suspect this means that Tomcat is launching two instances of the webapp that is stored in the Tomcat webapps folder. Obviously when you want to launch something that only one will be running, this is a problem, and I need to work out how to ensure that Tomcat doesn't run the webapp twice!

如果有人有任何想法...

If anyone has any ideas ...

代码:

public class RatesPoller {

  private static RatesPoller instance;

  private RatesPoller() {}

  public static RatesPoller getInstance() {
    if (instance == null) {
        instance = new RatesPoller();
    }
    return instance;
  }

  public RatesPoller clone() throws CloneNotSupportedException {
    throw new CloneNotSupportedException("Singleton. Tsk!");
  }
}

和LenderInterfaceServlet中的init()方法:

and the init() method in LenderInterfaceServlet:

public class LenderInterfaceServlet extends AxisServlet {
  // ...
  @Override
  public void init() throws ServletException {
    logger.info("[init] Start: " + this);
    super.init();
    logger.info("[init] Starting up the Rates pollers.");
    RatesPoller rp = RatesPoller.getInstance();
    logger.info("[init] Got " + rp);
  }
  // ...
}

推荐答案

Tomcat正在自动部署Web应用程序中的所有内容,并根据server.xml进行部署.只需将您的Web应用程序从Web应用程序中删除,即可避免重复加载.

Tomcat is autodeploying anything in webapps, and also deploying according to server.xml. Just remove your webapp from webapps to avoid double loading.

这篇关于Tomcat-Servlet init()在启动时被调用了两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆