KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > web > tomcat > tc5 > session > IntervalSnapshotManager


1 /*
2  * JBoss, the OpenSource WebOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.web.tomcat.tc5.session;
8
9 import java.util.HashMap JavaDoc;
10 import java.util.Iterator JavaDoc;
11
12 import org.apache.catalina.Context;
13 import org.apache.catalina.Session;
14 import org.jboss.logging.Logger;
15
16 /**
17  * A snapshot manager that collects all modified sessions over a given
18  * period of time and distributes them en bloc.
19  *
20  * @author Thomas Peuss <jboss@peuss.de>
21  * @version $Revision: 1.4.2.2 $
22  */

23 public class IntervalSnapshotManager extends SnapshotManager implements Runnable JavaDoc
24 {
25    static Logger log = Logger.getLogger(IntervalSnapshotManager.class);
26
27    // the interval in ms
28
protected int interval = 1000;
29
30    // the modified sessions
31
protected HashMap JavaDoc sessions = new HashMap JavaDoc();
32
33    // the distribute thread
34
protected Thread JavaDoc thread = null;
35
36    // has the thread finished?
37
protected boolean threadDone = false;
38
39    public IntervalSnapshotManager(AbstractJBossManager manager, String JavaDoc path)
40    {
41       super(manager, path);
42    }
43
44    public IntervalSnapshotManager(AbstractJBossManager manager, String JavaDoc path, int interval)
45    {
46       super(manager, path);
47       this.interval = interval;
48    }
49
50    /**
51     * Store the modified session in a hashmap for the distributor thread
52     */

53    public void snapshot(String JavaDoc id)
54    {
55       try
56       {
57          Session session = (Session) manager.findSession(id);
58          synchronized (sessions)
59          {
60             sessions.put(id, session);
61          }
62       }
63       catch (Exception JavaDoc e)
64       {
65          log.warn("Failed to replicate sessionID:" + id, e);
66       }
67    }
68
69    /**
70     * Distribute all modified sessions
71     */

72    protected void processSessions()
73    {
74       HashMap JavaDoc copy = new HashMap JavaDoc(sessions.size());
75
76       synchronized (sessions)
77       {
78          copy.putAll(sessions);
79          sessions.clear();
80       }
81       Iterator JavaDoc iter = copy.values().iterator();
82
83       // distribute all modified sessions using default replication type
84
while (iter.hasNext())
85       {
86          Session session = (Session) iter.next();
87          manager.storeSession(session);
88       }
89       copy.clear();
90    }
91
92    /**
93     * Start the snapshot manager
94     */

95    public void start()
96    {
97       startThread();
98    }
99
100    /**
101     * Stop the snapshot manager
102     */

103    public void stop()
104    {
105       stopThread();
106       synchronized (sessions)
107       {
108          sessions.clear();
109       }
110    }
111
112    /**
113     * Start the distributor thread
114     */

115    protected void startThread()
116    {
117       if (thread != null)
118       {
119          return;
120       }
121
122       thread = new Thread JavaDoc(this, "ClusteredSessionDistributor[" + contextPath + "]");
123       thread.setDaemon(true);
124       thread.setContextClassLoader(manager.getContainer().getLoader().getClassLoader());
125       threadDone = false;
126       thread.start();
127    }
128
129    /**
130     * Stop the distributor thread
131     */

132    protected void stopThread()
133    {
134       if (thread == null)
135       {
136          return;
137       }
138       threadDone = true;
139       thread.interrupt();
140       try
141       {
142          thread.join();
143       }
144       catch (InterruptedException JavaDoc e)
145       {
146       }
147       thread = null;
148    }
149
150    /**
151     * Little Thread - sleep awhile...
152     */

153    protected void threadSleep()
154    {
155       try
156       {
157          Thread.sleep(interval);
158       }
159       catch (InterruptedException JavaDoc e)
160       {
161       }
162    }
163
164    /**
165     * Thread-loop
166     */

167    public void run()
168    {
169       while (!threadDone)
170       {
171          threadSleep();
172          processSessions();
173       }
174    }
175 }
176
Popular Tags