1 16 package com.sun.slamd.example; 17 18 19 20 import netscape.ldap.*; 21 22 23 24 33 public class LatencyCheckMasterThread 34 extends Thread 35 { 36 boolean active; 39 40 boolean stopCompleted; 42 43 boolean stopRequested; 45 46 int latencyDelay; 48 49 LatencyCheckMasterThread masterThread; 51 52 LDAPConnection connection; 54 55 long lastModTime; 57 58 ReplicaLatencyCheckJobClass jobThread; 60 61 String attributeName; 63 64 String entryDN; 66 67 68 86 public LatencyCheckMasterThread(ReplicaLatencyCheckJobClass jobThread, 87 String masterHost, int masterPort, 88 String bindDN, String bindPW, String entryDN, 89 String attributeName, int latencyDelay) 90 throws LDAPException 91 { 92 setName("Latency Check Master Thread"); 93 94 this.jobThread = jobThread; 96 this.entryDN = entryDN; 97 this.latencyDelay = latencyDelay; 98 this.attributeName = attributeName; 99 stopCompleted = false; 100 stopRequested = false; 101 lastModTime = 0; 102 masterThread = null; 103 104 connection = new LDAPConnection(); 106 connection.connect(3, masterHost, masterPort, bindDN, bindPW); 107 } 108 109 110 111 115 public void startChecking() 116 { 117 active = true; 118 } 119 120 121 122 125 public void run() 126 { 127 masterThread = this; 128 129 while ((! active) && (! stopRequested)) 131 { 132 try 133 { 134 Thread.sleep(10); 135 } catch (InterruptedException ie) {} 136 } 137 138 while (! stopRequested) 140 { 141 LDAPAttribute attr = new LDAPAttribute(attributeName, 143 String.valueOf(lastModTime)); 144 LDAPModification mod = 145 new LDAPModification(LDAPModification.REPLACE, attr); 146 147 try 148 { 149 synchronized (jobThread.latencyCheckMutex) 150 { 151 connection.modify(entryDN, mod); 152 jobThread.latencyTime.startTimer(); 153 lastModTime = System.currentTimeMillis(); 154 155 try 156 { 157 jobThread.latencyCheckMutex.wait(); 158 long modStopTime = System.currentTimeMillis(); 159 int latencySeconds = ((int) (modStopTime - lastModTime)) / 1000; 160 jobThread.latencyCategories.increment(latencySeconds + "-" + 161 (latencySeconds+1) + " s"); 162 } catch (InterruptedException ie) {} 163 } 164 } 165 catch (LDAPException le) 166 { 167 jobThread.writeVerbose("Unable to modify replica entry " + entryDN + 168 ": " + le); 169 break; 170 } 171 172 if (! stopRequested) 174 { 175 long now = System.currentTimeMillis(); 176 long sleepTime = latencyDelay - (now - lastModTime); 177 if (sleepTime > 0) 178 { 179 try 180 { 181 Thread.sleep(sleepTime); 182 } 183 catch (InterruptedException ie) {} 184 } 185 } 186 } 187 188 stopCompleted = true; 190 masterThread = null; 191 } 192 193 194 195 199 public void stopAndWait() 200 { 201 if (stopCompleted) 202 { 203 return; 204 } 205 206 stopRequested = true; 207 208 try 209 { 210 if (masterThread != null) 211 { 212 masterThread.interrupt(); 213 } 214 } catch (Exception e) {} 215 216 while (! stopCompleted) 217 { 218 try 219 { 220 Thread.sleep(10); 221 } catch (InterruptedException ie) {} 222 } 223 224 try 225 { 226 connection.disconnect(); 227 } catch (LDAPException le) {} 228 } 229 } 230 231 | Popular Tags |