incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r620457 - in /incubator/sling/trunk: ./ sling/threads/ sling/threads/src/ sling/threads/src/main/ sling/threads/src/main/java/ sling/threads/src/main/java/org/ sling/threads/src/main/java/org/apache/ sling/threads/src/main/java/org/apache/s...
Date Mon, 11 Feb 2008 11:03:18 GMT
Author: cziegeler
Date: Mon Feb 11 03:03:12 2008
New Revision: 620457

URL: http://svn.apache.org/viewvc?rev=620457&view=rev
Log:
Add new threads module for providing thread pools.

Added:
    incubator/sling/trunk/sling/threads/
    incubator/sling/trunk/sling/threads/pom.xml   (with props)
    incubator/sling/trunk/sling/threads/src/
    incubator/sling/trunk/sling/threads/src/main/
    incubator/sling/trunk/sling/threads/src/main/java/
    incubator/sling/trunk/sling/threads/src/main/java/org/
    incubator/sling/trunk/sling/threads/src/main/java/org/apache/
    incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/
    incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/
    incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/ThreadPool.java
  (with props)
    incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/ThreadPoolManager.java
  (with props)
    incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/impl/
    incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/impl/DefaultThreadFactory.java
  (with props)
    incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/impl/DefaultThreadPool.java
  (with props)
    incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/impl/DefaultThreadPoolManager.java
  (with props)
    incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/impl/ExtendedThreadFactory.java
  (with props)
Modified:
    incubator/sling/trunk/pom.xml

Modified: incubator/sling/trunk/pom.xml
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/pom.xml?rev=620457&r1=620456&r2=620457&view=diff
==============================================================================
--- incubator/sling/trunk/pom.xml (original)
+++ incubator/sling/trunk/pom.xml Mon Feb 11 03:03:12 2008
@@ -99,6 +99,7 @@
         <module>sling/scheduler</module>
         <module>sling/sample</module>
         <module>sling/servlets-standard</module>
+        <module>sling/threads</module>
 
         <!-- Scripting Support -->
         <module>scripting/api</module>

Added: incubator/sling/trunk/sling/threads/pom.xml
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/sling/threads/pom.xml?rev=620457&view=auto
==============================================================================
--- incubator/sling/trunk/sling/threads/pom.xml (added)
+++ incubator/sling/trunk/sling/threads/pom.xml Mon Feb 11 03:03:12 2008
@@ -0,0 +1,85 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<!--
+  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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <groupId>org.apache.sling</groupId>
+        <artifactId>sling</artifactId>
+        <version>1-incubator-SNAPSHOT</version>
+        <relativePath>../../parent/pom.xml</relativePath>
+    </parent>
+
+    <artifactId>org.apache.sling.threads</artifactId>
+    <packaging>bundle</packaging>
+    <version>2.0.0-incubator-SNAPSHOT</version>
+
+    <name>Sling - Thread Support</name>
+    <description>
+        Support for thread handling like pooling.
+    </description>
+
+    <scm>
+        <connection>
+            scm:svn:http://svn.apache.org/repos/asf/incubator/sling/trunk/sling/threads
+        </connection>
+        <developerConnection>
+            scm:svn:https://svn.apache.org/repos/asf/incubator/sling/trunk/sling/threads
+        </developerConnection>
+        <url>
+            http://svn.apache.org/viewvc/incubator/sling/trunk/sling/threads
+        </url>
+    </scm>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.felix</groupId>
+                <artifactId>maven-scr-plugin</artifactId>
+            </plugin>
+            <plugin>
+                <groupId>org.apache.felix</groupId>
+                <artifactId>maven-bundle-plugin</artifactId>
+                <extensions>true</extensions>
+                <configuration>
+                    <instructions>
+                        <Export-Package>
+                            org.apache.sling.threads
+                        </Export-Package>
+                        <Private-Package>
+                            org.apache.sling.threads.impl
+                        </Private-Package>
+                    </instructions>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.osgi.compendium</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-api</artifactId>
+        </dependency>
+    </dependencies>
+</project>

Propchange: incubator/sling/trunk/sling/threads/pom.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/trunk/sling/threads/pom.xml
------------------------------------------------------------------------------
    svn:keywords = Id

