简体   繁体   English

调用servlet的destroy方法

[英]Calling servlet's destroy method

As per the link http://www.xyzws.com/Servletfaq/when-is-destroy-of-servlets-called/20 , one of the reason of calling destroy method is when the servlet hasn't got a request in a long time . 根据链接http://www.xyzws.com/Servletfaq/when-is-destroy-of-servlets-called/20 ,调用destroy方法的原因之一是当servlet没有得到请求时很长一段时间

I was thinking there could be some pages that don't get called for a long time. 我想可能会有一些页面长时间没有被调用。 So, does that mean destroy will be called and they will be no longer used? 那么,这是否意味着销毁将被调用,它们将不再使用?

Actually, I was asked this question in interview and he told me that destroy method will only be called when server is shut down. 实际上,我在采访中被问到这个问题,他告诉我只有在服务器关闭时才会调用destroy方法。

Appreciate any help on this. 感谢任何帮助。

AFAIK, 据我所知,

In java servlet, destroy() is not supposed to be called by the programmer. 在java servlet中, destroy()不应该由程序员调用。 But, if it is invoked, it gets executed. 但是,如果它被调用,它就会被执行。 The implicit question is, will the servlet get destroyed? 隐含的问题是,servlet会被破坏吗? No, it will not. 不,它不会。 destroy() method is not supposed to and will not destroy a java servlet. destroy()方法不应该也不会破坏java servlet。

The meaning of destroy() in java servlet is, the content gets executed just before when the container decides to destroy the servlet. java servlet中destroy()的含义是,当容器决定销毁servlet之前,内容就会被执行。 But if you invoke the destroy() method yourself, the content just gets executed and then the respective process continues. 但是如果你自己调用destroy()方法,那么内容就会被执行,然后相应的进程继续。 With respective to this question, the destroy() gets executed and then the servlet initialization gets completed. 对于这个问题, destroy()被执行,然后servlet初始化完成。

