You are viewing a plain text version of this content. The canonical link for it is here.
Posted to axis-cvs@ws.apache.org by ga...@apache.org on 2006/03/23 06:35:38 UTC

svn commit: r388048 - in /webservices/axis/trunk/java/src/org/apache/axis/components/threadpool: PooledTaskManager.java SimpleTaskManager.java TaskManager.java TaskManagerFactory.java

Author: gawor
Date: Wed Mar 22 21:35:37 2006
New Revision: 388048

URL: http://svn.apache.org/viewcvs?rev=388048&view=rev
Log:
basic task manager clases

Added:
    webservices/axis/trunk/java/src/org/apache/axis/components/threadpool/PooledTaskManager.java
    webservices/axis/trunk/java/src/org/apache/axis/components/threadpool/SimpleTaskManager.java
    webservices/axis/trunk/java/src/org/apache/axis/components/threadpool/TaskManager.java
    webservices/axis/trunk/java/src/org/apache/axis/components/threadpool/TaskManagerFactory.java

Added: webservices/axis/trunk/java/src/org/apache/axis/components/threadpool/PooledTaskManager.java
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/java/src/org/apache/axis/components/threadpool/PooledTaskManager.java?rev=388048&view=auto
==============================================================================
--- webservices/axis/trunk/java/src/org/apache/axis/components/threadpool/PooledTaskManager.java (added)
+++ webservices/axis/trunk/java/src/org/apache/axis/components/threadpool/PooledTaskManager.java Wed Mar 22 21:35:37 2006
@@ -0,0 +1,123 @@
+/*
+ * Copyright 2001-2004 The Apache Software Foundation.
+ * 
+ * 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 org.apache.axis.components.threadpool;
+
+import java.util.LinkedList;
+import java.util.ArrayList;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import org.apache.axis.utils.Messages;
+
+/**
+ * Pooled TaskManager implementation. 
+ * Uses pool of threads to execute the tasks. The pools of threads grows at
+ * specified rate and shrinks with time if not being used.
+ */
+public class PooledTaskManager implements TaskManager {
+
+    private static final int DEFAULT_NOTIFICATION_EXPONENT = 2;
+
+    protected static Log log =
+        LogFactory.getLog(PooledTaskManager.class.getName());
+
+    private Queue queue;
+    private ArrayList threads;
+    private double exponent;
+    private int computedSize;
+    
+    public PooledTaskManager() {
+        this(DEFAULT_NOTIFICATION_EXPONENT);
+    }
+
+    public PooledTaskManager(double exponent) {
+        this.exponent = exponent;
+        this.queue = new Queue();
+        this.threads = new ArrayList();
+        this.computedSize = 0;
+    }
+        
+    public void execute(Runnable r) {
+        int size = this.queue.enqueue(r);
+        addThread(size);
+    }
+        
+    private synchronized void computeSize() {
+        this.computedSize = 
+            (int)Math.pow(this.threads.size(), this.exponent);
+    }
+    
+    private synchronized void addThread(int size) {
+        if (size > this.computedSize) {
+            NotificationThread thread = new NotificationThread();
+            thread.setDaemon(true);
+            this.threads.add(thread);
+            thread.start();
+            computeSize();
+        }
+    }
+    
+    private synchronized void removeThread(Thread thread) {
+            this.threads.remove(thread);
+            computeSize();
+    }
+    
+    
+    private class NotificationThread extends Thread {
+
+        public void run() {
+            Runnable r = null;
+            while(true) {
+                try {
+                    r = (Runnable)queue.dequeue();
+                } catch (InterruptedException e) {
+                    break;
+                }
+                if (r == null) {
+                    break;
+                }
+                try {
+                    r.run();
+                } catch (Throwable e) {
+                    log.debug(Messages.getMessage("exceptionPrinting"), e);
+                }
+            }
+            removeThread(this);
+        }
+    }
+    
+    private static class Queue {
+        
+        private LinkedList queue = new LinkedList();
+        
+        public synchronized int enqueue(Runnable r) {
+            queue.add(r);
+            notify();
+            return queue.size();
+        }
+        
+        public synchronized Object dequeue() throws InterruptedException {
+            if (queue.isEmpty()) {
+                wait(1000 * 60 * 2);
+            }
+            return (queue.isEmpty()) ? null : queue.removeFirst();
+        }
+        
+    }
+       
+}

Added: webservices/axis/trunk/java/src/org/apache/axis/components/threadpool/SimpleTaskManager.java
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/java/src/org/apache/axis/components/threadpool/SimpleTaskManager.java?rev=388048&view=auto
==============================================================================
--- webservices/axis/trunk/java/src/org/apache/axis/components/threadpool/SimpleTaskManager.java (added)
+++ webservices/axis/trunk/java/src/org/apache/axis/components/threadpool/SimpleTaskManager.java Wed Mar 22 21:35:37 2006
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2001-2004 The Apache Software Foundation.
+ * 
+ * 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 org.apache.axis.components.threadpool;
+
+/**
+ * Simple TaskManager implementation. 
+ * Starts a new thread for each task.
+ */
+public class SimpleTaskManager implements TaskManager {
+    
+    public void execute(Runnable task) {
+        Thread thread = new Thread(task);
+        thread.start();
+    }
+    
+}

Added: webservices/axis/trunk/java/src/org/apache/axis/components/threadpool/TaskManager.java
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/java/src/org/apache/axis/components/threadpool/TaskManager.java?rev=388048&view=auto
==============================================================================
--- webservices/axis/trunk/java/src/org/apache/axis/components/threadpool/TaskManager.java (added)
+++ webservices/axis/trunk/java/src/org/apache/axis/components/threadpool/TaskManager.java Wed Mar 22 21:35:37 2006
@@ -0,0 +1,26 @@
+/*
+ * Copyright 2001-2004 The Apache Software Foundation.
+ * 
+ * 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 org.apache.axis.components.threadpool;
+
+public interface TaskManager {
+
+    /**
+     * Executes the specified task asynchronously.
+     */
+    void execute(Runnable task);
+    
+}

Added: webservices/axis/trunk/java/src/org/apache/axis/components/threadpool/TaskManagerFactory.java
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/java/src/org/apache/axis/components/threadpool/TaskManagerFactory.java?rev=388048&view=auto
==============================================================================
--- webservices/axis/trunk/java/src/org/apache/axis/components/threadpool/TaskManagerFactory.java (added)
+++ webservices/axis/trunk/java/src/org/apache/axis/components/threadpool/TaskManagerFactory.java Wed Mar 22 21:35:37 2006
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2001-2004 The Apache Software Foundation.
+ * 
+ * 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 org.apache.axis.components.threadpool;
+
+import org.apache.axis.AxisProperties;
+
+import org.apache.commons.logging.LogFactory;
+import org.apache.commons.logging.Log;
+
+public abstract class TaskManagerFactory {
+
+    protected static Log log = 
+        LogFactory.getLog(TaskManagerFactory.class.getName());
+
+    static {
+        AxisProperties.setClassOverrideProperty(
+                TaskManager.class, 
+                "axis.TaskManager");
+        AxisProperties.setClassDefault(
+                TaskManager.class, 
+                "org.apache.axis.components.threadpool.SimpleTaskManager");
+    }
+
+    /**
+     * Returns an instance of TaskManager.
+     */
+    public static TaskManager getTaskManager() {
+        TaskManager taskManager= (TaskManager) AxisProperties.newInstance(TaskManager.class);
+        log.debug("axis.TaksManager: " + taskManager);
+        return taskManager;
+    }
+}