You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Chip McVey <Ch...@quest.com> on 2012/07/26 21:19:06 UTC

Permanent servlet in TC7

Hi,



In TC7, is there a way to tell Tomcat to never unload a given servlet unless Tomcat itself is being shutdown?  I want a single servlet instance that I can know will exist for the life of the tomcat process without being unloaded & reloaded (unless someone manually instructs Tomcat to do so, of course).



Yes, I know I'm "not supposed to do this" and that the servlet spec says a container is allowed to unload a server any time as long as a request is not in process.  I still would like to know if there's some Tomcat specific way to tell TC7 to never unload the servlet.  And yes, I realize even if there is such a way that future versions of Tomcat may remove this capability.  :)



Thanks in advance,

Chip



Re: Permanent servlet in TC7

Posted by Pid <pi...@pidster.com>.
On 26/07/2012 22:38, Christopher Schultz wrote:
> Chip,
> 
> On 7/26/12 3:19 PM, Chip McVey wrote:
>> In TC7, is there a way to tell Tomcat to never unload a given
>> servlet unless Tomcat itself is being shutdown?  I want a single
>> servlet instance that I can know will exist for the life of the
>> tomcat process without being unloaded & reloaded (unless someone
>> manually instructs Tomcat to do so, of course).
> 
>> Yes, I know I'm "not supposed to do this" and that the servlet
>> spec says a container is allowed to unload a server any time as
>> long as a request is not in process.  I still would like to know if
>> there's some Tomcat specific way to tell TC7 to never unload the
>> servlet. And yes, I realize even if there is such a way that future
>> versions of Tomcat may remove this capability.  :)
> 
> You have said both "servlet" and "server": which did you mean?
> 
> Do you need a particular web application to stay loaded? That's easy:
> simply don't undeploy it.
> 
> Do you need a single (or multiple) servlet(s) to stay loaded all the
> time? That's also easy: I don't believe Tomcat ever actually unloads a
> servlet. (You can easily test this by implementing the destroy()
> method and emitting some kind of log).
> 
> What is it about the servlet that it needs to stay loaded? Servlets
> aren't really supposed to have any state, so unloading and re-loading
> the servlet shouldn't represent anything traumatic to your web
> application.
> 
> If you have to have some kind of data loaded all the time, consider
> moving that data into the ServletContext (aka "application")
> attributes: then the servlet can be unloaded and re-loaded at will and
> the data will persist.

+1

-> ServletContextListener


p

> -chris
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 

-- 

[key:62590808]


RE: Permanent servlet in TC7

Posted by Chip McVey <Ch...@quest.com>.
Thanks to all for the input - I am ending up doing some refactoring to follow Ben's advice wrt a POJO that does the heavy lifting stored in context, making the destroy() essentially a no-op rather than doing full shutdown, and using the ContextListener to really do final shutdown using the POJO when the context itself is destroyed.

-----Original Message-----
From: Ben Souther [mailto:ben@souther.us] 
Sent: Friday, July 27, 2012 1:56 PM
To: users@tomcat.apache.org
Subject: RE: Permanent servlet in TC7

On Thu, 2012-07-26 at 19:54 +0000, Chip McVey wrote:
> So I'm looking for a guarantee that no automatic destroys will happen 
> going forward just because Tomcat decides to do it on its own.  If it 
> is instructed to shutdown/undeploy/unload by some outside system or by 
> a human user, that's fine, I'm not worried about that.  But I don't 
> want these servlets to be destroyed just because Tomcat decides to do 
> so for whatever reason.

If I were looking for permanence, I would create POJO, bind it to context scope, and then call it from my servlets, passing it whatever it needs to function.




RE: Permanent servlet in TC7

Posted by Ben Souther <be...@souther.us>.
On Thu, 2012-07-26 at 19:54 +0000, Chip McVey wrote:
> So I'm looking for a guarantee that no automatic destroys will happen
> going forward just because Tomcat decides to do it on its own.  If it
> is instructed to shutdown/undeploy/unload by some outside system or by
> a human user, that's fine, I'm not worried about that.  But I don't
> want these servlets to be destroyed just because Tomcat decides to do
> so for whatever reason.

If I were looking for permanence, I would create POJO, bind it to
context scope, and then call it from my servlets, passing it whatever it
needs to function.




---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Permanent servlet in TC7

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Chip,

On 7/26/12 3:54 PM, Chip McVey wrote:
> I have read that Tomcat does not unload servlets (even though it 
> would be permissible to do so), but as you say, I have done some 
> logging in the destroy method, and I see this behavior.

Can you give us a stack trace of that event?

