KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quikj > application > web > talk > feature > proactive > server > ProactiveServiceController


1 /*
2  * ProactiveServiceController.java
3  *
4  * Created on April 29, 2003, 7:17 AM
5  */

6
7 package com.quikj.application.web.talk.feature.proactive.server;
8
9 import java.io.*;
10 import java.util.*;
11
12 import com.quikj.server.framework.*;
13
14 /**
15  *
16  * @author amit
17  */

18 public class ProactiveServiceController extends com.quikj.server.framework.AceThread
19 {
20     private static ProactiveServiceController instance = null;
21     private static final long SESSION_TIMEOUT = 30 * 60 * 1000L;
22     
23     private int timerId = -1;
24     
25     /** Creates a new instance of ProactiveServiceController */
26     public ProactiveServiceController() throws IOException
27     {
28         super("ProactiveServiceController");
29         instance = this;
30     }
31     
32     public static ProactiveServiceController getInstance()
33     {
34         return instance;
35     }
36     
37     public void run()
38     {
39         try
40         {
41             timerId = AceTimer.Instance().startTimer(SESSION_TIMEOUT, 0L);
42             if (timerId == -1)
43             {
44                 AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
45                 getName()
46                 + "- ProactiveServiceController.run() -- Could not start timer - "
47                 + getErrorMessage());
48             }
49             
50         }
51         catch (IOException ex)
52         {
53             // should not happen
54
}
55         
56         while (true)
57         {
58             AceMessageInterface message = waitMessage();
59             if (message == null)
60             {
61                 // print error message
62
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
63                 getName()
64                 + "- ProactiveServiceController.run() -- A null message was received while waiting for a message - "
65                 + getErrorMessage());
66                 
67                 break;
68             }
69             
70             if ((message instanceof AceSignalMessage) == true)
71             {
72                 // A signal message is received
73

74                 // print informational message
75
AceLogger.Instance().log(AceLogger.INFORMATIONAL, AceLogger.SYSTEM_LOG,
76                 getName()
77                 + " - ProactiveServiceController.run() -- A signal "
78                 + ((AceSignalMessage)message).getSignalId()
79                 + " is received : "
80                 + ((AceSignalMessage)message).getMessage());
81                 break;
82             }
83             else if ((message instanceof AceTimerMessage) == true)
84             {
85                 timerId = -1;
86
87                 // process the timer message
88
removeOldSessions();
89                 
90                 // and re-start the time
91
try
92                 {
93                     timerId = AceTimer.Instance().startTimer(SESSION_TIMEOUT, 0L);
94                     if (timerId == -1)
95                     {
96                         AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
97                         getName()
98                         + "- ProactiveServiceController.run() -- Could not re-start timer - "
99                         + getErrorMessage());
100                     }
101                 }
102                 catch (IOException ex)
103                 {
104                     // should not happen
105
}
106             }
107         }
108         
109         dispose();
110     }
111     
112     private void removeOldSessions()
113     {
114         long d = (new Date()).getTime() - SESSION_TIMEOUT;
115         
116         ProactiveUserData.getInstance().removeSessions(new Date(d));
117     }
118     
119     public void dispose()
120     {
121         if (instance != null) // if not previously disposed
122
{
123             // interrupt the wait (kill this thread)
124
interruptWait(AceSignalMessage.SIGNAL_TERM, "disposed");
125             
126             if (timerId != -1)
127             {
128                 try
129                 {
130                     AceTimer.Instance().cancelTimer(timerId);
131                 }
132                 catch (IOException ex)
133                 {
134                     // should not happen
135
}
136                 timerId = -1;
137             }
138             
139             super.dispose();
140             instance = null;
141         }
142     }
143 }
144
Popular Tags