You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Elie Medeiros <nc...@networkphotographers.com> on 2004/03/03 11:44:19 UTC

addShutdownHook in Tomcat does not seem to get called on shutdown

Hi,

I added a shutdown hook in my app, which works fine when I run it in
standalone mode, but which does not seem to get called when Tomcat stops.

The shutdown hook operates according to following the following semantics:
__________________________________________________________________________
class MyApp{

public void doSomething(){
	ShutdownHook sdh = new ShutdownHook();
	synchronized(Runtime.getRuntime()){
		Runtime.getRuntime().addShutdownHook(sdh);
	}
	//
	//do something here
	//
	logger.info("Finished doing something");
	//remove shutdown hook once process has finished
	sdh.setFinished();
	if (!sdh.isInitialised()){
		synchronized(Runtime.getRuntime()){
			Runtime.getRuntime().removeShutdownHook(sdh);
		}
	}
}

	private class ShutdownHook extends Thread {
		private boolean INITIALISED = false;
		private boolean FINISHED = false;

		public void run() {
			this.INITIALISED = true;
			this.setPriority(2);
			logger.debug("Shutdown hook: shutdown thread started - a shutdown has
been requested");
			out :
			while (true) {
				//keep on looping until the claaing app has finished
				synchronized(this.FINISHED){
					if (this.FINISHED == true) {
						logger.debug("Shutdown hook: parent program finished, allowing
shutdown process to complete");
						return;
					}
				}
			}
		}

		public synchronized boolean isInitialised() {
				return this.INITIALISED;
		}

		public synchronized void setFinished() {
				this.FINISHED = true;
		}
	}
}
__________________________________________________________________________


The log does not show any trace of the shutdown hook being called, and
the process does indeed not complete before Tomcat shuts down, which to
me sounds like the hook is not getting registered properly for some
reason. Any ideas why this might be happening? I am running Tomcat
4.1.18 on Windows 2K (no advice about switching to Linux please).

Thanks,

Elie


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


Re: Can't access session id

Posted by Frank Burns <fr...@the-hub.demon.co.uk>.
Tim, that's great.
I now realise, following from your advice, the difference between JSTL
Expression Language implicit variables and the JSP implicit objects.
Thanks,
Frank.

Thanks, also, to Justin Ruthenbeck for the JSP expression script version.

----- Original Message ----- 
From: "Tim Funk" <fu...@joedog.org>
To: "Tomcat Users List" <to...@jakarta.apache.org>
Sent: Thursday, March 04, 2004 1:12 AM
Subject: Re: Can't access session id


> You need to use the pageContext implicit object.
> session id = <c:out value="${pageContext.request.session.id}" />
>
> -Tim
>
> Frank Burns wrote:
>
> > I need to access the session id from within a JSP and pass it,
explicitly,
> > to a Flash-based client.
> >
> > I am using the following code fragment as part of my JSP, but the value
> > returned for the session id is always blank.
> >
> > Am I doing something wrong?
> >
> > <%@page contentType="text/xml" session="true" %>
> > <?xml version="1.0" encoding="UTF-8"?>
> > <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
> >  <myResponse>
> >     <sessionId>
> >         session id = <c:out value="${sessionScope.id}" />
> >     </sessionId>
> > </myResponse>
> >
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
> >
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
>


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


Re: Can't access session id

Posted by Tim Funk <fu...@joedog.org>.
You need to use the pageContext implicit object.
session id = <c:out value="${pageContext.request.session.id}" />

-Tim

Frank Burns wrote:

> I need to access the session id from within a JSP and pass it, explicitly,
> to a Flash-based client.
> 
> I am using the following code fragment as part of my JSP, but the value
> returned for the session id is always blank.
> 
> Am I doing something wrong?
> 
> <%@page contentType="text/xml" session="true" %>
> <?xml version="1.0" encoding="UTF-8"?>
> <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
>  <myResponse>
>     <sessionId>
>         session id = <c:out value="${sessionScope.id}" />
>     </sessionId>
> </myResponse>
> 
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
> 
> 


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


Re: Can't access session id

Posted by Justin Ruthenbeck <ju...@nextengine.com>.
-----==== Start jsp source ====-----
The sessionId is: <%session.getSessionId() %>.
-----==== END jsp source   ====-----

I'm sure you can do it with tablibs as well, but I don't use 'em, so I'm 
not the one to ask.

justin


At 03:06 PM 3/3/2004, you wrote:
>So what is the best way to access the session id?
>
>
>----- Original Message -----
>From: "Ed Bicker" <gu...@travelin.com>
>To: "Tomcat Users List" <to...@jakarta.apache.org>
>Sent: Wednesday, March 03, 2004 10:25 PM
>Subject: RE: Can't access session id
>
>
> > I think the "out" object is not referenced correctly. The out  object
>writes
> > into the output stream to the client, but this is a "buffered" 
> version of
> > the java.io.PrintWriter class and is of type 
> javax.servlet.jsp.JspWriter.
>I
> > believe the c:out is pointing to the wrong value or the sessionScope 
> field
> > is not correctly indexed.
> >
> > -----Original Message-----
> > From: Frank Burns [mailto:frankburns@the-hub.demon.co.uk]
> > Sent: Wednesday, March 03, 2004 5:07 PM
> > To: Tomcat Users List
> > Subject: Can't access session id
> >
> >
> > I need to access the session id from within a JSP and pass it, 
> explicitly,
> > to a Flash-based client.
> >
> > I am using the following code fragment as part of my JSP, but the value
> > returned for the session id is always blank.
> >
> > Am I doing something wrong?
> >
> > <%@page contentType="text/xml" session="true" %>
> > <?xml version="1.0" encoding="UTF-8"?>
> > <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
> >  <myResponse>
> >     <sessionId>
> >         session id = <c:out value="${sessionScope.id}" />
> >     </sessionId>
> > </myResponse>
> >
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
> >
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
> >
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


______________________________________________
Justin Ruthenbeck
Software Engineer, NextEngine Inc.
justinr - AT - nextengine DOT com
Confidential. See:
http://www.nextengine.com/confidentiality.php
______________________________________________


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


Re: Can't access session id

Posted by Frank Burns <fr...@the-hub.demon.co.uk>.
So what is the best way to access the session id?


----- Original Message ----- 
From: "Ed Bicker" <gu...@travelin.com>
To: "Tomcat Users List" <to...@jakarta.apache.org>
Sent: Wednesday, March 03, 2004 10:25 PM
Subject: RE: Can't access session id


> I think the "out" object is not referenced correctly. The out  object
writes
> into the output stream to the client, but this is a "buffered" version of
> the java.io.PrintWriter class and is of type javax.servlet.jsp.JspWriter.
I
> believe the c:out is pointing to the wrong value or the sessionScope field
> is not correctly indexed.
>
> -----Original Message-----
> From: Frank Burns [mailto:frankburns@the-hub.demon.co.uk]
> Sent: Wednesday, March 03, 2004 5:07 PM
> To: Tomcat Users List
> Subject: Can't access session id
>
>
> I need to access the session id from within a JSP and pass it, explicitly,
> to a Flash-based client.
>
> I am using the following code fragment as part of my JSP, but the value
> returned for the session id is always blank.
>
> Am I doing something wrong?
>
> <%@page contentType="text/xml" session="true" %>
> <?xml version="1.0" encoding="UTF-8"?>
> <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
>  <myResponse>
>     <sessionId>
>         session id = <c:out value="${sessionScope.id}" />
>     </sessionId>
> </myResponse>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
>


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


RE: Can't access session id

Posted by Ed Bicker <gu...@travelin.com>.
I think the "out" object is not referenced correctly. The out  object writes
into the output stream to the client, but this is a "buffered" version of
the java.io.PrintWriter class and is of type javax.servlet.jsp.JspWriter. I
believe the c:out is pointing to the wrong value or the sessionScope field
is not correctly indexed.

-----Original Message-----
From: Frank Burns [mailto:frankburns@the-hub.demon.co.uk]
Sent: Wednesday, March 03, 2004 5:07 PM
To: Tomcat Users List
Subject: Can't access session id


I need to access the session id from within a JSP and pass it, explicitly,
to a Flash-based client.

I am using the following code fragment as part of my JSP, but the value
returned for the session id is always blank.

Am I doing something wrong?

<%@page contentType="text/xml" session="true" %>
<?xml version="1.0" encoding="UTF-8"?>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
 <myResponse>
    <sessionId>
        session id = <c:out value="${sessionScope.id}" />
    </sessionId>
</myResponse>




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




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


Can't access session id

Posted by Frank Burns <fr...@the-hub.demon.co.uk>.
I need to access the session id from within a JSP and pass it, explicitly,
to a Flash-based client.

I am using the following code fragment as part of my JSP, but the value
returned for the session id is always blank.

Am I doing something wrong?

<%@page contentType="text/xml" session="true" %>
<?xml version="1.0" encoding="UTF-8"?>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
 <myResponse>
    <sessionId>
        session id = <c:out value="${sessionScope.id}" />
    </sessionId>
</myResponse>




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


RE: addShutdownHook in Tomcat does not seem to get called on shutdown

Posted by Peter Guyatt <pg...@telesoft-technologies.com>.
Hi There,

