Re: Counting active sessions if server restarts very often
Thank you, Andrew, for your fast reply. I hope this works, but I still don't undestand what happens when the server is restarted when the sessionCount (current active sessions) is 10. Will sessionWillPassivate() and sessionDidActivate() be called 10 times? Why not once? But if they were called once, my count variable would be 1 instead of 10 after a restart. Could someone explain me in a few words what happens when the server is restarted? Thanx, Christian Bodycombe, Andrew wrote: The easiest way is probably to have a single listener that implements both the HttpSessionListener and HttpSessionActivationListener interfaces. This has a single count of active sessions. In the sessionCreated() method, increment the count In the sessionDestroyed() method, decrement the count In the sessionDidActivate() method, increment the count In the sessionWillPassivate() method, decrement the count. This should eliminate the need to store anything in the session. Hope this helps Andy -Original Message- From: Christian Hauser [mailto:[EMAIL PROTECTED] Sent: 04 September 2003 10:01 To: Tomcat Users List Subject: Re: Counting active sessions if server restarts very often Shapira, Yoav wrote: Howdy, Is there an other way to implement this? Maybe by saving the variable activeSessions to a session (which is restored when the server has restarted)? This is not a bad idea, and might be the easiest way. A DB write on shutdown/read on startup is also an option. To recapitulate: I want to display all active sessions (~ active users). I use HttpSessionListener and increment a static int field every time sessionCreated is called. Unfortunately the server is restarted very often so I'd like to remember the active sessions by putting them into the session. But how? Like this? public class SessionCounter implements HttpSessionListener { public synchronized void sessionCreated(HttpSessionEvent event) { Integer i = (Integer)event.getSession().getAttribute("session.counter"); if (i == null) { i = new Integer(0); } int activeSessions = i.intValue() + 1; event.getSession().setAttribute("session.counter", activeSessions); } public synchronized void sessionDestroyed(HttpSessionEvent event) { Integer i = (Integer)event.getSession().getAttribute("session.counter"); if (i == null) { i = new Integer(0); } int activeSessions = i.intValue(); if (activeSessions > 0) { activeSessions--; } event.getSession().setAttribute("session.counter", activeSessions); } public static int getActiveSessions() { return activeSessions; } } Jon Wingfield gave me the hint to put an object that implements HttpSessionActivationListener as an attribute to the session. But if I do that in the SessionListener#sessionCreated method I have 100 of those objects around when 100 concurrent users are using my web application. Does that make any sense? And what should I do when the object implementing HttpSessionActivationListener enters sessionWillPassivate? How do I save the count of active sessions? Sorry for all those questions, but I'd like to count the sessions even when the server restarts very often. Thank you for your help, Christian - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Counting active sessions if server restarts very often
Shapira, Yoav wrote: Howdy, Is there an other way to implement this? Maybe by saving the variable activeSessions to a session (which is restored when the server has restarted)? This is not a bad idea, and might be the easiest way. A DB write on shutdown/read on startup is also an option. To recapitulate: I want to display all active sessions (~ active users). I use HttpSessionListener and increment a static int field every time sessionCreated is called. Unfortunately the server is restarted very often so I'd like to remember the active sessions by putting them into the session. But how? Like this? public class SessionCounter implements HttpSessionListener { public synchronized void sessionCreated(HttpSessionEvent event) { Integer i = (Integer)event.getSession().getAttribute("session.counter"); if (i == null) { i = new Integer(0); } int activeSessions = i.intValue() + 1; event.getSession().setAttribute("session.counter", activeSessions); } public synchronized void sessionDestroyed(HttpSessionEvent event) { Integer i = (Integer)event.getSession().getAttribute("session.counter"); if (i == null) { i = new Integer(0); } int activeSessions = i.intValue(); if (activeSessions > 0) { activeSessions--; } event.getSession().setAttribute("session.counter", activeSessions); } public static int getActiveSessions() { return activeSessions; } } Jon Wingfield gave me the hint to put an object that implements HttpSessionActivationListener as an attribute to the session. But if I do that in the SessionListener#sessionCreated method I have 100 of those objects around when 100 concurrent users are using my web application. Does that make any sense? And what should I do when the object implementing HttpSessionActivationListener enters sessionWillPassivate? How do I save the count of active sessions? Sorry for all those questions, but I'd like to count the sessions even when the server restarts very often. Thank you for your help, Christian - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Counting active sessions if server restarts very often
Hello list I implemented a session counter to count all active sessions. Now I have the problem that because the server is being restarted very often, my static variable activeSessions is always set to 0. Is there an other way to implement this? Maybe by saving the variable activeSessions to a session (which is restored when the server has restarted)? I'd like to know the previous value (= value before restart) of the active session count when the server has been restarted. Thank you in advance for any hint on that. Christian Here's my actual code: public class SessionCounter implements HttpSessionListener { private static int activeSessions = 0; public synchronized void sessionCreated(HttpSessionEvent event) { activeSessions++; } public synchronized void sessionDestroyed(HttpSessionEvent event) { if (activeSessions > 0) { activeSessions--; } } public static int getActiveSessions() { return activeSessions; } } - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
System properties
Hello I'd like to know how I can set system properties when starting Tomcat 4.1. I mean those properties that I can get from within a JSP file as: System.getProperty("CONFIG_HOME") Thank you in advance for any hint. Christian - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: HttpSessionListener: Negative session count
Thank you very much, Ralph. Regards, Christian Ralph Einfeldt wrote: You don't need the useBean. A bean with session scope doesn't make sense at all, if you want to access the methods through an object you can make the object application global. Or you can make sessionCreated/sessionDestroyed static and just call them through the class. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: HttpServletRequest not found from within library
Hi Yiannis I didn't work. I stopped tomcat, removed servlet.jar from the CLASSPATH and restarted it. Same exception. servlet.jar can only be found in CATALINA_HOME/common/lib. Christian Yiannis Mavroukakis wrote: IMHO you should try removing servlet.jar from your classpath, as you probably only need it on compile time, and restart Tomcat. Let me know if this worked for you. Yiannis. -Original Message- From: Christian Hauser [mailto:[EMAIL PROTECTED] Sent: 13 August 2003 09:36 To: Tomcat Users List Subject: HttpServletRequest not found from within library Hello all I'm using Tomcat 4.1.24 and have the following problem. In a JSP file I have a bean called nav pointing to a class some.package.Navigation. This class is located at a JAR file in the lib directory of the web application. ... <% nav.handleRequest(request, ...); ... In the Navigation class I get a java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest But the HttpServletRequest Interface is imported: import javax.servlet.http.*; Furthermore the CLASSPATH contains the path to servlet.jar, which also is located in /common/lib. I can't figure out why I get this error and how to make it find the servlet.jar. Thank you in advance for every hint. Christian - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] This e-mail has been scanned for all viruses by Star Internet. The service is powered by MessageLabs. For more information on a proactive anti-virus service working around the clock, around the globe, visit: http://www.star.net.uk This e-mail has been scanned for all viruses by Star Internet. The service is powered by MessageLabs. For more information on a proactive anti-virus service working around the clock, around the globe, visit: http://www.star.net.uk - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: HttpSessionListener: Negative session count
I have a further question: Is it correct that I don't have to use and set the scope to "session"? Because now I don't use "useBean". Should I? Thank you in advance, Christian Christian Hauser wrote: Hello J2EE programmers I wrote a JSP some time ago that uses a class SessionCounter (which implements HttpSessionListener) to count the active sessions of a web application and to display them. Unfortunately there seems to be a problem with my program, because the web application says that there are currently -18 active sessions. However, the lowest possible count should be 0. Of course I could implement the sessionDestroyed the following way (to prohibit a negative session count, but then the current session count would not be correct, not? public void sessionDestroyed(HttpSessionEvent event) { if (activeSessions > 0) activeSessions--; } Code of JSP file: Concurrent sessions: <%= SessionCounter.getActiveSessions() %> Last refresh: <%= new Date() %> Last session change: <%= SessionCounter.getLastChange() %> Here's the class SessionCounter, which belongs to a utility JAR that is situated in the lib directory of The web application. public class SessionCounter implements HttpSessionListener { /** Static variable to keep track of the current number of active sessions. */ private static int activeSessions = 0; public void sessionCreated(HttpSessionEvent event) { activeSessions++; } public void sessionDestroyed(HttpSessionEvent event) { activeSessions--; } public static int getActiveSessions() { return activeSessions; } } I'm using Tomcat 4.1.24 and hope that someone of you might give me a hint on how I can get a reliable session counter. Christian - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
HttpServletRequest not found from within library
Hello all I'm using Tomcat 4.1.24 and have the following problem. In a JSP file I have a bean called nav pointing to a class some.package.Navigation. This class is located at a JAR file in the lib directory of the web application. ... <% nav.handleRequest(request, ...); ... In the Navigation class I get a java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest But the HttpServletRequest Interface is imported: import javax.servlet.http.*; Furthermore the CLASSPATH contains the path to servlet.jar, which also is located in /common/lib. I can't figure out why I get this error and how to make it find the servlet.jar. Thank you in advance for every hint. Christian - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Multiple Tomcat instances
Angus Mezick wrote: Can the developers have a tomcat instance running inside of eclipse on their desktops? This will allow them do use the debugger. --Angus I haven't yet thought about that. I guess it should be possible, in fact it would be great if that works. Do you know how simple that would be to set up? Christian - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: HttpSessionListener: Negative session count
Thank you Ralph So I should use the following: public void sessionCreated(HttpSessionEvent event) { synchronized (this) { activeSessions++; } } public void sessionDestroyed(HttpSessionEvent event) { synchronized (this) { if (activeSessions > 0) { activeSessions--; } } } Ralph Einfeldt wrote: You have to synchronize the -- and ++ operations. Otherwise you will have unpredictable results. You have to keep in mind that activeSessions++ is not atomic, so another thread can get between the computation of the value and the assignment. One scenario: Thread A: read activeSessions = 0 Thread B: read activeSessions = 0 Thread A: compute activeSessions + 1= 1 Thread B: compute activeSessions + 1= 1 Thread A: store the result in activeSessions = 1 Thread B: store the result in activeSessions = 1 Now activeSessions is 1 although 2 Sessions are active. If the sessions are not destroyed in a very close gap the result will get negative. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: HttpSessionListener: Negative session count
Hi Yoav That could be the problem as well. So if the server is restarted my session counter variable is set to 0, but there might still be 30 active sessions. I didn't think of that problem, because I thought that the sessions would be invalidated when the server is restarted. Has anyone a good solution to this problem? Or should I just prevent the session counter variable from becoming negative and after a while (session timeout for all remaining sessions) the counter will be working correctly again? Cheers, Christian Shapira, Yoav wrote: Howdy, I would mention one other possibility, unrelated to synchronization, which may cause this behavior. If your session listener simply increments a counter on every sessionCreated() and decrements the counter on every sessionDestroyed(), it is vulnerable to this behavior. When the server is restarted, if there are active sessions they are persisted by default. Then the server comes back up, and all these sessions are active, without a session created event, so your listener has a counter value of 0, but there are actually >0 active sessions. When these are destroyed as usual, sessionDestroyed() will be called for them, decrementing your counter and resulting in a negative value. Yoav Shapira Millennium ChemInformatics - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
HttpSessionListener: Negative session count
Hello J2EE programmers I wrote a JSP some time ago that uses a class SessionCounter (which implements HttpSessionListener) to count the active sessions of a web application and to display them. Unfortunately there seems to be a problem with my program, because the web application says that there are currently -18 active sessions. However, the lowest possible count should be 0. Of course I could implement the sessionDestroyed the following way (to prohibit a negative session count, but then the current session count would not be correct, not? public void sessionDestroyed(HttpSessionEvent event) { if (activeSessions > 0) activeSessions--; } Code of JSP file: Concurrent sessions: <%= SessionCounter.getActiveSessions() %> Last refresh: <%= new Date() %> Last session change: <%= SessionCounter.getLastChange() %> Here's the class SessionCounter, which belongs to a utility JAR that is situated in the lib directory of The web application. public class SessionCounter implements HttpSessionListener { /** Static variable to keep track of the current number of active sessions. */ private static int activeSessions = 0; public void sessionCreated(HttpSessionEvent event) { activeSessions++; } public void sessionDestroyed(HttpSessionEvent event) { activeSessions--; } public static int getActiveSessions() { return activeSessions; } } I'm using Tomcat 4.1.24 and hope that someone of you might give me a hint on how I can get a reliable session counter. Christian - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: System properties
Tim Funk wrote: http://jakarta.apache.org/tomcat/faq/misc.html#properties -Tim Thank you Tim for pointing me to the right place. But what do I have to do if I would like to set more than one system property? export JAVA_OPTS='-DpropName1=propValue1 -DpropName2=propValue2' ??? Regards, Christian - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Multiple Tomcat instances
Hello Tomcat cracks I'm new to Tomcat administration and would like to switch from ServletExec to Tomcat 4.1. However, I have some difficulty understanding how to set up different Tomcat instances. I'd like to have different Tomcat instances for each developer (to test web applications) and for each application. Tomcat is installed into /usr/local/tomcat-4.1.24 (CATALINA_HOME). Every developer and web application should now have its own instance somewhere (CATALINA_BASE). For example: /home/chauser/tomcat in my case (CATALINA_BASE). This directory contains some folders: logs, conf, work, webapps, bin. Which port to change? In the conf directory I'd like to put a tailored versions of web.xml and server.xml, but I don't know which Port I have to change. How to deal with user/root problems? When I have a start, stop or restart script in /bin, I cannot startup or shutdown tomcat, because the startup.sh and shutdown.sh (in /bin) belong to root. Is there a good Tomcat 4.1 administration reference available online (apart from the Tomcat documentation)? In about 2-3 weeks I'll get the book "Tomcat: The Definitive Guide (O'Reilly)", however, I'd like to progress until then. Thank you in advance for all your help. Christian - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]