Permalink
Browse files

Extracted configuration points required for running in hosted environ…

…ment (gae)
  • Loading branch information...
1 parent b1e1b0c commit 283dd33e92b2df54fd9a4bc15fc3080e7e1fdb64 @vaclav committed Apr 24, 2012
@@ -0,0 +1,58 @@
+// GPars - Groovy Parallel Systems
+//
+// Copyright © 2008-11 The original author or authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package groovyx.gpars;
+
+import groovyx.gpars.scheduler.Pool;
+import groovyx.gpars.scheduler.ResizeablePool;
+import groovyx.gpars.util.TimerFactory;
+
+import java.util.Timer;
+
+/**
+ * Enables to specify custom thread pools and timers to run GPars in hosted environments, such as GAE
+ *
+ * @author Vaclav Pech
+ */
+public final class GParsConfig {
+ private static volatile Pool defaultPool;
+ private static volatile TimerFactory timerFactory;
+
+ public static void setDefaultPool(final Pool pool) {
+ if (pool == null) throw new IllegalArgumentException("The default pool must not be null");
+ defaultPool = pool;
+ }
+
+ public static Pool retrieveDefaultPool() {
+ if (defaultPool != null) return defaultPool;
+ return new ResizeablePool(true, 1);
+ }
+
+ public static void setTimerFactory(final TimerFactory timerFactory) {
+ if (timerFactory == null) throw new IllegalArgumentException("The TimerFactory must not be null");
+ GParsConfig.timerFactory = timerFactory;
+ }
+
+ public static TimerFactory retrieveTimerFactory() {
+ if (timerFactory != null) return timerFactory;
+ return new TimerFactory() {
+ @Override
+ public Timer createTimer(final String name, final boolean daemon) {
+ return new Timer(name, daemon);
+ }
+ };
+ }
+}
@@ -20,6 +20,7 @@ import groovy.time.Duration
import groovyx.gpars.dataflow.DataflowVariable
import groovyx.gpars.scheduler.DefaultPool
import groovyx.gpars.util.PAUtils
+
import java.util.concurrent.Callable
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.ConcurrentLinkedQueue
@@ -28,6 +29,7 @@ import java.util.concurrent.Future
import java.util.concurrent.Semaphore
import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicReference
+
import static groovyx.gpars.util.PAGroovyUtils.createCollection
import static groovyx.gpars.util.PAUtils.buildClosureForMaps
import static groovyx.gpars.util.PAUtils.buildClosureForMapsWithIndex
@@ -47,7 +49,7 @@ public class GParsExecutorsPoolUtil {
/**
* Allows timeouts for async operations
*/
- private static final Timer timer = new Timer('GParsExecutorsTimeoutTimer', true)
+ private static final Timer timer = GParsConfig.retrieveTimerFactory().createTimer('GParsExecutorsTimeoutTimer', true)
/**
* schedules the supplied closure for processing in the underlying thread pool.
@@ -67,7 +67,7 @@
/**
* Allows timeouts for async operations
*/
- private static final Timer timer = new Timer("GParsTimeoutTimer", true);
+ private static final Timer timer = GParsConfig.retrieveTimerFactory().createTimer("GParsTimeoutTimer", true);
private static ForkJoinPool retrievePool() {
final ForkJoinPool pool = (ForkJoinPool) GParsPool.retrieveCurrentPool();
@@ -19,6 +19,7 @@
import groovy.lang.MetaClass;
import groovy.lang.MetaMethod;
import groovy.time.BaseDuration;
+import groovyx.gpars.GParsConfig;
import groovyx.gpars.MessagingRunnable;
import groovyx.gpars.actor.impl.MessageStream;
import groovyx.gpars.dataflow.DataCallback;
@@ -85,7 +86,7 @@
/**
* Timer holding timeouts for react methods
*/
- protected static final Timer timer = new Timer("GPars Actor Timer", true);
+ protected static final Timer timer = GParsConfig.retrieveTimerFactory().createTimer("GPars Actor Timer", true);
protected Actor() {
this(new DataflowVariable<Object>());
@@ -17,8 +17,8 @@
package groovyx.gpars.actor;
import groovy.lang.Closure;
+import groovyx.gpars.GParsConfig;
import groovyx.gpars.group.DefaultPGroup;
-import groovyx.gpars.scheduler.ResizeablePool;
/**
* Provides handy helper methods to create pooled actors and customize the underlying thread pool.
@@ -50,7 +50,7 @@
/**
* The default actor group to share by all actors created through the Actors class.
*/
- public static final DefaultPGroup defaultActorPGroup = new DefaultPGroup(new ResizeablePool(true));
+ public static final DefaultPGroup defaultActorPGroup = new DefaultPGroup(GParsConfig.retrieveDefaultPool());
/**
* Creates a new instance of DefaultActor, using the passed-in closure as the body of the actor's act() method.
@@ -17,10 +17,10 @@
package groovyx.gpars.dataflow;
import groovy.lang.Closure;
+import groovyx.gpars.GParsConfig;
import groovyx.gpars.dataflow.operator.DataflowProcessor;
import groovyx.gpars.group.DefaultPGroup;
import groovyx.gpars.group.PGroup;
-import groovyx.gpars.scheduler.ResizeablePool;
import java.util.List;
import java.util.Map;
@@ -38,7 +38,7 @@
/**
* The parallel group used by all Dataflow Concurrency actors by default.
*/
- public static final PGroup DATA_FLOW_GROUP = new DefaultPGroup(new ResizeablePool(true, 1));
+ public static final PGroup DATA_FLOW_GROUP = new DefaultPGroup(GParsConfig.retrieveDefaultPool());
/**
* Maps threads/tasks to parallel groups they belong to
@@ -16,9 +16,9 @@
package groovyx.gpars.dataflow
+import groovyx.gpars.GParsConfig
import groovyx.gpars.group.DefaultPGroup
import groovyx.gpars.group.PGroup
-import groovyx.gpars.scheduler.ResizeablePool
/**
* A KanbanFlow is a network of dataflow connections made up from {@link KanbanLink}s.
@@ -40,7 +40,7 @@ class KanbanFlow {
boolean cycleAllowed = false
/** If adapted, this must be set before calling start() */
- PGroup pooledGroup = new DefaultPGroup(new ResizeablePool( /*isDaemon*/ true)) // default pool size
+ PGroup pooledGroup = new DefaultPGroup(GParsConfig.retrieveDefaultPool()) // default pool size
/**
* First part of the sequence <code>link producer to consumer</code>.
@@ -69,7 +69,7 @@ class KanbanFlow {
}
/** Stop all {@link KanbanLink}s of this flow. **/
- void stop() { links*.stop() } // note dk: needs investigation, maybe do it in reverse
+ void stop() { links*.stop() } // note dk: needs investigation, maybe do it in reverse
/** Helper method that inverses the sequence of Closure parameters. **/
static Closure inverse(Closure body) {
@@ -0,0 +1,28 @@
+// GPars - Groovy Parallel Systems
+//
+// Copyright © 2008-11 The original author or authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package groovyx.gpars.util;
+
+import java.util.Timer;
+
+/**
+ * Allows customized timers to be created in managed environments such as GAE
+ *
+ * @author Vaclav Pech
+ */
+public interface TimerFactory {
+ public Timer createTimer(final String name, final boolean daemon);
+}

0 comments on commit 283dd33

Please sign in to comment.