> A certain servlet within app B & C (let's call them B1 and C1) upon
>  init make a call to a servlet in app A (let's call it A1). What I 
> see is A1, B1, and C1 all initialize. The init is done by a thread 
> called " pool-2-thread-1", which seems like a system thread.  Ok no
>  problem.
> 
> Then, for some reason, A1's destroy method is called, and it is 
> called by a thread named " http-apr-8080-exec-3", making me think
> it is being called by a thread processing the request for either B1
> or C1, since no other requests from anyone outside of B or C have 
> happened yet.

http-apr-8080-exec-3 should be a request-processing thread, which has no
business calling destroy() on a servlet.

> After destroy winds down, then, A1's init method is called by a 
> thread named " http-apr-8080-exec-11" - another request processing
>  thread I assume.

Yes. This is probably happening because the servlet needs to be brought
(back) into service.

> Everything all works out in the end, and after that I don't see
> A1, B1, or C1 ever destroyed - but am I guaranteed that they won't
> ever be destroyed again unless TC is shutdown or they are manually 
> unloaded?

There are no guarantees, but Tomcat shouldn't randomly unloading your
servlets.

Do you only see one of these (surprising) unloads, or do they happen a
lot over time?

> I understand that given servlets can by spec go up & down, maybe 
> using them was not the right choice for an application that needs 
> permanency, but we're too late in development to make that switch 
> now.

Is your webapp somehow being damaged by these unload operations? A
servlet should be fairly lightweight, and the init method "should" run
very quickly.

> So I'm looking for a guarantee that no automatic destroys will 
> happen going forward just because Tomcat decides to do it on its 
> own.

You will never get that guarantee, because the servlet spec does not
require it.

> If it is instructed to shutdown/undeploy/unload by some outside 
> system or by a human user, that's fine, I'm not worried about
> that. But I don't want these servlets to be destroyed just because
> Tomcat decides to do so for whatever reason.

Sorry, you can't control that. What you can do is architect your
application in such a way that it doesn't matter.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAlASqKAACgkQ9CaO5/Lv0PCcAQCfTz5FSmH+dangfTdWWu0urO2T
a+wAoMRX9W77BTrJMphN6OJUKTBsVVsL
=Si2b
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Permanent servlet in TC7

Posted by Chip McVey <Ch...@quest.com>.
Hi Chris,

Thanks for the response.  Sorry, where I said "...unload a server any time" I should have said 'servlet'.

I have read that Tomcat does not unload servlets (even though it would be permissible to do so), but as you say, I have done some logging in the destroy method, and I see this behavior.

Here's the scenario:  I have 3 webapps, let's call them A, B, C.  They are all hosted in the same TC7 instance currently, but they could also all be hosted on different machines in different TC7 containers.

A certain servlet within app B & C (let's call them B1 and C1) upon init make a call to a servlet in app A (let's call it A1). What I see is A1, B1, and C1 all initialize. The init is done by a thread called " pool-2-thread-1", which seems like a system thread.  Ok no problem.

Then, for some reason, A1's destroy method is called, and it is called by a thread named " http-apr-8080-exec-3", making me think it is being called by a thread processing the request for either B1 or C1, since no other requests from anyone outside of B or C have happened yet.

After destroy winds down, then, A1's init method is called by a thread named " http-apr-8080-exec-11" - another request processing thread I assume.   Everything all works out in the end, and after that I don't see A1, B1, or C1 ever destroyed - but am I guaranteed that they won't ever be destroyed again unless TC is shutdown or they are manually unloaded?

I understand that given servlets can by spec go up & down, maybe using them was not the right choice for an application that needs permanency, but we're too late in development to make that switch now.

So I'm looking for a guarantee that no automatic destroys will happen going forward just because Tomcat decides to do it on its own.  If it is instructed to shutdown/undeploy/unload by some outside system or by a human user, that's fine, I'm not worried about that.  But I don't want these servlets to be destroyed just because Tomcat decides to do so for whatever reason.

Hope that helps clarify, and thanks for the feedback.

-----Original Message-----
From: Christopher Schultz [mailto:chris@christopherschultz.net] 
Sent: Thursday, July 26, 2012 12:38 PM
To: Tomcat Users List
Subject: Re: Permanent servlet in TC7

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Chip,

On 7/26/12 3:19 PM, Chip McVey wrote:
> In TC7, is there a way to tell Tomcat to never unload a given servlet 
> unless Tomcat itself is being shutdown?  I want a single servlet 
> instance that I can know will exist for the life of the tomcat process 
> without being unloaded & reloaded (unless someone manually instructs 
> Tomcat to do so, of course).
> 
> Yes, I know I'm "not supposed to do this" and that the servlet spec 
> says a container is allowed to unload a server any time as long as a 
> request is not in process.  I still would like to know if there's some 
> Tomcat specific way to tell TC7 to never unload the servlet. And yes, 
> I realize even if there is such a way that future versions of Tomcat 
> may remove this capability.  :)

You have said both "servlet" and "server": which did you mean?

Do you need a particular web application to stay loaded? That's easy:
simply don't undeploy it.

Do you need a single (or multiple) servlet(s) to stay loaded all the time? That's also easy: I don't believe Tomcat ever actually unloads a servlet. (You can easily test this by implementing the destroy() method and emitting some kind of log).

What is it about the servlet that it needs to stay loaded? Servlets aren't really supposed to have any state, so unloading and re-loading the servlet shouldn't represent anything traumatic to your web application.

If you have to have some kind of data loaded all the time, consider moving that data into the ServletContext (aka "application")
attributes: then the servlet can be unloaded and re-loaded at will and the data will persist.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAlARnJ8ACgkQ9CaO5/Lv0PCaGQCgv7Kgkl04ElbBu5bkqNtuuX+v
5OsAn1MSf6i3CH9vIogXTb+aUKRYq0WN
=6vQK
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Permanent servlet in TC7

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Chip,

On 7/26/12 3:19 PM, Chip McVey wrote:
> In TC7, is there a way to tell Tomcat to never unload a given
> servlet unless Tomcat itself is being shutdown?  I want a single
> servlet instance that I can know will exist for the life of the
> tomcat process without being unloaded & reloaded (unless someone
> manually instructs Tomcat to do so, of course).
> 
> Yes, I know I'm "not supposed to do this" and that the servlet
> spec says a container is allowed to unload a server any time as
> long as a request is not in process.  I still would like to know if
> there's some Tomcat specific way to tell TC7 to never unload the
> servlet. And yes, I realize even if there is such a way that future
> versions of Tomcat may remove this capability.  :)