Added: incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/ThreadPool.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/ThreadPool.java?rev=620457&view=auto
==============================================================================
--- incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/ThreadPool.java
(added)
+++ incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/ThreadPool.java
Mon Feb 11 03:03:12 2008
@@ -0,0 +1,42 @@
+/*
+ * 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.sling.threads;
+
+/**
+ * The ThreadPool interface allows to start runnables by
+ * getting threads from a managed pool.
+ *
+ * @version $Id$
+ */
+public interface ThreadPool {
+
+    /**
+     * Execute a runnable
+     * @param runnable The {@link Runnable} to execute
+     */
+    void execute(Runnable runnable);
+
+    /**
+     * The name of the thread pool.
+     */
+    String getName();
+
+    /**
+     * Shut down the thread pool.
+     */
+    void shutdown();
+}

Propchange: incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/ThreadPool.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/ThreadPool.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision url

Added: incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/ThreadPoolManager.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/ThreadPoolManager.java?rev=620457&view=auto
==============================================================================
--- incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/ThreadPoolManager.java
(added)
+++ incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/ThreadPoolManager.java
Mon Feb 11 03:03:12 2008
@@ -0,0 +1,40 @@
+/*
+ * 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.sling.threads;
+
+/**
+ * The <cod>ThreadPoolManager</code> manages thread pools.
+ *
+ * @version $Id$
+ */
+public interface ThreadPoolManager {
+
+    /**
+     * Add a new pool.
+     * If a pool with the same name already exists, the new pool is not added
+     * and false is returned.
+     * @param pool The pool
+     * @return True if the pool could be added, false otherwise.
+     */
+    boolean add(ThreadPool pool);
+
+    /**
+     * Get a thread pool
+     * @param name The name of the thread pool or null for the default pool.
+     */
+    ThreadPool get(String name);
+}

Propchange: incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/ThreadPoolManager.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/ThreadPoolManager.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision url

Added: incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/impl/DefaultThreadFactory.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/impl/DefaultThreadFactory.java?rev=620457&view=auto
==============================================================================
--- incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/impl/DefaultThreadFactory.java
(added)
+++ incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/impl/DefaultThreadFactory.java
Mon Feb 11 03:03:12 2008
@@ -0,0 +1,79 @@
+/*
+ * 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.sling.threads.impl;
+
+
+/**
+ * This class is responsible to create new Thread instances.
+ * It's a very basic implementation.
+ *
+ * @version $Id$
+ */
+public class DefaultThreadFactory
+    implements ExtendedThreadFactory {
+
+    /** The daemon mode */
+    private boolean isDaemon = DefaultThreadPoolManager.DEFAULT_DAEMON_MODE;
+
+    /** The priority of newly created Threads */
+    private int priority = DefaultThreadPoolManager.DEFAULT_THREAD_PRIORITY;
+
+    /**
+     * @see org.apache.sling.threads.impl.ExtendedThreadFactory#setDaemon(boolean)
+     */
+    public void setDaemon( boolean isDaemon ) {
+        this.isDaemon = isDaemon;
+    }
+
+    /**
+     * @see org.apache.sling.threads.impl.ExtendedThreadFactory#isDaemon()
+     */
+    public boolean isDaemon() {
+        return this.isDaemon;
+    }
+
+    /**
+     * @see org.apache.sling.threads.impl.ExtendedThreadFactory#setPriority(int)
+     */
+    public void setPriority( final int priority ) {
+        if( ( Thread.MAX_PRIORITY == priority ) ||
+            ( Thread.MIN_PRIORITY == priority ) ||
+            ( Thread.NORM_PRIORITY == priority ) ) {
+            this.priority = priority;
+        } else {
+            throw new IllegalStateException("Unknown priority " + this.priority);
+        }
+    }
+
+    /**
+     * @see org.apache.sling.threads.impl.ExtendedThreadFactory#getPriority()
+     */
+    public int getPriority() {
+        return this.priority;
+    }
+
+    /**
+     * @see org.apache.sling.threads.impl.ExtendedThreadFactory#newThread(java.lang.Runnable)
+     */
+    public Thread newThread( final Runnable command ) {
+        final Thread thread = new Thread( command );
+        thread.setPriority( this.priority );
+        thread.setDaemon( this.isDaemon );
+
+        return thread;
+    }
+}

Propchange: incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/impl/DefaultThreadFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/impl/DefaultThreadFactory.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision url

