KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > acct > AcctService


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2002 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

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 /**
25  * Account Service MBean
26  *
27  * @author Jorg Janke
28  * @version $Id: AcctService.java,v 1.3 2003/07/16 19:11:55 jjanke Exp $
29  */

30 public class AcctService extends ServiceMBeanSupport
31    implements AcctServiceMBean
32 {
33     /**
34      * Acct Service
35      */

36     public AcctService()
37     {
38         super();
39     } // AcctService
40

41     /**
42      * Get Name
43      * @return Name
44      */

45     public String JavaDoc getName()
46     {
47         return NAME;
48     } // getName
49

50     /** Worker Thread */
51     private AcctServer[] m_worker = null;
52
53     /** SleepMinutes */
54     private int m_sleepMinutes = 5;
55     /** Number of Invocations */
56     private volatile int m_count = 0;
57
58
59     /** Number of workers (1) */
60     private int m_noWorkers = 1;
61     /** Max Sleep time */
62     private int m_maxSleepMinutes = 10;
63     /** Batch Size per Query (20) */
64     private int m_batchSize = 20;
65     /** List of Clients */
66     private ArrayList m_clients = new ArrayList();
67     /** Total Work Queue */
68     private AcctServerWork[] m_totalQueue = null;
69
70     /** DataSource Name */
71     private String JavaDoc m_dataSourceName;
72
73     /*************************************************************************/
74
75     /**
76      * Create Service
77      * @throws Exception
78      */

79     protected void createService() throws Exception JavaDoc
80     {
81     } // createService
82

83     /**
84      * Start Service
85      * @throws Exception
86      */

87     protected void startService() throws Exception JavaDoc
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     } // startService
104

105     /**
106      * Stop Service
107      * @throws Exception
108      */

109     protected void stopService() throws Exception JavaDoc
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); // 10 sec max
125
m_worker[i] = null;
126             }
127             m_worker = null;
128         }
129         catch (Exception JavaDoc e)
130         {
131             log.error("stopService", e);
132         }
133     } // stopService
134

135     /**
136      * Destroy Service
137      * @throws Exception
138      */

139     protected void destroyService() throws Exception JavaDoc
140     {
141         m_worker = null;
142     } // destroyService
143

144     /**
145      * Are all workers Active ?
146      * @return true if All Alive
147      */

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     } // isAllActive
159

160
161     /*************************************************************************/
162
163     /**
164      * Set Sleep Minutes
165      * @param sleepMinutes sleep time in minutes
166      */

167     public void setSleepMinutes (int sleepMinutes)
168     {
169         m_sleepMinutes = sleepMinutes;
170     } // setSleepMinutes
171

172     /**
173      * Get Sleep Minutes
174      * @return sleep minutes
175      */

176     public int getSleepMinutes ()
177     {
178         return m_sleepMinutes;
179     } // getSleepMinutes
180

181     /**
182      * Get Statistics
183      * @return statistical info
184      */

185     public String JavaDoc getStatistics()
186     {
187         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
188         // Processor
189
sb.append("WorkerCount=").append(m_noWorkers);
190         sb.append(",AllAlive=").append(isAllAlive());
191         if (m_worker == null)
192             return sb.toString();
193
194         // Worker
195
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         //
208
return sb.toString();
209     } // getStatistics
210

211     /**
212      * Get DataSource Name
213      * @return DataSource Name
214      */

215     public String JavaDoc getDataSourceName()
216     {
217         return m_dataSourceName;
218     } // getDataSourceName
219

220     /**
221      * Set DataSource Name
222      * @param dataSourceName DataSource Name
223      */

224     public void setDataSourceName (String JavaDoc dataSourceName)
225     {
226         m_dataSourceName = dataSourceName;
227     } // setDataSourceName
228

229     /*************************************************************************/
230
231     /**
232      * Set Number of Workers.
233      * (default: one per client)
234      * Can only be set before first start
235      * @param noWorkers workers
236      */

237     public void setWorkerCount (int noWorkers)
238     {
239         m_noWorkers = noWorkers;
240     } // setNoWorkers
241

242     /**
243      * Get Number of Workers
244      * @return Number of Workers
245      */

246     public int getWorkerCount()
247     {
248         return m_noWorkers;
249     } // getNoWorkers
250

251
252     /**
253      * Set Max Number of minutes to sleep after each round.
254      * (default: 10)
255      * @param maxSleepMinutes minutes
256      */

257     public void setMaxSleepMinutes (int maxSleepMinutes)
258     {
259         m_maxSleepMinutes = maxSleepMinutes;
260     } // setMaxSleepMinutes
261

262     /**
263      * Get Max Sleep Minutes
264      * @return Sleep Minutes
265      */

266     public int getMaxSleepMinutes()
267     {
268         return m_maxSleepMinutes;
269     } // getSleepMinutes
270

271     /**
272      * Set Batch Size (0 = all documents).
273      * (default: 20)
274      * @param batchSize batch size
275      */

276     public void setBatchSize (int batchSize)
277     {
278         m_batchSize = batchSize;
279     } // setBatchSize
280

281     /**
282      * Get Batch Size
283      * @return Batch Size
284      */

285     public int getBatchSize()
286     {
287         return m_batchSize;
288     } // getBatchSize
289

290     /**
291      * Add Client to List of Clients
292      * @param AD_Client_ID client
293      */

294     public void addClientID (int AD_Client_ID)
295     {
296         log.info("addClient " + AD_Client_ID);
297         m_clients.add (new Integer JavaDoc(AD_Client_ID));
298     } // addClient
299

300     /**
301      * Add All Clients
302      */

303     public void addAllClients()
304     {
305         log.info("addAllClients");
306         String JavaDoc 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     } // addAllClients
321

322     /**
323      * Remove Client from List of Clients
324      * @param AD_Client_ID client
325      */

326     public void removeClientID (int AD_Client_ID)
327     {
328         Integer JavaDoc c = new Integer JavaDoc(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     } // removeClient
339

340     /**
341      * Get Client Number
342      * @return Number of Clients
343      */

344     public int getClientCount()
345     {
346         return m_clients.size();
347     } // getNoClients
348

349     /**
350      * Get Client List as String
351      * @return AD_Client_ID list
352      */

353     public String JavaDoc getClientList()
354     {
355         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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     } // getClientList
364

365     /**
366      * Create Work.
367      * Create [client,table] pairs and distribute them among workers
368      * @param workerNo worker
369      * @return AcctServerWork Array
370      */

371     private synchronized AcctServerWork[] createWork (int workerNo)
372     {
373         // Create TotalWork
374
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 JavaDoc)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) // last worker gets rest
390
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     } // createWork
402

403     /**
404      * Run Now
405      */

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     } // runNow
414

415 } // AcctService
Popular Tags