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.engine.sequencing.framework.threads;
030:
031: import java.util.Hashtable;
032: import java.util.logging.Logger;
033:
034: /**
035: * This class manages the work requests and dispatches the requests to be
036: * executed in a free thread.
037: *
038: * @author Sun Microsystems, Inc.
039: */
040: public final class WorkManager {
041: /**
042: * Handle to the list of Work Manager instances.
043: */
044: private static Hashtable sWorkManagerBucket = new Hashtable();
045:
046: /**
047: * Internal handle to the logger instance.
048: */
049: private static Logger sLog;
050:
051: /**
052: * Default value for max threads.
053: */
054: private static final int DEFAULT_MAX_THREADS = 10;
055:
056: /**
057: * Default value for min threads.
058: */
059: private static final int DEFAULT_MIN_THREADS = 2;
060:
061: /**
062: * Handle to store the Work Manager state. Valid values are "INIT", "READY"
063: * and "STOP".
064: */
065: private String mState;
066:
067: /**
068: * Handle to the WorkThreadPool instance.
069: */
070: private WorkThreadPool mThreadPool;
071:
072: /**
073: * Value for max threads.
074: */
075: private int mMaxNumberOfThreads = DEFAULT_MAX_THREADS;
076:
077: /**
078: * Value for min threads.
079: */
080: private int mMinNumberOfThreads = DEFAULT_MIN_THREADS;
081:
082: /**
083: * Creates a new instance of WorkManager.
084: */
085: private WorkManager() {
086: mState = "PARKED";
087: init();
088: }
089:
090: /**
091: * Sets the log file.
092: *
093: * @param logFile log file.
094: */
095: public void setLogger(String logFile) {
096: sLog = sLog.getLogger(logFile);
097: }
098:
099: /**
100: * This method can be used to set the maximum threads in the Pool.
101: *
102: * @param count max threads.
103: */
104: public void setMaxThreads(int count) {
105: mMaxNumberOfThreads = count;
106: }
107:
108: /**
109: * Sets the minimu threads in the Thread pool.
110: *
111: * @param count min threads.
112: */
113: public void setMinThreads(int count) {
114: mMinNumberOfThreads = count;
115: }
116:
117: /**
118: * Returns a handle to the Work Manager instance for a unique service .
119: *
120: * @param name unique name.
121: *
122: * @return a work manager instance
123: */
124: public static WorkManager getWorkManager(String name) {
125: WorkManager manager = null;
126:
127: String serviceName = name;
128:
129: if (serviceName != null) {
130: manager = (WorkManager) sWorkManagerBucket.get(serviceName);
131:
132: if (manager == null) {
133: manager = new WorkManager();
134:
135: sWorkManagerBucket.put(serviceName, manager);
136: }
137: } else {
138: sLog = Logger
139: .getLogger("com.sun.jbi.engine.sequencing.framework");
140: sLog.severe("service name is null can get work manager");
141: }
142:
143: return manager;
144: }
145:
146: /**
147: * Returns the number of wokring threads in the pool.
148: *
149: * @return count of number of busy threads
150: */
151: public int getBusyThreads() {
152: return mThreadPool.getBusyThreads();
153: }
154:
155: /**
156: * Cleans up the workmanager. It notifies the workthread pool to shutdown
157: * all its threads.
158: */
159: public void cease() {
160: // Stop the thread pool.
161: if (mState.equals("INIT") || mState.equals("STARTED")) {
162: try {
163: mThreadPool.stop();
164: } catch (Exception e) {
165: e.printStackTrace();
166: }
167: }
168:
169: setState("STOPPED");
170: }
171:
172: /**
173: *
174: */
175: public void finishWork() {
176: mThreadPool.exitWhenBusyThreadsDone();
177: }
178:
179: /**
180: * Initializes the Work Manager.
181: */
182: public void init() {
183: if (mState.equals("INIT")) {
184: return;
185: }
186:
187: sLog = Logger.getLogger(this .getClass().getPackage().getName());
188: sWorkManagerBucket = new Hashtable();
189: mThreadPool = new WorkThreadPool();
190: mThreadPool.setMinThreads(mMinNumberOfThreads);
191: mThreadPool.setMaxThreads(mMaxNumberOfThreads);
192: mThreadPool.init();
193: setState("INIT");
194: }
195:
196: /**
197: * Process the Command in a different thread. The method places the command
198: * in its internal cache and returns control to the invoking thread.
199: *
200: * @param command - command to be processed.
201: *
202: * @return true if a free thread is available, false otherwise
203: */
204: public boolean processCommand(Command command) {
205: WorkThread workerThread;
206: workerThread = mThreadPool.getFreeThread();
207:
208: boolean status = false;
209:
210: if (workerThread != null) {
211: sLog.info("WorkManager passing request to worker thread");
212: workerThread.setCommand(command);
213: status = true;
214: } else {
215: // Worker thread pool has been instructed to cleanup
216: sLog.info("Could not obtain free thread");
217: }
218:
219: return status;
220: }
221:
222: /**
223: * Starts the Work Manager.
224: */
225: public void start() {
226: try {
227: mThreadPool.start();
228: } catch (Exception e) {
229: ;
230: }
231:
232: setState("STARTED");
233: }
234:
235: /**
236: * Sets the state of the work manager.
237: *
238: * @param state - state of the work manager.
239: */
240: protected void setState(String state) {
241: mState = state;
242: }
243: }
|