Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
811 views
in Technique[技术] by (71.8m points)

java - Xpages - Get number of active sessions (Lotus Domino 8.5.2)

How do I get the number of active sessions in Xpage. I'm trying to use managed beans but it just returns a weird string. Here's the simple code:

import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class SessionCounterListener implements HttpSessionListener {

  private static int totalActiveSessions;

  public static int getTotalActiveSessions(){
    return totalActiveSessions;
  }

  public void sessionCreated(HttpSessionEvent arg0) {
    totalActiveSessions++;
    System.out.println("sessionCreated - add one session into counter");
  }

  public void sessionDestroyed(HttpSessionEvent arg0) {
    totalActiveSessions--;
    System.out.println("sessionDestroyed - deduct one session from counter");
  } 
}

I got this from here. But when I call SessionCounterListener.getTotalActiveSessions(), it only returns 0 (regardless if someone is logged in).

If you have the time, you can check out my test database here.

Please help me. Thanks a lot!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

A session listener must be injected in the application to get working as desired. Otherwise the sessionCreated/sessionDestroyed methods will never be called and 0is returned always. But there are some differences between XPages and "traditional" JSF. Have a look here:

http://www.openntf.org/projects/pmt.nsf/66d9103768cc2fed85256c59006b5433/00a38cdf382f4893862578b80082dd15!OpenDocument

Or have a look inside the XSP Starterkit.

EDIT:

To add a session Listener, do the following:

  1. Switch to java perspective
  2. In the "Code/Java" folder, create a folder "META-INF"
  3. In this META-INF folder, create a folder "services"
  4. In the "services" folder create a file named "com.ibm.xsp.core.events.SessionListener"
  5. In this file, add the full name of your class: package.SessionCounterListener

Now, the session listener should be activated.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...