1 7 package org.jboss.web.tomcat.tc5.session; 8 9 import java.util.HashMap ; 10 import java.util.Iterator ; 11 12 import org.apache.catalina.Context; 13 import org.apache.catalina.Session; 14 import org.jboss.logging.Logger; 15 16 23 public class IntervalSnapshotManager extends SnapshotManager implements Runnable 24 { 25 static Logger log = Logger.getLogger(IntervalSnapshotManager.class); 26 27 protected int interval = 1000; 29 30 protected HashMap sessions = new HashMap (); 32 33 protected Thread thread = null; 35 36 protected boolean threadDone = false; 38 39 public IntervalSnapshotManager(AbstractJBossManager manager, String path) 40 { 41 super(manager, path); 42 } 43 44 public IntervalSnapshotManager(AbstractJBossManager manager, String path, int interval) 45 { 46 super(manager, path); 47 this.interval = interval; 48 } 49 50 53 public void snapshot(String 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 e) 64 { 65 log.warn("Failed to replicate sessionID:" + id, e); 66 } 67 } 68 69 72 protected void processSessions() 73 { 74 HashMap copy = new HashMap (sessions.size()); 75 76 synchronized (sessions) 77 { 78 copy.putAll(sessions); 79 sessions.clear(); 80 } 81 Iterator iter = copy.values().iterator(); 82 83 while (iter.hasNext()) 85 { 86 Session session = (Session) iter.next(); 87 manager.storeSession(session); 88 } 89 copy.clear(); 90 } 91 92 95 public void start() 96 { 97 startThread(); 98 } 99 100 103 public void stop() 104 { 105 stopThread(); 106 synchronized (sessions) 107 { 108 sessions.clear(); 109 } 110 } 111 112 115 protected void startThread() 116 { 117 if (thread != null) 118 { 119 return; 120 } 121 122 thread = new Thread (this, "ClusteredSessionDistributor[" + contextPath + "]"); 123 thread.setDaemon(true); 124 thread.setContextClassLoader(manager.getContainer().getLoader().getClassLoader()); 125 threadDone = false; 126 thread.start(); 127 } 128 129 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 e) 145 { 146 } 147 thread = null; 148 } 149 150 153 protected void threadSleep() 154 { 155 try 156 { 157 Thread.sleep(interval); 158 } 159 catch (InterruptedException e) 160 { 161 } 162 } 163 164 167 public void run() 168 { 169 while (!threadDone) 170 { 171 threadSleep(); 172 processSessions(); 173 } 174 } 175 } 176 | Popular Tags |