	Why not use the ServletContextListener interface to do all of your cleanup
stuff when the contextDestroyed method is called ?

Pete

-----Original Message-----
From: Elie Medeiros [mailto:nc-ml@networkphotographers.com]
Sent: 03 March 2004 10:44
To: tomcat-user@jakarta.apache.org
Subject: addShutdownHook in Tomcat does not seem to get called on
shutdown



Hi,

I added a shutdown hook in my app, which works fine when I run it in
standalone mode, but which does not seem to get called when Tomcat stops.

The shutdown hook operates according to following the following semantics:
__________________________________________________________________________
class MyApp{

public void doSomething(){
	ShutdownHook sdh = new ShutdownHook();
	synchronized(Runtime.getRuntime()){
		Runtime.getRuntime().addShutdownHook(sdh);
	}
	//
	//do something here
	//
	logger.info("Finished doing something");
	//remove shutdown hook once process has finished
	sdh.setFinished();
	if (!sdh.isInitialised()){
		synchronized(Runtime.getRuntime()){
			Runtime.getRuntime().removeShutdownHook(sdh);
		}
	}
}

	private class ShutdownHook extends Thread {
		private boolean INITIALISED = false;
		private boolean FINISHED = false;

		public void run() {
			this.INITIALISED = true;
			this.setPriority(2);
			logger.debug("Shutdown hook: shutdown thread started - a shutdown has
been requested");
			out :
			while (true) {
				//keep on looping until the claaing app has finished
				synchronized(this.FINISHED){
					if (this.FINISHED == true) {
						logger.debug("Shutdown hook: parent program finished, allowing
shutdown process to complete");
						return;
					}
				}
			}
		}

		public synchronized boolean isInitialised() {
				return this.INITIALISED;
		}

		public synchronized void setFinished() {
				this.FINISHED = true;
		}
	}
}
__________________________________________________________________________


The log does not show any trace of the shutdown hook being called, and
the process does indeed not complete before Tomcat shuts down, which to
me sounds like the hook is not getting registered properly for some
reason. Any ideas why this might be happening? I am running Tomcat
4.1.18 on Windows 2K (no advice about switching to Linux please).

Thanks,

Elie


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


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