001: /*
002: * $Id: ChainedThreadingProfile.java 10489 2008-01-23 17:53:38Z dfeist $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.config;
012:
013: import org.mule.api.config.ThreadingProfile;
014: import org.mule.api.context.WorkManager;
015:
016: import edu.emory.mathcs.backport.java.util.concurrent.RejectedExecutionHandler;
017: import edu.emory.mathcs.backport.java.util.concurrent.ThreadFactory;
018: import edu.emory.mathcs.backport.java.util.concurrent.ThreadPoolExecutor;
019:
020: /**
021: * This was written (perhaps too far in advance) with an eye to how we will manage default values
022: * in a dynamic environment. Since very little has been decided in that direction the correct
023: * behaviour is unclear - changing the default value of "dyanamic"
024: * {@link org.mule.config.ChainedThreadingProfile#ChainedThreadingProfile(ThreadingProfile)}
025: * will switch behaviour between dynamic and static chaining.
026: *
027: * <p>Note that within Spring, as far as I understand things, object creation is always ordered
028: * so that dependencies are correctly resolved. In that case, in a static scenario (or in a
029: * dynamic one that rebuilds the instances) dyanmic and static behaviour should be identical.</p>
030: *
031: * <p>Also, the "lazy" chaining is an optimisation - all hierarchies should be grounded in a final
032: * default which is {@link ImmutableThreadingProfile} and, as such, return reliable
033: * values (lazy would be necessary if this is not the case, since we must avoid evaluating
034: * incomplete delegates).</p>
035: */
036: public class ChainedThreadingProfile implements ThreadingProfile {
037:
038: private Integer maxThreadsActive;
039: private Integer maxThreadsIdle;
040: private Integer maxBufferSize;
041: private Long threadTTL;
042: private Long threadWaitTimeout;
043: private Integer poolExhaustedAction;
044: private Boolean doThreading;
045:
046: private WorkManagerFactory workManagerFactory = new ImmutableThreadingProfile.DefaultWorkManagerFactory();
047: private RejectedExecutionHandler rejectedExecutionHandler;
048: private ThreadFactory threadFactory;
049:
050: private ThreadingProfile delegate;
051:
052: /**
053: * Generate a mutable threading profile with fixed default values taken from
054: * {@link #DEFAULT_THREADING_PROFILE}
055: */
056: public ChainedThreadingProfile() {
057: // the default is itself constant, so dynanmic=true irrelevant
058: this (DEFAULT_THREADING_PROFILE);
059: }
060:
061: /**
062: * Generate a mutable threading profile with dynamic default values taken from the
063: * given delegate.
064: *
065: * @param delegate
066: */
067: public ChainedThreadingProfile(ThreadingProfile delegate) {
068: this (delegate, true);
069: }
070:
071: /**
072: * Generate a mutable threading profile. Default values are taken from the "delegate"
073: * argument. If dynamic is true then changes in the delegate instance are reflected in
074: * this instance.
075: *
076: * @param delegate Source of default values.
077: * @param dynamic If true, changes in delegate are reflected in this instance
078: */
079: public ChainedThreadingProfile(ThreadingProfile delegate,
080: boolean dynamic) {
081: if (!dynamic) {
082: // for static dependencies, we delegate to a fixed copy
083: delegate = new ImmutableThreadingProfile(delegate);
084: }
085: this .delegate = delegate;
086: }
087:
088: public int getMaxThreadsActive() {
089: return null != maxThreadsActive ? maxThreadsActive.intValue()
090: : delegate.getMaxThreadsActive();
091: }
092:
093: public int getMaxThreadsIdle() {
094: return null != maxThreadsIdle ? maxThreadsIdle.intValue()
095: : delegate.getMaxThreadsIdle();
096: }
097:
098: public long getThreadTTL() {
099: return null != threadTTL ? threadTTL.longValue() : delegate
100: .getThreadTTL();
101: }
102:
103: public long getThreadWaitTimeout() {
104: return null != threadWaitTimeout ? threadWaitTimeout
105: .longValue() : delegate.getThreadWaitTimeout();
106: }
107:
108: public int getPoolExhaustedAction() {
109: return null != poolExhaustedAction ? poolExhaustedAction
110: .intValue() : delegate.getPoolExhaustedAction();
111: }
112:
113: public RejectedExecutionHandler getRejectedExecutionHandler() {
114: return rejectedExecutionHandler;
115: }
116:
117: public ThreadFactory getThreadFactory() {
118: return threadFactory;
119: }
120:
121: public void setMaxThreadsActive(int maxThreadsActive) {
122: this .maxThreadsActive = new Integer(maxThreadsActive);
123: }
124:
125: public void setMaxThreadsIdle(int maxThreadsIdle) {
126: this .maxThreadsIdle = new Integer(maxThreadsIdle);
127: }
128:
129: public void setThreadTTL(long threadTTL) {
130: this .threadTTL = new Long(threadTTL);
131: }
132:
133: public void setThreadWaitTimeout(long threadWaitTimeout) {
134: this .threadWaitTimeout = new Long(threadWaitTimeout);
135: }
136:
137: public void setPoolExhaustedAction(int poolExhaustPolicy) {
138: this .poolExhaustedAction = new Integer(poolExhaustPolicy);
139: }
140:
141: public void setRejectedExecutionHandler(
142: RejectedExecutionHandler rejectedExecutionHandler) {
143: this .rejectedExecutionHandler = rejectedExecutionHandler;
144: }
145:
146: public void setThreadFactory(ThreadFactory threadFactory) {
147: this .threadFactory = threadFactory;
148: }
149:
150: public int getMaxBufferSize() {
151: return null != maxBufferSize ? maxBufferSize.intValue()
152: : delegate.getMaxBufferSize();
153: }
154:
155: public void setMaxBufferSize(int maxBufferSize) {
156: this .maxBufferSize = new Integer(maxBufferSize);
157: }
158:
159: public WorkManagerFactory getWorkManagerFactory() {
160: return workManagerFactory;
161: }
162:
163: public void setWorkManagerFactory(
164: WorkManagerFactory workManagerFactory) {
165: this .workManagerFactory = workManagerFactory;
166: }
167:
168: public WorkManager createWorkManager(String name) {
169: return workManagerFactory.createWorkManager(this , name);
170: }
171:
172: public ThreadPoolExecutor createPool() {
173: return createPool(null);
174: }
175:
176: public ThreadPoolExecutor createPool(String name) {
177: return ImmutableThreadingProfile.createPool(name, this );
178: }
179:
180: public boolean isDoThreading() {
181: return null != doThreading ? doThreading.booleanValue()
182: : delegate.isDoThreading();
183: }
184:
185: public void setDoThreading(boolean doThreading) {
186: this .doThreading = Boolean.valueOf(doThreading);
187: }
188:
189: public String toString() {
190: return "ThreadingProfile{" + "maxThreadsActive="
191: + maxThreadsActive + ", maxThreadsIdle="
192: + maxThreadsIdle + ", maxBufferSize=" + maxBufferSize
193: + ", threadTTL=" + threadTTL + ", poolExhaustedAction="
194: + poolExhaustedAction + ", threadWaitTimeout="
195: + threadWaitTimeout + ", doThreading=" + doThreading
196: + ", workManagerFactory=" + workManagerFactory
197: + ", rejectedExecutionHandler="
198: + rejectedExecutionHandler + ", threadFactory="
199: + threadFactory + "}";
200: }
201:
202: }
|