001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)WorkManager.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.binding.jms.framework;
030:
031: import java.util.Hashtable;
032:
033: import java.util.logging.Logger;
034:
035: /**
036: * This class manages the work requests and dispatches the requests to be
037: * executed in a free thread.
038: *
039: * @author Sun Microsystems, Inc.
040: */
041: public final class WorkManager {
042: /**
043: * Handle to the list of Work Manager instances.
044: */
045: private static Hashtable sWorkManagerBucket = new Hashtable();
046:
047: /**
048: * Internal handle to the logger instance.
049: */
050: private static Logger sLog;
051:
052: /**
053: * Default value for max threads.
054: */
055: private static final int DEFAULT_MAX_THREADS = 10;
056:
057: /**
058: * Default value for min threads.
059: */
060: private static final int DEFAULT_MIN_THREADS = 2;
061:
062: /**
063: * Handle to store the Work Manager state. Valid values are "INIT", "READY"
064: * and "STOP".
065: */
066: private String mState;
067:
068: /**
069: * Handle to the WorkThreadPool instance.
070: */
071: private WorkThreadPool mThreadPool;
072:
073: /**
074: * Value for max threads.
075: */
076: private int mMaxNumberOfThreads = DEFAULT_MAX_THREADS;
077:
078: /**
079: * Value for min threads.
080: */
081: private int mMinNumberOfThreads = DEFAULT_MIN_THREADS;
082:
083: /**
084: * Creates a new instance of WorkManager.
085: */
086: private WorkManager() {
087: mState = "PARKED";
088: init();
089: }
090:
091: /**
092: * Sets the log file.
093: *
094: * @param logFile log file.
095: */
096: public void setLogger(String logFile) {
097: sLog = sLog.getLogger(logFile);
098: }
099:
100: /**
101: * This method can be used to set the maximum threads in the Pool.
102: *
103: * @param count maximum threads.
104: */
105: public void setMaxThreads(int count) {
106: mMaxNumberOfThreads = count;
107: }
108:
109: /**
110: * Sets the minimu threads in the Thread pool.
111: *
112: * @param count minimum thread.
113: */
114: public void setMinThreads(int count) {
115: mMinNumberOfThreads = count;
116: }
117:
118: /**
119: * Returns a handle to the Work Manager instance for a unique service .
120: *
121: * @param name name of the work manager.
122: *
123: * @return a work manager instance.
124: */
125: public static WorkManager getWorkManager(String name) {
126: WorkManager manager = null;
127:
128: String serviceName = name;
129:
130: if (serviceName != null) {
131: manager = (WorkManager) sWorkManagerBucket.get(serviceName);
132:
133: if (manager == null) {
134: manager = new WorkManager();
135:
136: sWorkManagerBucket.put(serviceName, manager);
137: }
138: } else {
139: sLog = Logger
140: .getLogger("com.sun.jbi.binding.jms.framework");
141: sLog.severe("service name is null Cannot get work manager");
142: }
143:
144: return manager;
145: }
146:
147: /**
148: * Returns the number of wokring threads in the pool.
149: *
150: * @return count of number of busy threads
151: */
152: public int getBusyThreads() {
153: return mThreadPool.getBusyThreads();
154: }
155:
156: /**
157: * Cleans up the workmanager. It notifies the workthread pool to shutdown
158: * all its threads.
159: */
160: public void cease() {
161: // Stop the thread pool.
162: if (mState.equals("INIT") || mState.equals("STARTED")) {
163: try {
164: mThreadPool.stop();
165: } catch (Exception e) {
166: e.printStackTrace();
167: }
168: }
169:
170: setState("STOPPED");
171: }
172:
173: /**
174: *
175: */
176: public void finishWork() {
177: mThreadPool.exitWhenBusyThreadsDone();
178: }
179:
180: /**
181: * Initializes the Work Manager.
182: */
183: public void init() {
184: if (mState.equals("INIT")) {
185: return;
186: }
187:
188: sLog = Logger.getLogger(this .getClass().getPackage().getName());
189: sWorkManagerBucket = new Hashtable();
190: mThreadPool = new WorkThreadPool();
191: mThreadPool.setMinThreads(mMinNumberOfThreads);
192: mThreadPool.setMaxThreads(mMaxNumberOfThreads);
193: mThreadPool.init();
194: setState("INIT");
195: }
196:
197: /**
198: * Process the Command in a different thread. The method places the command
199: * in its internal cache and returns control to the invoking thread.
200: *
201: * @param command - command to be processed.
202: *
203: * @return true if a free thread is available, false otherwise
204: */
205: public boolean processCommand(Command command) {
206: WorkThread workerThread;
207: workerThread = mThreadPool.getFreeThread();
208:
209: boolean status = false;
210:
211: if (workerThread != null) {
212: sLog.info("WorkManager passing request to worker thread");
213: workerThread.setCommand(command);
214: status = true;
215: } else {
216: // Worker thread pool has been instructed to cleanup
217: sLog.info("Could not obtain free thread");
218: }
219:
220: return status;
221: }
222:
223: /**
224: * Starts the Work Manager.
225: */
226: public void start() {
227: try {
228: mThreadPool.start();
229: } catch (Exception e) {
230: sLog.severe("Cannot start thread pool " + e.getMessage());
231: }
232:
233: setState("STARTED");
234: }
235:
236: /**
237: * Sets the state of the work manager.
238: *
239: * @param state - state of the work manager.
240: */
241: protected void setState(String state) {
242: mState = state;
243: }
244: }
|