Added: incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/impl/DefaultThreadPool.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/impl/DefaultThreadPool.java?rev=620457&view=auto
==============================================================================
--- incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/impl/DefaultThreadPool.java
(added)
+++ incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/impl/DefaultThreadPool.java
Mon Feb 11 03:03:12 2008
@@ -0,0 +1,239 @@
+/*
+ * 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.sling.threads.impl;
+
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.RejectedExecutionHandler;
+import java.util.concurrent.SynchronousQueue;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.sling.threads.ThreadPool;
+import org.apache.sling.threads.ThreadPoolManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * The DefaultThreadPool class implements the {@link ThreadPool} interface.
+ * Instances of this class are managed by the {@link ThreadPoolManager}.
+ *
+ * @version $Id$
+ */
+public class DefaultThreadPool
+    implements ThreadPool {
+
+    /** By default we use the logger for this class. */
+    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
+
+    /** The name of this thread pool */
+    protected final String name;
+
+    /** The executor. */
+    protected ThreadPoolExecutor executor;
+
+    /** Should we wait for running jobs to terminate on shutdown ? */
+    protected final boolean shutdownGraceful;
+
+    /** How long to wait for running jobs to terminate on disposition */
+    protected final int shutdownWaitTimeMs;
+
+    /**
+     * Create a new thread pool.
+     * @param name - The name of the thread pool. If null {@link DefaultThreadPoolManager#DEFAULT_THREADPOOL_NAME}
+     *               is used
+     */
+    public DefaultThreadPool(final String name) {
+        this(DefaultThreadPoolManager.DEFAULT_THREADPOOL_NAME,
+                DefaultThreadPoolManager.DEFAULT_MIN_POOL_SIZE,
+                DefaultThreadPoolManager.DEFAULT_MAX_POOL_SIZE,
+                DefaultThreadPoolManager.DEFAULT_QUEUE_SIZE,
+                DefaultThreadPoolManager.DEFAULT_KEEP_ALIVE_TIME,
+                DefaultThreadPoolManager.DEFAULT_BLOCK_POLICY,
+                DefaultThreadPoolManager.DEFAULT_SHUTDOWN_GRACEFUL,
+                DefaultThreadPoolManager.DEFAULT_SHUTDOWN_WAIT_TIME,
+             null,
+             DefaultThreadPoolManager.DEFAULT_THREAD_PRIORITY,
+             DefaultThreadPoolManager.DEFAULT_DAEMON_MODE);
+    }
+
+    /**
+     * Create a new thread pool.
+     * @param name - The name of the thread pool. If null {@link DefaultThreadPoolManager#DEFAULT_THREADPOOL_NAME}
+     *               is used
+     */
+    public DefaultThreadPool(final String name,
+                             int   minPoolSize,
+                             int   maxPoolSize,
+                             final int queueSize,
+                             long  keepAliveTime,
+                             String blockPolicy,
+                             final boolean shutdownGraceful,
+                             final int shutdownWaitTimeMs,
+                             final ThreadFactory factory,
+                             final int   priority,
+                             final boolean isDaemon) {
+        this.logger.info("ThreadPool [{}] initializing ...", name);
+
+        // name
+        if ( name != null ) {
+            this.name = name;
+        } else {
+            this.name = DefaultThreadPoolManager.DEFAULT_THREADPOOL_NAME;
+        }
+
+        // factory
+        final ThreadFactory threadFactory;
+        if (factory == null) {
+            logger.warn("No ThreadFactory is configured. Will use a "
+                + DefaultThreadFactory.class.getName());
+            threadFactory = new DefaultThreadFactory();
+        } else {
+            threadFactory = factory;
+        }
+
+        // Min pool size
+        // make sure we have enough threads for the default thread pool as we
+        // need one for ourself
+        if (DefaultThreadPoolManager.DEFAULT_THREADPOOL_NAME.equals(name)
+            && ((minPoolSize > 0) && (minPoolSize < DefaultThreadPoolManager.DEFAULT_MIN_POOL_SIZE)))
{
+            minPoolSize = DefaultThreadPoolManager.DEFAULT_MIN_POOL_SIZE;
+        } else if (minPoolSize < 1) {
+            minPoolSize = 1;
+            this.logger.warn("min-pool-size < 1 for pool \"" + name + "\". Set to 1");
+        }
+        // Max pool size
+        maxPoolSize = (maxPoolSize < 0) ? Integer.MAX_VALUE : maxPoolSize;
+
+        // Set priority and daemon if the factory is an extended factory
+        if ( threadFactory instanceof ExtendedThreadFactory ) {
+            final ExtendedThreadFactory extTF = (ExtendedThreadFactory)threadFactory;
+            extTF.setPriority(priority);
+            extTF.setDaemon(isDaemon);
+        } else {
+            if ( priority != Thread.NORM_PRIORITY ) {
+                this.logger.warn("ThreadFactory " + threadFactory + " does not support setting
the priority or daemon setting.");
+            }
+            if ( isDaemon != DefaultThreadPoolManager.DEFAULT_DAEMON_MODE ) {
+                this.logger.warn("ThreadFactory " + threadFactory + " does not support setting
the daemon mode.");
+            }
+        }
+
+        // Keep alive time
+        if (keepAliveTime < 0) {
+            keepAliveTime = 1000;
+            this.logger.warn("keep-alive-time-ms < 0 for pool \"" + name + "\". Set to
1000");
+        }
+
+        // Queue
+        final BlockingQueue<Runnable> queue;
+        if (queueSize != 0) {
+            if (queueSize > 0) {
+                queue = new java.util.concurrent.ArrayBlockingQueue<Runnable>(queueSize);
+            } else {
+                queue = new LinkedBlockingQueue<Runnable>();
+            }
+        } else {
+            queue = new SynchronousQueue<Runnable>();
+        }
+
+        if ( blockPolicy == null ) {
+            blockPolicy = DefaultThreadPoolManager.DEFAULT_BLOCK_POLICY;
+        }
+        final RejectedExecutionHandler handler;
+        if (DefaultThreadPoolManager.POLICY_ABORT.equalsIgnoreCase(blockPolicy)) {
+            handler = new ThreadPoolExecutor.AbortPolicy();
+        } else if (DefaultThreadPoolManager.POLICY_DISCARD.equalsIgnoreCase(blockPolicy))
{
+            handler = new ThreadPoolExecutor.AbortPolicy();
+        } else if (DefaultThreadPoolManager.POLICY_DISCARD_OLDEST.equalsIgnoreCase(blockPolicy))
{
+            handler = new ThreadPoolExecutor.AbortPolicy();
+        } else if (DefaultThreadPoolManager.POLICY_RUN.equalsIgnoreCase(blockPolicy)) {
+            handler = new ThreadPoolExecutor.AbortPolicy();
+        } else {
+            final StringBuffer msg = new StringBuffer();
+            msg.append("WARNING: Unknown block-policy configuration \"")
+                .append(blockPolicy);
+            msg.append("\". Should be one of \"").append(DefaultThreadPoolManager.POLICY_ABORT);
+            msg.append("\",\"").append(DefaultThreadPoolManager.POLICY_DISCARD);
+            msg.append("\",\"").append(DefaultThreadPoolManager.POLICY_DISCARD_OLDEST);
+            msg.append("\",\"").append(DefaultThreadPoolManager.POLICY_RUN);
+            msg.append("\". Will use \"").append(DefaultThreadPoolManager.DEFAULT_BLOCK_POLICY).append("\"");
+            logger.warn(msg.toString());
+            handler = new ThreadPoolExecutor.CallerRunsPolicy();
+        }
+        this.shutdownGraceful = shutdownGraceful;
+        this.shutdownWaitTimeMs = shutdownWaitTimeMs;
+        this.executor = new ThreadPoolExecutor(minPoolSize,
+                maxPoolSize,
+                keepAliveTime,
+                TimeUnit.MILLISECONDS,
+                queue,
+                threadFactory,
+                handler);
+        this.logger.info("ThreadPool [{}] initialized.", name);
+    }
+
+    /**
+     * @see org.apache.sling.threads.ThreadPool#getName()
+     */
+    public String getName() {
+	    return name;
+    }
+
+    /**
+     * @see org.apache.sling.threads.ThreadPool#execute(java.lang.Runnable)
+     */
+    public void execute(Runnable runnable) {
+        if ( this.executor == null ) {
+            throw new IllegalStateException("Thread pool " + this.name + " is already shutdown.");
+        }
+        if ( runnable != null ) {
+            this.logger.debug("Executing runnable: {},pool={}", runnable, this.name);
+
+        	this.executor.execute(runnable);
+        }
+    }
+
+    /**
+     * @see org.apache.sling.threads.ThreadPool#shutdown()
+     */
+    public void shutdown() {
+        if ( this.executor != null ) {
+            if (shutdownGraceful) {
+                this.executor.shutdown();
+            } else {
+                this.executor.shutdownNow();
+            }
+
+            try {
+                if (this.shutdownWaitTimeMs > 0) {
+                    if (!this.executor.awaitTermination(this.shutdownWaitTimeMs, TimeUnit.MILLISECONDS))
{
+                        logger.warn("running commands have not terminated within "
+                            + this.shutdownWaitTimeMs
+                            + "ms. Will shut them down by interruption");
+                        this.executor.shutdownNow();
+                    }
+                }
+            } catch (final InterruptedException ie) {
+                this.logger.error("Cannot shutdown ThreadPool", ie);
+            }
+            this.executor = null;
+        }
+    }
+}

Propchange: incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/impl/DefaultThreadPool.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/impl/DefaultThreadPool.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision url

Added: incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/impl/DefaultThreadPoolManager.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/impl/DefaultThreadPoolManager.java?rev=620457&view=auto
==============================================================================
--- incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/impl/DefaultThreadPoolManager.java
(added)
+++ incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/impl/DefaultThreadPoolManager.java
Mon Feb 11 03:03:12 2008
@@ -0,0 +1,150 @@
+/*
+ * 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.sling.threads.impl;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.sling.threads.ThreadPool;
+import org.apache.sling.threads.ThreadPoolManager;
+import org.osgi.service.component.ComponentContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * The DefaultThreadPoolManager implements the {@link ThreadPoolManager} interface
+ * and is responsible to manage {@link ThreadPool}s.
+ *
+ * @scr.component metatype="false"
+ * @scr.service interface="org.apache.sling.threads.ThreadPoolManager"
+ *
+ * @version $Id$
+ */
+public class DefaultThreadPoolManager implements ThreadPoolManager {
+
+    /** The default queue size */
+    protected final static int DEFAULT_QUEUE_SIZE = -1;
+
+    /** The default maximum pool size */
+    protected final static int DEFAULT_MAX_POOL_SIZE = 5;
+
+    /** The default minimum pool size */
+    protected final static int DEFAULT_MIN_POOL_SIZE = 5;
+
+    /** The default thread priority */
+    protected final static int DEFAULT_THREAD_PRIORITY =  Thread.NORM_PRIORITY;
+
+    /** The default daemon mode */
+    protected final static boolean DEFAULT_DAEMON_MODE = false;
+
+    /** The default keep alive time */
+    protected final static long DEFAULT_KEEP_ALIVE_TIME = 60000L;
+
+    /** The default way to shutdown gracefully */
+    protected final static boolean DEFAULT_SHUTDOWN_GRACEFUL = false;
+
+    /** The default shutdown waittime time */
+    protected final static int DEFAULT_SHUTDOWN_WAIT_TIME = -1;
+
+    /** The default shutdown waittime time */
+    protected final static String DEFAULT_THREADPOOL_NAME = "default";
+
+    /** ThreadPool block policy ABORT */
+    protected final static String POLICY_ABORT = "ABORT";
+
+    /** ThreadPool block policy DISCARD */
+    protected final static String POLICY_DISCARD = "DISCARD";
+
+    /** ThreadPool block policy DISCARD-OLDEST */
+    protected final static String POLICY_DISCARD_OLDEST = "DISCARDOLDEST";
+
+    /** ThreadPool block policy RUN */
+    protected final static String POLICY_RUN = "RUN";
+
+    /** The default shutdown waittime time */
+    protected final static String DEFAULT_BLOCK_POLICY = POLICY_RUN;
+
+    /** By default we use the logger for this class. */
+    protected Logger logger = LoggerFactory.getLogger(getClass());
+
+    /** The managed thread pools */
+    protected final Map<String, ThreadPool> pools = new HashMap<String, ThreadPool>();
+
+    /**
+     * Activate this component.
+     */
+    protected void activate(ComponentContext context) throws Exception {
+        this.logger.info("Starting thread pool manager.");
+        final ThreadPool defaultPool = new DefaultThreadPool(
+                    DEFAULT_THREADPOOL_NAME,
+                    DEFAULT_MIN_POOL_SIZE,
+                    DEFAULT_MAX_POOL_SIZE,
+                    DEFAULT_QUEUE_SIZE,
+                    DEFAULT_KEEP_ALIVE_TIME,
+                    DEFAULT_BLOCK_POLICY,
+                    DEFAULT_SHUTDOWN_GRACEFUL,
+                    DEFAULT_SHUTDOWN_WAIT_TIME,
+                    null,
+                    DEFAULT_THREAD_PRIORITY,
+                    DEFAULT_DAEMON_MODE);
+        this.pools.put(defaultPool.getName(), defaultPool);
+        this.logger.info("Thread pool manager startet with default pool.");
+    }
+
+    /**
+     * Deactivate this component.
+     */
+    protected void deactivate(ComponentContext context) throws Exception {
+        this.logger.info("Stopping thread pool manager.");
+        this.logger.debug("Disposing all thread pools");
+
+        for (ThreadPool pool : this.pools.values()) {
+            this.logger.debug("Shutting down thread pool {}", pool.getName());
+
+            pool.shutdown();
+
+            this.logger.debug("Thread pool " + pool.getName() + " is shut down.");
+        }
+        this.pools.clear();
+        this.logger.info("Thread pool manager stopped.");
+    }
+
+    /**
+     * @see org.apache.sling.threads.ThreadPoolManager#add(org.apache.sling.threads.ThreadPool)
+     */
+    public boolean add(ThreadPool pool) {
+        synchronized ( this.pools ) {
+            if (null != pools.get(pool.getName())) {
+                return false;
+            }
+            pools.put(pool.getName(), pool);
+        }
+        return true;
+    }
+
+    /**
+     * @see org.apache.sling.threads.ThreadPoolManager#get(java.lang.String)
+     */
+    public ThreadPool get(String name) {
+        if ( name == null ) {
+            name = DEFAULT_THREADPOOL_NAME;
+        }
+        synchronized (this.pools) {
+            return this.pools.get(name);
+        }
+    }
+}

Propchange: incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/impl/DefaultThreadPoolManager.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/impl/DefaultThreadPoolManager.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision url

Added: incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/impl/ExtendedThreadFactory.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/impl/ExtendedThreadFactory.java?rev=620457&view=auto
==============================================================================
--- incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/impl/ExtendedThreadFactory.java
(added)
+++ incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/impl/ExtendedThreadFactory.java
Mon Feb 11 03:03:12 2008
@@ -0,0 +1,67 @@
+/*
+ * 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.sling.threads.impl;
+
+import java.util.concurrent.ThreadFactory;
+
+/**
+ * This is an extension to the {@link ThreadFactory}.
+ *
+ * @version $Id$
+ */
+public interface ExtendedThreadFactory
+    extends ThreadFactory {
+
+    /**
+     * Set the daemon mode of created <code>Thread</code>s should have
+     *
+     * @param isDaemon Whether new {@link Thread}s should run as daemons.
+     */
+    void setDaemon( boolean isDaemon );
+
+    /**
+     * Get the daemon mode created <code>Thread</code>s will have
+     *
+     * @return Whether new {@link Thread}s should run as daemons.
+     */
+    boolean isDaemon();
+
+    /**
+     * Set the priority newly created <code>Thread</code>s should have
+     *
+     * @param priority One of {@link Thread#MIN_PRIORITY}, {@link
+     *        Thread#NORM_PRIORITY}, {@link Thread#MAX_PRIORITY}
+     */
+    void setPriority( int priority );
+
+    /**
+     * Get the priority newly created <code>Thread</code>s will have
+     *
+     * @return One of {@link Thread#MIN_PRIORITY}, {@link
+     *         Thread#NORM_PRIORITY}, {@link Thread#MAX_PRIORITY}
+     */
+    int getPriority();
+
+    /**
+     * Create a new Thread for a {@link Runnable} command
+     *
+     * @param command The <code>Runnable</code>
+     *
+     * @return new <code>Thread</code>
+     */
+    Thread newThread( Runnable command );
+}

Propchange: incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/impl/ExtendedThreadFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/impl/ExtendedThreadFactory.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision url



Mime
View raw message