1 31 32 package org.opencms.main; 33 34 import org.opencms.report.A_CmsReportThread; 35 import org.opencms.util.CmsUUID; 36 37 import java.util.HashSet ; 38 import java.util.Hashtable ; 39 import java.util.Iterator ; 40 import java.util.Map ; 41 import java.util.Set ; 42 43 import org.apache.commons.logging.Log; 44 45 64 public class CmsThreadStore extends Thread { 65 66 67 private static final Log LOG = CmsLog.getLog(CmsThreadStore.class); 68 69 70 private boolean m_alive; 71 72 73 private Map m_threads; 74 75 78 protected CmsThreadStore() { 79 80 super(new ThreadGroup ("OpenCms Thread Store"), "OpenCms: Grim Reaper"); 81 setDaemon(true); 82 m_threads = new Hashtable (); 84 m_alive = true; 85 start(); 86 } 87 88 93 public void addThread(A_CmsReportThread thread) { 94 95 m_threads.put(thread.getUUID(), thread); 96 if (LOG.isDebugEnabled()) { 97 dumpThreads(); 98 } 99 } 100 101 107 public A_CmsReportThread retrieveThread(CmsUUID key) { 108 109 if (LOG.isDebugEnabled()) { 110 dumpThreads(); 111 } 112 return (A_CmsReportThread)m_threads.get(key); 113 } 114 115 118 public void run() { 119 120 int m_minutesForSessionUpdate = 0; 121 while (m_alive) { 122 try { 124 sleep(60000); 126 } catch (InterruptedException e) { 127 } 129 try { 130 Iterator i; 131 i = m_threads.keySet().iterator(); 132 Set doomed = new HashSet (); 133 while (i.hasNext()) { 135 CmsUUID key = (CmsUUID)i.next(); 136 A_CmsReportThread thread = (A_CmsReportThread)m_threads.get(key); 137 if (thread.isDoomed()) { 138 doomed.add(key); 139 if (LOG.isDebugEnabled()) { 140 LOG.debug(Messages.get().getBundle().key( 141 Messages.LOG_THREADSTORE_DOOMED_2, 142 thread.getName(), 143 thread.getUUID())); 144 } 145 } 146 } 147 i = doomed.iterator(); 148 while (i.hasNext()) { 150 m_threads.remove(i.next()); 151 } 152 if (LOG.isDebugEnabled()) { 153 dumpThreads(); 154 } 155 } catch (Throwable t) { 156 LOG.error(Messages.get().getBundle().key(Messages.LOG_THREADSTORE_CHECK_THREADS_ERROR_0), t); 158 } 159 160 m_minutesForSessionUpdate++; 162 if (m_minutesForSessionUpdate >= 5) { 163 m_minutesForSessionUpdate = 0; 165 try { 166 CmsSessionManager sessionInfoManager = OpenCms.getSessionManager(); 167 if (sessionInfoManager != null) { 168 sessionInfoManager.validateSessionInfos(); 170 } 171 } catch (Throwable t) { 172 LOG.error(Messages.get().getBundle().key(Messages.LOG_THREADSTORE_CHECK_SESSIONS_ERROR_0), t); 173 } 174 } 175 } 176 } 177 178 181 protected synchronized void shutDown() { 182 183 m_alive = false; 184 interrupt(); 185 } 186 187 190 private void dumpThreads() { 191 192 if (LOG.isDebugEnabled()) { 193 StringBuffer b = new StringBuffer (512); 194 Iterator i = m_threads.keySet().iterator(); 195 while (i.hasNext()) { 196 CmsUUID key = (CmsUUID)i.next(); 197 A_CmsReportThread thread = (A_CmsReportThread)m_threads.get(key); 198 b.append(thread.getName()); 199 b.append(" - "); 200 b.append(thread.getUUID()); 201 b.append('\n'); 202 } 203 LOG.debug(Messages.get().getBundle().key( 204 Messages.LOG_THREADSTORE_POOL_CONTENT_2, 205 new Integer (m_threads.size()), 206 b.toString())); 207 } 208 } 209 } | Popular Tags |