4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
SessionCleaner.java
Go to the documentation of this file.
1 /*
2  * Project: Server for annotations sharing
3  * Author: Ing. Jaroslav Dytrych idytrych@fit.vutbr.cz
4  * File: SessionCleaner.java
5  * Description: Class that provides cleaning of unactive sessions (basic version, not final)
6  */
7 
8 /**
9  * @file SessionCleaner.java
10  *
11  * @brief Class that provides cleaning of unactive sessions (basic version, not final)
12  */
13 package cz.vutbr.fit.knot.annotations.comet;
14 
17 import java.util.Iterator;
18 import java.util.Timer;
19 import java.util.TimerTask;
20 
21 /**
22  * Class that provides cleaning of unactive sessions (basic version, not final)
23  *
24  * @brief Class that provides cleaning of unactive sessions (basic version, not final)
25  * @author Martin Petr (xpetrm05)
26  */
27 public class SessionCleaner {
28 
29  /** timer that execute task CheckAndClean evry time period*/
30  private Timer timer = null;
31  /** represent maximum time for confirmation of activity from client */
32  private long confirmPeriod;
33  /** miliseconds in one second constant */
34  public static final int SECOND = 1000;
35  /** timer tick period */
36  public static final int TIMER_PERIOD_S = 60;
37 
38  /**
39  * Constructor
40  */
41  public SessionCleaner() {
42  confirmPeriod = Constants.COMET_TIMEOUT_SEC*SECOND*Constants.SESSION_CLEAN_INTERVAL;
43  //timer init
44  if(timer == null){
45  timer = new Timer();
46  timer.schedule(new CheckAndClean(),TIMER_PERIOD_S * SECOND, TIMER_PERIOD_S * SECOND);
47  }
48  }
49 
50  /**
51  * A task that can be scheduled for one-time or repeated execution by a Timer.
52  *
53  * @brief Class implement run() method. That method go trough list of sessions
54  * and try to find expired sessions. First kind of expiration occur in case of
55  * client unactivity second kind occur in case of lost connection with annotation
56  * client. This object detect both cases and remove session from sessions list.
57  */
58  class CheckAndClean extends TimerTask {
59  /**
60  * Method go trough list of sessions and try to find expired sessions. That sessions
61  * delete.
62  */
63  @Override
64  public void run() {
65  synchronized(AppBean.getSessions()){
66  Iterator <EditorSession> sessionsIt = AppBean.getSessions().iterator();
67  if(sessionsIt == null)return;
68 
69  while(sessionsIt.hasNext()){
70  EditorSession actualSession = sessionsIt.next();
71  long actualTime = System.currentTimeMillis();
72 
73  if(actualSession == null || actualTime - actualSession.getLastConfirmTime() > confirmPeriod){
74  if(actualSession != null && actualSession.getSyncDocument() != null)
75  AppBean.decrementDocumentUsage(actualSession.getSyncDocument().getId());
76  sessionsIt.remove();
77  continue;
78  }
79  }
80  } // synchronized(AppBean.getSessions())
81  } // run()
82  } // class CheckAndClean
83 
84  /**
85  * Sets actual time stamp for session confirm time
86  *
87  * @param sessionId Id of session that will be update
88  */
89  public void setSessionConfirm(long sessionId){
90  if(AppBean.getSession(sessionId) != null){
91  AppBean.getSession(sessionId).setLastConfirmTime(System.currentTimeMillis());
92  }
93  }
94 } // public class SessionCleaner
Singleton for storing global variables.
Definition: AppBean.java:47
static synchronized void decrementDocumentUsage(Integer documentId)
Definition: AppBean.java:492
Class that provides cleaning of unactive sessions (basic version, not final)
static EditorSession getSession(long id)
Definition: AppBean.java:338
Informations about client session.
Class implement run() method. That method go trough list of sessions and try to find expired sessions...