Find Number of Open Sessions
Easy does not mean no database in this case. Also relying on session to see how many users are active is not reliable.
If you wanted to go that route, you could do a cronjob that is run by a safe process every few minutes and saves that count into a file or db, which PHP reads.
But i recommend going the database route.
Retrieve number of open sessions from ServletContainer or ApplicationServer
I don't think servlet Api provide this. but you can do it by functionly.
Create map of user and session object.
Map<User, HttpSession> logggedUserMap = new HashMap<User, HttpSession>();
Add entry inside while user logged in and remove it when logout.
so logggedUserMap.size()
value is total opened user session.
Use HttpSessionBindingListener, which will track of anywhere in code session attribute bound or unbound from a session.
Create class
class SessionObject implements HttpSessionBindingListener {
String message = "";
User loggedInUser;
Logger log = Logger.getLogger(SessionObject.class);
public SessionObject(User loggedInUser) {
this.loggedInUser=loggedInUser;
}
public void valueBound(HttpSessionBindingEvent event) {
log.info("=========in valueBound method==============");
HttpSession session =LoggedInUserSessionUtil.getLogggedUserMap().get(loggedInUser);
try{
if (session != null && session.getLastAccessedTime() != 0) {
message = "ALL_READY_LOGGEDIN";
return;
}
}catch(IllegalStateException e){
e.printStackTrace();
session = LoggedInUserSessionUtil.removeLoggedUser(loggedInUser);
}
System.out.println("*************************************"+event.getSession().getId() +"------"+loggedInUser+"*********************************************");
log.info("=========valueBound putting in user map==============");
LoggedInUserSessionUtil.getLogggedUserMap().put(loggedInUser, event.getSession());
return;
}
public void valueUnbound(HttpSessionBindingEvent event) {
// This work already doing in Force logout servlet
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
And bind this object instance while user logged in.
SessionObject sessionObj = new SessionObject(loggedInUser);
req.getSession().setAttribute("Binder.object",sessionObj);
Can I find out the number of open sessions for my web aplication (IIS 7 and higher) programmatically
You can do this with a global.asax file something like this:
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
Application.Lock()
Application("User_Count") = 0
Application.UnLock()
End Sub
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
Application.Lock()
Application("User_Count") = Integer.Parse(Application("User_Count")) + 1
Application.UnLock()
End Sub
Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
Application.Lock()
Application("User_Count") = Integer.Parse(Application("User_Count")) - 1
Application.UnLock()
End Sub
Then Application("User_Count") will have the number of non-expired sessions. To access from a console application you might need to create a service on your site that you can call and retrieve the value.
Is there a way to monitor how many open sessions in AEM instance?
The starting point could be this article which specifically deals with Unclosed sessions.
https://helpx.adobe.com/experience-manager/kb/AnalyzeUnclosedSessions.html
In one of my projects i had to fix unclosed sessions throughout the codebase and the first step obviously was to find the places causing the leak.
On a unix machine the command below helped me finding the most meaningful information. the command basically does a grep on the Unclosed session warning and find the line causing it in your java or JSP code, and than sort it to list the biggest culprit at the top.
grep -h -A18 "Unclosed session detected" error.log* | grep -E "YOUR_PACKAGE_NAME|org.apache.jsp" | sort | uniq -c | sort -nr
hope this helps.
How can I find out the amount of open sessions programatically in Tomcat using Java?
You can find this info using JMX. See here for how to enable JMX and what variables to query.
Using an Ant JMX task you can use:
<!-- get all sessions and split result as delimiter <em>SPACE</em> for easy
access all session ids directly with ant property sessions.[0..n].
-->
<jmx:invoke
name="Catalina:type=Manager,path=/ClusterTest,host=localhost"
operation="listSessionIds"
resultproperty="sessions"
echo="false"
delimiter=" "
/>
but you can use other tools e.g. JConsole.
How to list active / open connections in Oracle?
Use the V$SESSION
view.
V$SESSION
displays session information for each current session.
Related Topics
Warning: MySQLi_Query() Expects Parameter 1 to Be MySQLi, String Given In
Laravel 5 Not Finding CSS Files
Remove Diacritics from a String
Exotic Names for Methods, Constants, Variables and Fields - Bug or Feature
How to Assign PHP Variable Value to JavaScript Variable
Do I Need to Restart Apache After Changing the PHP.Ini File
Set an Environment Variable in .Htaccess and Retrieve It in PHP
Selecting All Columns That Start with Xxx Using a Wildcard
Simple PHP Contact Form with Firebase Hosting
Pg_Query Result Contains Strings Instead of Integer, Numeric
MySQL Error: 1364 Field 'Display_Name' Doesn't Have Default Value
Codeigniter - Why Use Xss_Clean
How to Get Unique Value in Multidimensional Array