-
Notifications
You must be signed in to change notification settings - Fork 10
Counting online users with wicket
astrapi69 edited this page Oct 7, 2015
·
13 revisions
The OnlineUsers class can track the users that are online in the application. It can take two generic parameters the USER and the ID. The USER is for the user class and the ID is for the session id.
The OnlineUsers class provides methods like if a specific user is online or how many users are online.
Here an example:
public class WicketApplication extends WebApplication {
...
`
/**
* Adds the user online.
*
* @param user
* the user
* @param sessionId
* the session id
* @return the string
*/
public synchronized String addUserOnline(Integer user, String sessionId, WicketSession session)
{
return onlineUsers.addOnline(user, sessionId, session);
}
/**
* Removes the user from the map.
*
* @param user
* the user
* @return the string
*/
public synchronized String removeUserOnline(Integer user)
{
return onlineUsers.removeOnline(user);
}
/**
* Removes the user from the map.
*
* @param user
* the user
* @return the string
*/
public synchronized Integer removeUserOnline(String sessionId)
{
return onlineUsers.remove(sessionId);
}
/**
* Checks if the user is online.
*
* @param userId
* the id from the user
* @return true, if the user is online
*/
public boolean isOnline(Integer userId)
{
return onlineUsers.isOnline(userId);
}
/**
* Checks if the user is online.
*
* @param user
* the user
* @return true, if the user is online
*/
public boolean isOnline(Users user)
{
if (user == null)
{
return false;
}
if (user.getId() == null)
{
return false;
}
return isOnline(user.getId());
}
/**
* Gets the user over the sessionId.
*
* @param sessionId
* the session id
* @return the user
*/
public Integer getUser(String sessionId)
{
return onlineUsers.getUser(sessionId);
}
/**
* Gets the wicket session over the sessionId.
*
* @param sessionId
* the session id
* @return the wicket session
*/
public WicketSession getSession(String sessionId)
{
return onlineUsers.getSession(sessionId);
}
/**
* Gets the wicket session over the user id.
*
* @param userId
* the id from the user
* @return the wicket session
*/
public WicketSession getSession(Integer userId)
{
return onlineUsers.get(userId);
}
/**
* Gets the wicket session over the user.
*
* @param user
* the user
* @return the wicket session
*/
public WicketSession getSession(Users user)
{
return getSession(user.getId());
}
/**
* Gets the size of the online users.
*
* @return how many users are at this moment online.
*/
public int getSizeOfOnlineUsers()
{
return onlineUsers.getSize();
}
/**
* OnlineUsers will be updated when this method is invoked.
*
* @see org.apache.wicket.session.ISessionStore.UnboundListener#sessionUnbound(java.lang.String)
*/
@Override
public void sessionUnbound(String sessionId)
{
super.sessionUnbound(sessionId);
Integer user = removeUserOnline(sessionId);
if (user != null)
{
LOGGER.info("The user '" + user + "' session is unbound. Session id:" + sessionId);
}
}
...
}
`