1 14 package org.compiere.acct; 15 16 import java.util.*; 17 import java.sql.*; 18 19 import javax.management.*; 20 import org.jboss.system.*; 21 22 import org.compiere.util.DB; 23 24 30 public class AcctService extends ServiceMBeanSupport 31 implements AcctServiceMBean 32 { 33 36 public AcctService() 37 { 38 super(); 39 } 41 45 public String getName() 46 { 47 return NAME; 48 } 50 51 private AcctServer[] m_worker = null; 52 53 54 private int m_sleepMinutes = 5; 55 56 private volatile int m_count = 0; 57 58 59 60 private int m_noWorkers = 1; 61 62 private int m_maxSleepMinutes = 10; 63 64 private int m_batchSize = 20; 65 66 private ArrayList m_clients = new ArrayList(); 67 68 private AcctServerWork[] m_totalQueue = null; 69 70 71 private String m_dataSourceName; 72 73 74 75 79 protected void createService() throws Exception  80 { 81 } 83 87 protected void startService() throws Exception  88 { 89 if (m_worker == null || m_worker.length != m_noWorkers) 90 m_worker = new AcctServer [m_noWorkers]; 91 92 if (m_clients.size() == 0) 93 addAllClients(); 94 95 for (int i = 0; i < m_worker.length; i++) 96 { 97 m_worker[i] = new AcctServer (i, m_sleepMinutes, m_maxSleepMinutes, 98 m_batchSize, createWork(i), false); 99 m_worker[i].setPriority(Thread.MIN_PRIORITY); 100 m_worker[i].setDaemon(true); 101 m_worker[i].start(); 102 } 103 } 105 109 protected void stopService() throws Exception  110 { 111 if (m_worker == null) 112 return; 113 114 try 115 { 116 for (int i = 0; i < m_worker.length; i++) 117 { 118 if (m_worker[i] != null && m_worker[i].isAlive()) 119 m_worker[i].interrupt(); 120 } 121 log.info("stopService - interrupted waiting ..."); 122 for (int i = 0; i < m_worker.length; i++) 123 { 124 m_worker[i].join (10000); m_worker[i] = null; 126 } 127 m_worker = null; 128 } 129 catch (Exception e) 130 { 131 log.error("stopService", e); 132 } 133 } 135 139 protected void destroyService() throws Exception  140 { 141 m_worker = null; 142 } 144 148 public synchronized boolean isAllAlive() 149 { 150 if (m_worker == null) 151 return false; 152 for (int i = 0; i < m_worker.length; i++) 153 { 154 if (m_worker[i] == null && !m_worker[i].isAlive()) 155 return false; 156 } 157 return true; 158 } 160 161 162 163 167 public void setSleepMinutes (int sleepMinutes) 168 { 169 m_sleepMinutes = sleepMinutes; 170 } 172 176 public int getSleepMinutes () 177 { 178 return m_sleepMinutes; 179 } 181 185 public String getStatistics() 186 { 187 StringBuffer sb = new StringBuffer (); 188 sb.append("WorkerCount=").append(m_noWorkers); 190 sb.append(",AllAlive=").append(isAllAlive()); 191 if (m_worker == null) 192 return sb.toString(); 193 194 for (int i = 0; i < m_worker.length; i++) 196 { 197 sb.append(";Worker_").append(i).append("="); 198 if (m_worker[i] == null) 199 sb.append("null"); 200 else if (!m_worker[i].isAlive()) 201 sb.append("NotAlive"); 202 else if (m_worker[i].isInterrupted()) 203 sb.append("Interrupted"); 204 else 205 sb.append("Alive"); 206 } 207 return sb.toString(); 209 } 211 215 public String getDataSourceName() 216 { 217 return m_dataSourceName; 218 } 220 224 public void setDataSourceName (String dataSourceName) 225 { 226 m_dataSourceName = dataSourceName; 227 } 229 230 231 237 public void setWorkerCount (int noWorkers) 238 { 239 m_noWorkers = noWorkers; 240 } 242 246 public int getWorkerCount() 247 { 248 return m_noWorkers; 249 } 251 252 257 public void setMaxSleepMinutes (int maxSleepMinutes) 258 { 259 m_maxSleepMinutes = maxSleepMinutes; 260 } 262 266 public int getMaxSleepMinutes() 267 { 268 return m_maxSleepMinutes; 269 } 271 276 public void setBatchSize (int batchSize) 277 { 278 m_batchSize = batchSize; 279 } 281 285 public int getBatchSize() 286 { 287 return m_batchSize; 288 } 290 294 public void addClientID (int AD_Client_ID) 295 { 296 log.info("addClient " + AD_Client_ID); 297 m_clients.add (new Integer (AD_Client_ID)); 298 } 300 303 public void addAllClients() 304 { 305 log.info("addAllClients"); 306 String sql = "SELECT AD_Client_ID FROM AD_Client WHERE IsActive='Y' AND AD_Client_ID <> 0"; 307 try 308 { 309 PreparedStatement pstmt = DB.prepareStatement(sql); 310 ResultSet rs = pstmt.executeQuery(); 311 while (rs.next()) 312 addClientID (rs.getInt(1)); 313 rs.close(); 314 pstmt.close(); 315 } 316 catch (SQLException e) 317 { 318 log.error("addAllClients", e); 319 } 320 } 322 326 public void removeClientID (int AD_Client_ID) 327 { 328 Integer c = new Integer (AD_Client_ID); 329 if (m_clients.contains(c)) 330 { 331 if (m_clients.remove(c)) 332 log.info("removeClient " + AD_Client_ID); 333 else 334 log.warn("removeClient " + AD_Client_ID + " - not removed"); 335 } 336 else 337 log.warn("removeClient " + AD_Client_ID + " - does not exist"); 338 } 340 344 public int getClientCount() 345 { 346 return m_clients.size(); 347 } 349 353 public String getClientList() 354 { 355 StringBuffer sb = new StringBuffer (); 356 for (int i = 0; i < m_clients.size(); i++) 357 { 358 if (i != 0) 359 sb.append(", "); 360 sb.append(m_clients.get(i)); 361 } 362 return sb.toString(); 363 } 365 371 private synchronized AcctServerWork[] createWork (int workerNo) 372 { 373 if (m_totalQueue == null) 375 { 376 m_totalQueue = new AcctServerWork [ m_clients.size() * Doc.documents.length ]; 377 int index = 0; 378 for (int c = 0; c < m_clients.size(); c++) 379 { 380 int AD_Client_ID = ((Integer )m_clients.get(c)).intValue(); 381 for (int t = 0; t < Doc.documents.length; t++) 382 m_totalQueue[index++] = new AcctServerWork (AD_Client_ID, Doc.documents[t]); 383 } 384 } 385 386 int no = m_totalQueue.length / m_worker.length; 387 int start = no * workerNo; 388 int end = (workerNo+1) * no; 389 if (workerNo == m_worker.length-1) end = m_totalQueue.length; 391 392 log.debug ("createWork for Worker=" + workerNo 393 + "TotalQueue=" + m_totalQueue.length + ", WorkerQueue=" + (end-start) 394 + ", Index: " + start + " - " + end); 395 396 AcctServerWork[] retValue = new AcctServerWork[end-start]; 397 int index = 0; 398 for (int i = start; i < end; i++) 399 retValue[index++] = m_totalQueue[i]; 400 return retValue; 401 } 403 406 public void runNow() 407 { 408 AcctServer wk = new AcctServer (99, m_sleepMinutes, m_maxSleepMinutes, 409 m_batchSize, m_totalQueue, true); 410 wk.setPriority(Thread.MIN_PRIORITY); 411 wk.setDaemon(true); 412 wk.start(); 413 } 415 } | Popular Tags |