Prevent Tomcat from caching request during starup

2019-07-08 10:03发布

I have an issue with cached requests in Tomcat. Whenever I (re-)start my application, Tomcat begins caching incoming request before the application is fully initialized.

Is there a way to stop Tomcat behaving like this? I found the "cachingAllowed" option in the -Element, but I am not sure about this.

Can you please advise on, how to prevent Tomcat from caching before everything is initialized. The point I would expect requests to be cached is when the server startup is complete.

Many Thanks,

Marc

1条回答
甜甜的少女心
2楼-- · 2019-07-08 10:32

The effect you are describing is called request queuing. By default Tomcat starts and listens to its configured port. When the first request comes in, it triggers that the corresponding web application is deployed and started. All requests that come in before the application is started are blocked and processing starts when the application startup is done.

The above description is a little simplified, because depending on your Tomcat connector type and configuration there are different areas where requests can blocked and queued up:

  • A processing thread waits for the application to start. The limit is controlled by maxThreads.
  • A request waits for a processing thread. The limit is controlled by maxConnections.
  • An incoming TCP connection is accepted by the operating system but not yet handled by Tomcat. This is controlled by acceptCount. (BTW: I set this to 0)

As said, how the parameters work in detail may vary by connector type.

This is how you can control the limits and for the understanding what is going on. I think your effect will get better with setting acceptCount to 0.

Actually, the same problem and question was asked in: how to make http port to open after application startup in tomcat No real answer there, also. It seams this is not how Tomcat is designed to work. Solutions I can think of, but not yet tried myself:

  • Find a way to start Tomcat with a disabled connector (I found no parameter for that...) and enable it after startup by JMX
  • Switch to an embedded Tomcat or Jetty, that gets started from the application
查看更多
登录 后发表回答