destroy() method is invoked first, then Servlet is removed from the container and then eventually garbage collected. 首先调用destroy()方法,然后从容器中删除Servlet,最后进行垃圾回收。 destroy() method generally contains code to free any resources like JDBC connection that will not be garbage collected. destroy()方法通常包含释放任何资源(如JDBC连接destroy()代码,这些资源不会被垃圾回收。

Couple of cases : 几个案例:

  1. when the container shuts down or the application shuts down; 当容器关闭或应用程序关闭时;
  2. when the container decides that there is a shortage of memory; 当容器决定内存不足时;
  3. when this servlet hasn't got a request in a long time. 当这个servlet长时间没有请求时。

As per the Doc 根据文件

Called by the servlet container to indicate to a servlet that the servlet is being taken out of service. 由servlet容器调用,以向servlet指示servlet正在停止服务。 This method is only called once all threads within the servlet's service method have exited or after a timeout period has passed. 只有在servlet的服务方法中的所有线程都已退出或超时期限过后,才会调用此方法。 After the servlet container calls this method, it will not call the service method again on this servlet. 在servlet容器调用此方法之后,它不会再在此servlet上调用服务方法。

From the specification ( The Servlet Interface -> Servlet Life Cycle -> End of Service ): 从规范( The Servlet Interface -> Servlet Life Cycle -> End of Service ):

Once the destroy method is called on a servlet instance, the container may not route other requests to that instance of the servlet. 在servlet实例上调用destroy方法后,容器可能不会将其他请求路由到该servlet实例。 If the container needs to enable the servlet again, it must do so with a new instance of the servlet's class. 如果容器需要再次启用servlet,则必须使用servlet类的新实例。

The specification is referring to a particular instance of the servlet that is being destroyed. 规范指的是正在销毁的servlet的特定实例。

So, does that mean destroy will be called and they will be no longer used? 那么,这是否意味着销毁将被调用,它们将不再使用?

It doesn't mean that the servlet whose instance is destroyed is never used. 这并不意味着永远不会使用其实例被销毁的servlet。 It's only that particular instance that is not used as it would have been garbage collected. 它只是那个没有使用的特定实例,因为它本来是垃圾收集的。 When a request comes in for that servlet, the container would create a new instance of that servlet and performs all the life-cycle actions till serving the request, again. 当请求进入该servlet时,容器将创建该servlet的新实例并执行所有生命周期操作,直到再次提供请求为止。

This is one of those classic spec vs implementation things. 这是经典规范与实现之一。 Servlet containers like Tomcat are allowed by the specification to maintain a pool of Servlet objects, creating and destroying them as load goes up or down. 规范允许像Tomcat这样的Servlet容器来维护Servlet对象池,在负载上升或下降时创建和销毁它们。 In this kind of setup, destroy would be called when the particular instance was no longer needed. 在这种设置中,当不再需要特定实例时,将调用destroy。 It would even be legal to create a new instance for each request, calling init immediately before the request is processed and destroy straight after. 为每个请求创建一个新实例甚至是合法的,在处理请求之前立即调用init并在之后立即销毁。

However in practice virtually every Servlet container maintains exactly one instance of each Servlet. 但实际上,实际上每个Servlet容器都只维护每个Servlet的一个实例。 Since Servlets are required to be thread safe this is also perfectly ok. 由于要求Servlet是线程安全的,所以这也是完全可以的。 In this scenario destroy is only called when the context is shutdown. 在这种情况下,仅在关闭上下文时调用destroy。

I can't think of many cases where you would want to pool the Servlets, but it is allowed by the spec. 我想不出很多你想要汇集Servlet的情况,但规范允许这样做。

It totally depends on the implementation of the servlet container you are using. 它完全取决于您正在使用的servlet容器的实现。 If your servlet container is very conservative for saving resources, it may destroy the servlet that is not in service for long time. 如果您的servlet容器非常保守以节省资源,它可能会破坏长时间未使用的servlet。

In page 22 of servlet spec 2.5, you will find that, "When the servlet container determines that a servlet should be removed from service, it calls the destroy method of the Servlet interface to allow the servlet to release any resources it is using and save any persistent state. For example, the container may do this when it wants to conserve memory resources, or when it is being shut down." 在servlet规范2.5的第22页中,您会发现,“当servlet容器确定应该从服务中删除servlet时,它会调用Servlet接口的destroy方法,以允许servlet释放它正在使用的任何资源并保存任何持久状态。例如,容器可以在想要节省内存资源或关闭时执行此操作。“

Servlet.destroy() 's javadoc says: "Called by the servlet container to indicate to a servlet that the servlet is being taken out of service". Servlet.destroy()的javadoc说:“由servlet容器调用,以向servlet指示servlet正在停止服务”。 It doesn't specify what situations would cause a servlet to be "taken out of service", it is simply an event to which you can react to if you need to. 它没有指定哪些情况会导致servlet“停止服务”,它只是一个事件,如果需要,您可以对此做出反应。 For example you might want to close some resources you have open, or persist some state to storage. 例如,您可能希望关闭已打开的某些资源,或将某些状态保留到存储。

The most obvious reason for this is that the servlet container is being shut down, but a container could equally well decide that it's time to unload a servlet because it's not been used in a month and is simply consuming resources. 最明显的原因是servlet容器正在关闭,但容器同样可以很好地决定是否需要卸载servlet,因为它在一个月内没有被使用,只是消耗资源。 That behaviour is entirely up to the implementation, and it is not something you (as a servlet designer) should be concerned about. 这种行为完全取决于实现,并不是你(作为servlet设计者)应该关注的事情。

Servlet's destroy method is a callback. Servlet的destroy方法是一个回调。 These are the functions called by the container when a particular event is about to happen (in this case servlet about to be destroyed). 这些是当特定事件即将发生时容器调用的函数(在这种情况下,servlet即将被销毁)。 Thus the whole purpose is if you want to do something (eg: cleaning up the resource) you can do those under this callback. 因此,如果您想要做某事(例如:清理资源),您可以在此回调下执行此操作。

From Servlet Specification:- 从Servlet规范: -

destroy() method is called by the servlet container to indicate to a servlet that the servlet is being taken out of service. servlet容器调用destroy()方法向servlet指示servlet正在停止服务。 This method is only called once all threads within the servlet's service method have exited or after a timeout period has passed. 只有在servlet的服务方法中的所有线程都已退出或超时期限过后,才会调用此方法。 After the servlet container calls destroy() method, it will not call the service method again on "THIS SERVLET". 在servlet容器调用destroy()方法之后,它不会再在“THIS SERVLET”上调用服务方法。

The servlet container is not required to keep a servlet loaded for any particular period of time. servlet容器不需要在任何特定时间段内加载servlet。 A servlet instance may be kept active in a servlet container for a period of milliseconds, for the lifetime of the servlet container (which could be a number of days, months, or years), or any amount of time in between. 对于servlet容器的生命周期(可能是几天,几个月或几年),或者介于两者之间的任何时间量,servlet实例可以在servlet容器中保持活动状态一段时间。

When the servlet container determines that a servlet should be removed from service, it calls the destroy method of the Servlet interface to allow the servlet to release any resources it is using and save any persistent state. 当servlet容器确定应该从服务中删除servlet时,它调用Servlet接口的destroy方法以允许servlet释放它正在使用的任何资源并保存任何持久状态。 For example, the container may do this when it wants to conserve memory resources, or when it is being shut down. 例如,容器可以在想要节省内存资源时或在关闭时执行此操作。

Before the servlet container calls the destroy method, it must allow any threads that are currently running in the service method of the servlet to complete execution, or exceed a server-defined time limit. 在servlet容器调用destroy方法之前,它必须允许当前在servlet的service方法中运行的任何线程完成执行,或超过服务器定义的时间限制。

Once the destroy method is called on a servlet instance, the container may not route other requests to that instance of the servlet. 在servlet实例上调用destroy方法后,容器可能不会将其他请求路由到该servlet实例。

If the container needs to enable the servlet again, it must do so with a new instance of the servlet's class. 如果容器需要再次启用servlet,则必须使用servlet类的新实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM