KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.web.tomcat.tc6.session;
23
24 import java.util.LinkedHashSet JavaDoc;
25 import java.util.Set JavaDoc;
26
27 import org.jboss.logging.Logger;
28
29 /**
30  * A snapshot manager that collects all modified sessions over a given
31  * period of time and distributes them en bloc.
32  *
33  * @author Thomas Peuss <jboss@peuss.de>
34  * @author Brian Stansberry
35  * @version $Revision: 56542 $
36  */

37 public class IntervalSnapshotManager extends SnapshotManager implements Runnable JavaDoc
38 {
39    static Logger log = Logger.getLogger(IntervalSnapshotManager.class);
40
41    // the interval in ms
42
private int interval = 1000;
43
44    // the modified sessions
45
private Set JavaDoc sessions = new LinkedHashSet JavaDoc();
46
47    // the distribute thread
48
private Thread JavaDoc thread = null;
49
50    // Is session processing allowed?
51
private boolean processingAllowed = false;
52    
53    // has the thread finished?
54
private boolean threadDone = false;
55
56    public IntervalSnapshotManager(AbstractJBossManager manager, String JavaDoc path)
57    {
58       super(manager, path);
59    }
60
61    public IntervalSnapshotManager(AbstractJBossManager manager, String JavaDoc path, int interval)
62    {
63       super(manager, path);
64       this.interval = interval;
65    }
66
67    /**
68     * Store the modified session in a hashmap for the distributor thread
69     */

70    public void snapshot(ClusteredSession session)
71    {
72       try
73       {
74          // Don't hold a ref to the session for a long time
75
synchronized (sessions)
76          {
77             sessions.add(session);
78          }
79       }
80       catch (Exception JavaDoc e)
81       {
82          log.error("Failed to queue session " + session + " for replication", e);
83       }
84    }
85
86    /**
87     * Distribute all modified sessions
88     */

89    protected void processSessions()
90    {
91       ClusteredSession[] toProcess = null;
92       synchronized (sessions)
93       {
94          toProcess = new ClusteredSession[sessions.size()];
95          toProcess = (ClusteredSession[]) sessions.toArray(toProcess);
96          sessions.clear();
97       }
98
99       AbstractJBossManager mgr = getManager();
100       for (int i = 0; i < toProcess.length; i++)
101       {
102          // Confirm we haven't been stopped
103
if (!processingAllowed)
104             break;
105          
106          try
107          {
108             mgr.storeSession(toProcess[i]);
109          }
110          catch (Exception JavaDoc e)
111          {
112             getLog().error("Caught exception processing session " + toProcess[i].getRealId(), e);
113          }
114       }
115    }
116
117    /**
118     * Start the snapshot manager
119     */

120    public void start()
121    {
122       processingAllowed = true;
123       startThread();
124    }
125
126    /**
127     * Stop the snapshot manager
128     */

129    public void stop()
130    {
131       processingAllowed = false;
132       stopThread();
133       synchronized (sessions)
134       {
135          sessions.clear();
136       }
137    }
138
139    /**
140     * Start the distributor thread
141     */

142    protected void startThread()
143    {
144       if (thread != null)
145       {
146          return;
147       }
148
149       thread = new Thread JavaDoc(this, "ClusteredSessionDistributor[" + getContextPath() + "]");
150       thread.setDaemon(true);
151       thread.setContextClassLoader(getManager().getContainer().getLoader().getClassLoader());
152       threadDone = false;
153       thread.start();
154    }
155
156    /**
157     * Stop the distributor thread
158     */

159    protected void stopThread()
160    {
161       if (thread == null)
162       {
163          return;
164       }
165       threadDone = true;
166       thread.interrupt();
167       try
168       {
169          thread.join();
170       }
171       catch (InterruptedException JavaDoc e)
172       {
173       }
174       thread = null;
175    }
176
177    /**
178     * Thread-loop
179     */

180    public void run()
181    {
182       while (!threadDone)
183       {
184          try
185          {
186             Thread.sleep(interval);
187             processSessions();
188          }
189          catch (InterruptedException JavaDoc ie)
190          {
191             if (!threadDone)
192                getLog().error("Caught exception processing sessions", ie);
193          }
194          catch (Exception JavaDoc e)
195          {
196             getLog().error("Caught exception processing sessions", e);
197          }
198       }
199    }
200 }
201
Popular Tags