Hi Shapira,
I updated my log4j.properties with:
log4j.appender.R.layout.ConversionPattern=%p %t %c - %x %m%n
where %x defines NDC output.
Added the following code to my JSP:
int callId = session.getAttribute("CallId");
org.apache.log4j.NDC.push("CallId:" + callId);
...
org.apache.log4j.NDC.remove();
and got this in my log-file:
INFO Thread-5 NDCtest - CallId:2004117 Testing Log4j NDC.
Looks really nice.
Thanks
From: "Shapira, Yoav" <[EMAIL PROTECTED]>
Reply-To: "Tomcat Users List" <[EMAIL PROTECTED]>
To: "Tomcat Users List" <[EMAIL PROTECTED]>
Subject: RE: How to keep same thread during a session
Date: Thu, 21 Aug 2003 10:10:03 -0400
Howdy,
I like Senor Einfeldt's filter suggestion.
One other interesting possibility for you would be log4j's mapped
diagnostic context (MDC:
http://jakarta.apache.org/log4j/docs/api/org/apache/log4j/MDC.html) which
handles these things automatically on a per-thread basis, so you might not
have to do any coding.
Yoav Shapira
Millennium ChemInformatics
>-----Original Message-----
>From: Roland Nygren [mailto:[EMAIL PROTECTED]
>Sent: Thursday, August 21, 2003 9:48 AM
>To: [EMAIL PROTECTED]
>Subject: How to keep same thread during a session
>
>
>I have an application consisting of JSPs, Servlets and Java classes
running
>in Tomcat 4.1.
>Normal HTTPSession isused to keep customer session.
>Each new customer session is given a unique Id called CallId.
>I.e. must this CallId be used in all logging.
>I would like to not have to pass this CallId as a parameter in every
>method.
>Ex.
>Servlet or JSP code:
>int CallId = session.getParameter("CallId");
>fareFinder.search(from, to, departureDate, returnDate,
numberOfPassengers,
>callId);
>
>FareFinder code:
>search(...) {
> log(msg, callId);
>}
>
>To solve this I first thought of using
>session.getParameter("CallId");
>in the Java code (log method) but that only means I have to pass the
>session
>object as a aprameter in every method call instead.
>
>Next thought was that if each session keeps running on the same thread
>during the lifetime of the session I could keep a list in a singleton (or
>JNDI) with thread hashcodes and CallIds.
>Ex. log method:
>log (String msg) {
> int threadId = Thread.currentThread().hashCode();
> callId = callIdListSingleton.get(threadId);
> ...
>}
>
>To test if the same thread is used during a session I tried to put the
>thread hashcode in the HTTPSession
>test.jsp:
><html> <br>
><%
> out.print(session.getMaxInactiveInterval() / 60);
> out.print("<hr>");
> session.setAttribute("hash", new
>Integer(Thread.currentThread().hashCode())
>);
> out.print(Thread.currentThread().hashCode());
> out.print("<hr>");
> out.print(session.getAttribute("hash"));
>%>
></html>
>
>Reloading this page in intervals of 10-20 seconds show that a session
does
>NOT execute on tha same thread.
>I tried to find a configuration that solves this but didn't.
>Is there any way to keep the same thread during the session.
>The application is not heavily used the number of threads will not be a
>problem.
>
>_________________________________________________________________
>Hitta r�tt k�pare p� MSN K�p & S�lj http://www.msn.se/koposalj
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: [EMAIL PROTECTED]
>For additional commands, e-mail: [EMAIL PROTECTED]
This e-mail, including any attachments, is a confidential business
communication, and may contain information that is confidential,
proprietary and/or privileged. This e-mail is intended only for the
individual(s) to whom it is addressed, and may not be saved, copied,
printed, disclosed or used by anyone else. If you are not the(an) intended
recipient, please immediately delete this e-mail from your computer system
and notify the sender. Thank you.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
_________________________________________________________________
Hitta r�tt p� n�tet med MSN S�k http://search.msn.se/
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]