You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stratos.apache.org by ni...@apache.org on 2014/10/10 16:20:51 UTC
[2/5] git commit: Scheduled executor service pool to schedule tasks
in CC.
Scheduled executor service pool to schedule tasks in CC.
Project: http://git-wip-us.apache.org/repos/asf/stratos/repo
Commit: http://git-wip-us.apache.org/repos/asf/stratos/commit/0fe96488
Tree: http://git-wip-us.apache.org/repos/asf/stratos/tree/0fe96488
Diff: http://git-wip-us.apache.org/repos/asf/stratos/diff/0fe96488
Branch: refs/heads/container-autoscaling
Commit: 0fe96488a6ebf1e5ba3e0304811ac07341513680
Parents: ff2a02f
Author: Nirmal Fernando <ni...@gmail.com>
Authored: Fri Oct 10 19:47:37 2014 +0530
Committer: Nirmal Fernando <ni...@gmail.com>
Committed: Fri Oct 10 19:50:27 2014 +0530
----------------------------------------------------------------------
.../concurrent/ScheduledThreadExecutor.java | 78 ++++++++++++++++++++
1 file changed, 78 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/stratos/blob/0fe96488/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/concurrent/ScheduledThreadExecutor.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/concurrent/ScheduledThreadExecutor.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/concurrent/ScheduledThreadExecutor.java
new file mode 100644
index 0000000..56f2a2c
--- /dev/null
+++ b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/concurrent/ScheduledThreadExecutor.java
@@ -0,0 +1,78 @@
+/*
+ * 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.stratos.cloud.controller.concurrent;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * This class can be used to create a scheduled executor pool, and hand over new jobs to the pool.
+ */
+public class ScheduledThreadExecutor {
+ private ScheduledExecutorService executor;
+
+ private static class Holder {
+ private static final ScheduledThreadExecutor INSTANCE = new ScheduledThreadExecutor();
+ }
+
+ public static ScheduledThreadExecutor getInstance() {
+ return Holder.INSTANCE;
+ }
+
+ private ScheduledThreadExecutor() {
+ executor = Executors.newScheduledThreadPool(50);
+ }
+
+ public ScheduledFuture<?> schedule(Runnable job, int interval){
+ return executor.scheduleAtFixedRate(job, 0, interval, TimeUnit.MILLISECONDS);
+ }
+
+ public List<ScheduledFuture<?>> scheduleAll(Runnable[] jobs, int interval){
+ List<ScheduledFuture<?>> list = new ArrayList<ScheduledFuture<?>>();
+ for (Runnable job : jobs) {
+
+ list.add(this.schedule(job, interval));
+ }
+ return list;
+ }
+
+ public void shutdown() {
+ executor.shutdown(); // Disable new tasks from being submitted
+ try {
+ // Wait a while for existing tasks to terminate
+ if (!executor.awaitTermination(60, TimeUnit.SECONDS)) {
+ executor.shutdownNow(); // Cancel currently executing tasks
+ // Wait a while for tasks to respond to being cancelled
+ if (!executor.awaitTermination(60, TimeUnit.SECONDS))
+ System.err.println("Pool did not terminate");
+ }
+ } catch (InterruptedException ie) {
+ // (Re-)Cancel if current thread also interrupted
+ executor.shutdownNow();
+ // Preserve interrupt status
+ Thread.currentThread().interrupt();
+ }
+ }
+
+
+}