1 22 package org.jboss.web.tomcat.tc6.session; 23 24 import java.util.LinkedHashSet ; 25 import java.util.Set ; 26 27 import org.jboss.logging.Logger; 28 29 37 public class IntervalSnapshotManager extends SnapshotManager implements Runnable 38 { 39 static Logger log = Logger.getLogger(IntervalSnapshotManager.class); 40 41 private int interval = 1000; 43 44 private Set sessions = new LinkedHashSet (); 46 47 private Thread thread = null; 49 50 private boolean processingAllowed = false; 52 53 private boolean threadDone = false; 55 56 public IntervalSnapshotManager(AbstractJBossManager manager, String path) 57 { 58 super(manager, path); 59 } 60 61 public IntervalSnapshotManager(AbstractJBossManager manager, String path, int interval) 62 { 63 super(manager, path); 64 this.interval = interval; 65 } 66 67 70 public void snapshot(ClusteredSession session) 71 { 72 try 73 { 74 synchronized (sessions) 76 { 77 sessions.add(session); 78 } 79 } 80 catch (Exception e) 81 { 82 log.error("Failed to queue session " + session + " for replication", e); 83 } 84 } 85 86 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 if (!processingAllowed) 104 break; 105 106 try 107 { 108 mgr.storeSession(toProcess[i]); 109 } 110 catch (Exception e) 111 { 112 getLog().error("Caught exception processing session " + toProcess[i].getRealId(), e); 113 } 114 } 115 } 116 117 120 public void start() 121 { 122 processingAllowed = true; 123 startThread(); 124 } 125 126 129 public void stop() 130 { 131 processingAllowed = false; 132 stopThread(); 133 synchronized (sessions) 134 { 135 sessions.clear(); 136 } 137 } 138 139 142 protected void startThread() 143 { 144 if (thread != null) 145 { 146 return; 147 } 148 149 thread = new Thread (this, "ClusteredSessionDistributor[" + getContextPath() + "]"); 150 thread.setDaemon(true); 151 thread.setContextClassLoader(getManager().getContainer().getLoader().getClassLoader()); 152 threadDone = false; 153 thread.start(); 154 } 155 156 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 e) 172 { 173 } 174 thread = null; 175 } 176 177 180 public void run() 181 { 182 while (!threadDone) 183 { 184 try 185 { 186 Thread.sleep(interval); 187 processSessions(); 188 } 189 catch (InterruptedException ie) 190 { 191 if (!threadDone) 192 getLog().error("Caught exception processing sessions", ie); 193 } 194 catch (Exception e) 195 { 196 getLog().error("Caught exception processing sessions", e); 197 } 198 } 199 } 200 } 201 | Popular Tags |