KickJava   Java API By Example, From Geeks To Geeks.

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


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-2001 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 org.compiere.util.*;
20
21 /**
22  * Posting Controller
23  * Without setting parameters, one worker is created per Client
24  *
25  * @author Jorg Janke
26  * @version $Id: AcctServer.java,v 1.10 2003/10/04 03:57:43 jjanke Exp $
27  */

28 public class AcctServer extends Thread JavaDoc
29 {
30     /**
31      * Post Immediate
32      * @param AD_Table_ID Table ID of Document
33      * @param AD_Client_ID Client ID of Document
34      * @param Record_ID Record ID of this document
35      * @param force force posting
36      * @return true if success
37      */

38     public static boolean postImmediate (int AD_Table_ID, int AD_Client_ID, int Record_ID, boolean force)
39     {
40         return Doc.post(AD_Table_ID, AD_Client_ID, Record_ID, force);
41     } // postImmediate
42

43     /** Logger */
44     private static Logger log = Logger.getCLogger(AcctServer.class);
45
46     /*************************************************************************/
47
48     /**
49      * Constructor
50      * @param workerNo Worker number
51      * @param sleepMinutes Minutes to sleep after empty batch
52      * @param maxSleepMinutes Max Minutes to sleep after empty batch
53      * @param batchSize Number of rows to retrieve per query
54      * @param queue Work Queue
55      * @param runOnce Run just once
56      */

57     public AcctServer (int workerNo, int sleepMinutes, int maxSleepMinutes,
58         int batchSize, AcctServerWork[] queue, boolean runOnce)
59     {
60         super("AcctServer_" + workerNo);
61         m_workerNo = workerNo;
62         m_sleepMinutes = sleepMinutes;
63         m_maxSleepMinutes = maxSleepMinutes;
64         m_batchSize = batchSize;
65         m_queue = queue;
66         m_runOnce = runOnce;
67     } // AcctServerWorker
68

69     /** Worker Number */
70     private int m_workerNo;
71     /** Sleeping time in minutes */
72     private int m_sleepMinutes;
73     /** Sleeping time in minutes */
74     private int m_maxSleepMinutes;
75     /** Batch Size per query */
76     private int m_batchSize;
77     /** Work Queue */
78     private AcctServerWork[] m_queue;
79     private boolean m_runOnce;
80
81
82     /**
83      * Run - Post documents.
84      * <pre>
85      * while not interrupted
86      * initWorkQueue
87      * while workQueue
88      * postQueue
89      * sleep, if there were no Trx
90      * </pre>
91      */

92     public void run()
93     {
94         try
95         {
96             // wait server startup
97
Thread.sleep(60000); // 60 sec
98
}
99         catch (InterruptedException JavaDoc ex)
100         {
101             log.info("run_" + m_workerNo + ": " + ex.getMessage());
102             return;
103         }
104
105         int noTrx = 0;
106         int noSleeps = 0;
107
108         // Outer Loop
109
while (!isInterrupted())
110         {
111             log.info("run_" + m_workerNo);
112             // Work on Queue
113
noTrx = 0;
114             for (int i = 0; !isInterrupted() && i < m_queue.length; i++)
115             {
116                 // The document to post
117
Doc doc = Doc.get(m_queue[i].get_AD_Table_ID(), m_queue[i].get_AD_Client_ID());
118                 if (doc == null)
119                     continue;
120                 m_queue[i].addRun();
121                 // Select id FROM table
122
StringBuffer JavaDoc sql = new StringBuffer JavaDoc ("SELECT ");
123                 sql.append(doc.getTableName()).append("_ID")
124                     .append(" FROM ").append(doc.getTableName())
125                     .append(" WHERE AD_Client_ID=?")
126                     .append(" AND Processed='Y' AND Posted='N'");
127                 if (m_batchSize > 0)
128                     sql.append(" AND ROWNUM <= ").append(m_batchSize);
129                 sql.append(" ORDER BY Created");
130                 //
131
try
132                 {
133                     PreparedStatement pstmt = DB.prepareStatement(sql.toString());
134                     pstmt.setInt(1, m_queue[i].get_AD_Client_ID());
135                     ResultSet rs = pstmt.executeQuery();
136                     while (!isInterrupted() && rs.next())
137                     {
138                         m_queue[i].addPost (doc.post(rs.getInt(1), false)); // no force
139
}
140                     rs.close();
141                     pstmt.close();
142                 }
143                 catch (SQLException e)
144                 {
145                     log.error("run_" + m_workerNo, e);
146                 }
147                 if (m_queue[i].getTrxInRun() > 0)
148                     log.info("run_" + m_workerNo + " "
149                         + m_queue[i].toString());
150                 noTrx += m_queue[i].getTrxInRun();
151             } // work on queue
152

153             if (isInterrupted())
154                 break;
155
156             // Create Automatic Matching
157
new Matcher(m_queue[0].get_AD_Client_ID()).match ();
158
159             if (isInterrupted() || m_runOnce)
160                 break;
161
162             // sleep a bit if there was nothing to do
163
if (noTrx == 0)
164             {
165                 m_queue[0].addSleep();
166                 if (noSleeps++ > 0 && m_sleepMinutes < m_maxSleepMinutes)
167                     m_sleepMinutes++;
168                 log.debug("run_" + m_workerNo + " - sleeping - min=" + m_sleepMinutes);
169                 try
170                 {
171                     for (int i = 0; i < m_sleepMinutes * 6; i++)
172                         sleep (10000); // 10 sec
173
if (isInterrupted())
174                         break;
175                 }
176                 catch (Exception JavaDoc e)
177                 {
178                     log.info ("run_" + m_workerNo + ": " + e.getMessage());
179                     return;
180                 }
181             }
182             else
183                 noSleeps = 0;
184         } // outer loop
185
log.info ("run_" + m_workerNo + " - done ");
186     } // run
187

188 } // AcctServer
189
Popular Tags