You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by gn...@apache.org on 2009/03/18 11:47:13 UTC

svn commit: r755540 - in /servicemix/utils/trunk/src/main/java/org/apache/servicemix/executors: ExecutorAwareRunnable.java impl/ExecutorConfig.java impl/ExecutorFactoryImpl.java impl/ExecutorImpl.java

Author: gnodet
Date: Wed Mar 18 10:47:12 2009
New Revision: 755540

URL: http://svn.apache.org/viewvc?rev=755540&view=rev
Log:
Improve executors to be able to bypass the thread pool for a given task if needed

Added:
    servicemix/utils/trunk/src/main/java/org/apache/servicemix/executors/ExecutorAwareRunnable.java
Modified:
    servicemix/utils/trunk/src/main/java/org/apache/servicemix/executors/impl/ExecutorConfig.java
    servicemix/utils/trunk/src/main/java/org/apache/servicemix/executors/impl/ExecutorFactoryImpl.java
    servicemix/utils/trunk/src/main/java/org/apache/servicemix/executors/impl/ExecutorImpl.java

Added: servicemix/utils/trunk/src/main/java/org/apache/servicemix/executors/ExecutorAwareRunnable.java
URL: http://svn.apache.org/viewvc/servicemix/utils/trunk/src/main/java/org/apache/servicemix/executors/ExecutorAwareRunnable.java?rev=755540&view=auto
==============================================================================
--- servicemix/utils/trunk/src/main/java/org/apache/servicemix/executors/ExecutorAwareRunnable.java (added)
+++ servicemix/utils/trunk/src/main/java/org/apache/servicemix/executors/ExecutorAwareRunnable.java Wed Mar 18 10:47:12 2009
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.servicemix.executors;
+
+/**
+ * An enhanced runnable interface that can be implemented
+ * by tasks submitted to the Executor.
+ */
+public interface ExecutorAwareRunnable extends Runnable {
+
+    /**
+     * This method can be used by the executor to have a hint whether the internal thread
+     * pool can be bypassed for the execution of this runnable.
+     * If the Executor supports such things and the method returns <code>true</code>,
+     * this task will be executed in the caller thread.
+     *
+     * @return <code>true</code> if the executor thread pool can be bypassed for this task
+     */
+    boolean shouldRunSynchronously();
+
+}

Modified: servicemix/utils/trunk/src/main/java/org/apache/servicemix/executors/impl/ExecutorConfig.java
URL: http://svn.apache.org/viewvc/servicemix/utils/trunk/src/main/java/org/apache/servicemix/executors/impl/ExecutorConfig.java?rev=755540&r1=755539&r2=755540&view=diff
==============================================================================
--- servicemix/utils/trunk/src/main/java/org/apache/servicemix/executors/impl/ExecutorConfig.java (original)
+++ servicemix/utils/trunk/src/main/java/org/apache/servicemix/executors/impl/ExecutorConfig.java Wed Mar 18 10:47:12 2009
@@ -39,6 +39,8 @@
 
     private boolean allowCoreThreadsTimeout = true;
 
+    private boolean bypassIfSynchronous;
+
     /**
      * @return the corePoolSize
      */
@@ -159,4 +161,17 @@
         this.allowCoreThreadsTimeout = allowCoreThreadsTimeout;
     }
 
+    /**
+     * @return if synchronous tasks should bypass the executor
+     */
+    public boolean isBypassIfSynchronous() {
+        return bypassIfSynchronous;
+    }
+
+    /**
+     * @param bypassIfSynchronous if synchronous tasks should bypass the executor
+     */
+    public void setBypassIfSynchronous(boolean bypassIfSynchronous) {
+        this.bypassIfSynchronous = bypassIfSynchronous;
+    }
 }

Modified: servicemix/utils/trunk/src/main/java/org/apache/servicemix/executors/impl/ExecutorFactoryImpl.java
URL: http://svn.apache.org/viewvc/servicemix/utils/trunk/src/main/java/org/apache/servicemix/executors/impl/ExecutorFactoryImpl.java?rev=755540&r1=755539&r2=755540&view=diff
==============================================================================
--- servicemix/utils/trunk/src/main/java/org/apache/servicemix/executors/impl/ExecutorFactoryImpl.java (original)
+++ servicemix/utils/trunk/src/main/java/org/apache/servicemix/executors/impl/ExecutorFactoryImpl.java Wed Mar 18 10:47:12 2009
@@ -55,14 +55,14 @@
 
     public Executor createExecutor(String id) {
         ExecutorConfig config = getConfig(id);
-        return new ExecutorImpl(createService(id, config), config.getShutdownDelay());
+        return new ExecutorImpl(createService(id, config), config.getShutdownDelay(), config.isBypassIfSynchronous());
     }
 
     public Executor createDaemonExecutor(String id) {
 
         ExecutorConfig config = getConfig(id);
         config.setThreadDaemon(true);
-        return new ExecutorImpl(createService(id, config), config.getShutdownDelay());
+        return new ExecutorImpl(createService(id, config), config.getShutdownDelay(), config.isBypassIfSynchronous());
     }
 
     protected ExecutorConfig getConfig(String id) {

Modified: servicemix/utils/trunk/src/main/java/org/apache/servicemix/executors/impl/ExecutorImpl.java
URL: http://svn.apache.org/viewvc/servicemix/utils/trunk/src/main/java/org/apache/servicemix/executors/impl/ExecutorImpl.java?rev=755540&r1=755539&r2=755540&view=diff
==============================================================================
--- servicemix/utils/trunk/src/main/java/org/apache/servicemix/executors/impl/ExecutorImpl.java (original)
+++ servicemix/utils/trunk/src/main/java/org/apache/servicemix/executors/impl/ExecutorImpl.java Wed Mar 18 10:47:12 2009
@@ -21,6 +21,7 @@
 import java.util.concurrent.TimeUnit;
 
 import org.apache.servicemix.executors.Executor;
+import org.apache.servicemix.executors.ExecutorAwareRunnable;
 
 /**
  * The default Executor implementation which uses a 
@@ -34,12 +35,21 @@
 
     private final long shutdownDelay;
 
-    public ExecutorImpl(ThreadPoolExecutor threadPool, long shutdownDelay) {
+    private final boolean bypassIfSynchronous;
+
+    public ExecutorImpl(ThreadPoolExecutor threadPool, long shutdownDelay, boolean bypassIfSynchronous) {
         this.threadPool = threadPool;
         this.shutdownDelay = shutdownDelay;
+        this.bypassIfSynchronous = bypassIfSynchronous;
     }
 
     public void execute(Runnable command) {
+        if (bypassIfSynchronous && command instanceof ExecutorAwareRunnable) {
+            if (((ExecutorAwareRunnable) command).shouldRunSynchronously()) {
+                command.run();
+                return;
+            }
+        }
         threadPool.execute(command);
     }