KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > request > RequestService


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.request;
15
16 import javax.management.*;
17 import org.jboss.system.*;
18
19 /**
20  * Request Sevive
21  *
22  * @author Jorg Janke
23  * @version $Id: RequestService.java,v 1.4 2003/09/29 01:29:58 jjanke Exp $
24  */

25 public class RequestService extends ServiceMBeanSupport
26    implements RequestServiceMBean, Runnable JavaDoc
27 {
28     /**
29      * Request Service
30      */

31     public RequestService()
32     {
33         super();
34     } // RequestService
35

36     /**
37      * Get Name
38      * @return Name
39      */

40     public String JavaDoc getName()
41     {
42         return NAME;
43     } // getName
44

45     /** Worker Thread */
46     private Thread JavaDoc m_worker = null;
47     /** Array of Processors */
48     private RequestProcessorVO[] m_rpVO = null;
49
50     /** SleepMinutes */
51     private int m_sleepMinutes = 10;
52     /** Number of Invocations */
53     private volatile int m_count = 0;
54     /** DataSource Name */
55     private String JavaDoc m_dataSourceName;
56
57
58     /*************************************************************************/
59
60     /**
61      * Create Service
62      * @throws Exception
63      */

64     protected void createService() throws Exception JavaDoc
65     {
66         m_worker = new Thread JavaDoc (this, getName());
67         m_worker.setDaemon(true);
68         m_worker.setPriority(Thread.MIN_PRIORITY);
69         m_rpVO = RequestProcessorVO.get();
70     } // createService
71

72     /**
73      * Start Service
74      * @throws Exception
75      */

76     protected void startService() throws Exception JavaDoc
77     {
78         m_worker.start();
79     } // startService
80

81     /**
82      * Stop Service
83      * @throws Exception
84      */

85     protected void stopService() throws Exception JavaDoc
86     {
87         m_worker.interrupt();
88     } // stopService
89

90     /**
91      * Destroy Service
92      * @throws Exception
93      */

94     protected void destroyService() throws Exception JavaDoc
95     {
96         int maxTime = 10000;
97         int waitTime = 1000; // 1 sec
98
while (m_worker.isAlive())
99         {
100             m_worker.interrupt();
101             if (maxTime <= 0)
102                 break;
103             log.debug("Waiting for Worker");
104             Thread.sleep(waitTime);
105             maxTime -= waitTime;
106         }
107         m_worker = null;
108         m_rpVO = null;
109     } // destroyService
110

111
112     /**
113      * Run
114      */

115     public void run()
116     {
117         try
118         {
119             // wait server startup
120
Thread.sleep(60000); // 60 sec
121
}
122         catch (InterruptedException JavaDoc ex)
123         {
124             log.info ("run: " + ex.getMessage());
125             return;
126         }
127
128         while (!m_worker.isInterrupted())
129         {
130             try
131             {
132                 for (int i = 0; i < m_rpVO.length; i++)
133                 {
134                     RequestProcessor rp = new RequestProcessor (m_rpVO[i]);
135                     if (m_worker.isInterrupted())
136                         break;
137                 }
138                 if (!m_worker.isInterrupted())
139                 {
140                     m_count++;
141                     // put the thread to sleep for the interval specified
142
// Thread.currentThread().sleep(m_sleepMinutes * 60000);
143
Thread.sleep (m_sleepMinutes * 60000);
144                 }
145             }
146             catch (InterruptedException JavaDoc e)
147             {
148                 log.info ("run: " + e.getMessage());
149                 return;
150             }
151         } // while running
152
} // run
153

154     /*************************************************************************/
155
156     /**
157      * Set Sleep Minutes
158      * @param sleepMinutes sleep time in minutes
159      */

160     public void setSleepMinutes (int sleepMinutes)
161     {
162         m_sleepMinutes = sleepMinutes;
163     } // setSleepMinutes
164

165     /**
166      * Get Sleep Minutes
167      * @return sleep minutes
168      */

169     public int getSleepMinutes ()
170     {
171         return m_sleepMinutes;
172     } // getSleepMinutes
173

174     /**
175      * Get Statistics
176      * @return statistical info
177      */

178     public String JavaDoc getStatistics()
179     {
180         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
181         // Processor
182
sb.append("ProcessorCount=");
183         if (m_rpVO == null)
184             sb.append("null");
185         else
186             sb.append(m_rpVO.length);
187         sb.append(",RunCount=").append(m_count);
188         // Worker
189
sb.append(";Worker=");
190         if (m_worker == null)
191             sb.append("null");
192         else if (!m_worker.isAlive())
193             sb.append("NotAlive");
194         else if (m_worker.isInterrupted())
195             sb.append("Interrupted");
196         else
197             sb.append("Alive");
198         //
199
return sb.toString();
200     } // getStatistics
201

202     /**
203      * Get DataSource Name
204      * @return DataSource Name
205      */

206     public String JavaDoc getDataSourceName()
207     {
208         return m_dataSourceName;
209     } // getDataSourceName
210

211     /**
212      * Set DataSource Name
213      * @param dataSourceName DataSource Name
214      */

215     public void setDataSourceName (String JavaDoc dataSourceName)
216     {
217         m_dataSourceName = dataSourceName;
218     } // setDataSourceName
219

220     /*************************************************************************/
221
222     /**
223      * Run Now
224      */

225     public void runNow()
226     {
227         for (int i = 0; i < m_rpVO.length; i++)
228         {
229             RequestProcessor rp = new RequestProcessor (m_rpVO[i]);
230         }
231     } // runNow
232

233 } // RequestService
234
Popular Tags