You have said both "servlet" and "server": which did you mean?

Do you need a particular web application to stay loaded? That's easy:
simply don't undeploy it.

Do you need a single (or multiple) servlet(s) to stay loaded all the
time? That's also easy: I don't believe Tomcat ever actually unloads a
servlet. (You can easily test this by implementing the destroy()
method and emitting some kind of log).

What is it about the servlet that it needs to stay loaded? Servlets
aren't really supposed to have any state, so unloading and re-loading
the servlet shouldn't represent anything traumatic to your web
application.

If you have to have some kind of data loaded all the time, consider
moving that data into the ServletContext (aka "application")
attributes: then the servlet can be unloaded and re-loaded at will and
the data will persist.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAlARnJ8ACgkQ9CaO5/Lv0PCaGQCgv7Kgkl04ElbBu5bkqNtuuX+v
5OsAn1MSf6i3CH9vIogXTb+aUKRYq0WN
=6vQK
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Permanent servlet in TC7

Posted by Jose MarĂ­a Zaragoza <de...@gmail.com>.
2012/7/26 Chip McVey <Ch...@quest.com>:
> Hi,
>
>
>
> In TC7, is there a way to tell Tomcat to never unload a given servlet unless Tomcat itself is being shutdown?  I want a single servlet instance that I can know will exist for the life of the tomcat process without being unloaded & reloaded (unless someone manually instructs Tomcat to do so, of course).

I know that this is not a valid answer, but I would like to know why
you'd like to do something like that

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Permanent servlet in TC7

Posted by Daniel Mikusa <dm...@vmware.com>.
----- Original Message -----
> Hi,
> 
> In TC7, is there a way to tell Tomcat to never unload a given servlet
> unless Tomcat itself is being shutdown?  I want a single servlet
> instance that I can know will exist for the life of the tomcat
> process without being unloaded & reloaded (unless someone manually
> instructs Tomcat to do so, of course).

I'm not sure this will do exactly what you want, but if you set "autoDeploy" on the Host tag to "false" it will prevent any applications from deploying / undeploying while Tomcat is running.

This would roughly tell Tomcat to start an app (and it's servlets) when Tomcat starts up and shut down an app (and it's servlets) when Tomcat is shutdown.  I say roughly because I believe there might be a few ways to work around that (manager, jmx).

 
> Yes, I know I'm "not supposed to do this" and that the servlet spec
> says a container is allowed to unload a server any time as long as a
> request is not in process.  I still would like to know if there's
> some Tomcat specific way to tell TC7 to never unload the servlet.
>  And yes, I realize even if there is such a way that future versions
> of Tomcat may remove this capability.  :)

Maybe you could take a step back and tell us about your general use case for this feature.  With that in mind, perhaps someone can suggest a solution to the general problem even if your specific request is not possible.
 
Dan
 

> Thanks in advance,
> 
> Chip
> 
> 
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org