You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2018/08/21 10:17:41 UTC

[15/17] james-project git commit: JAMES-2523 Terminate threads for asynchronous tasks

JAMES-2523 Terminate threads for asynchronous tasks

They are used for ES DeleteByQuery operations


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/c8cd20d8
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/c8cd20d8
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/c8cd20d8

Branch: refs/heads/master
Commit: c8cd20d823474632d5eb01d6e098a34011c8faaa
Parents: b7cd375
Author: Benoit Tellier <bt...@linagora.com>
Authored: Mon Aug 20 16:01:20 2018 +0700
Committer: Benoit Tellier <bt...@linagora.com>
Committed: Tue Aug 21 17:15:05 2018 +0700

----------------------------------------------------------------------
 .../server/AsyncTasksExecutorModule.java        | 33 ++++++++++++++------
 1 file changed, 23 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/c8cd20d8/server/container/guice/guice-common/src/main/java/org/apache/james/modules/server/AsyncTasksExecutorModule.java
----------------------------------------------------------------------
diff --git a/server/container/guice/guice-common/src/main/java/org/apache/james/modules/server/AsyncTasksExecutorModule.java b/server/container/guice/guice-common/src/main/java/org/apache/james/modules/server/AsyncTasksExecutorModule.java
index 5b49d8b..b8e42fe 100644
--- a/server/container/guice/guice-common/src/main/java/org/apache/james/modules/server/AsyncTasksExecutorModule.java
+++ b/server/container/guice/guice-common/src/main/java/org/apache/james/modules/server/AsyncTasksExecutorModule.java
@@ -21,25 +21,38 @@ package org.apache.james.modules.server;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 
-import javax.inject.Named;
+import javax.annotation.PreDestroy;
 
 import com.google.inject.AbstractModule;
-import com.google.inject.Provides;
-import com.google.inject.Singleton;
+import com.google.inject.Provider;
+import com.google.inject.name.Names;
 
 public class AsyncTasksExecutorModule extends AbstractModule {
 
-    public static final int THREAD_POOL_SIZE = 8;
+    private static final int THREAD_POOL_SIZE = 8;
 
     @Override
     protected void configure() {
-
+        bind(ExecutorService.class).annotatedWith(Names.named("AsyncExecutor"))
+            .toProvider(new LifecycleAwareExecutorServiceProvider(
+                Executors.newFixedThreadPool(THREAD_POOL_SIZE)));
     }
 
-    @Provides
-    @Singleton
-    @Named("AsyncExecutor")
-    public ExecutorService provideAsyncExecutorService() {
-        return Executors.newFixedThreadPool(THREAD_POOL_SIZE);
+    public static class LifecycleAwareExecutorServiceProvider implements Provider<ExecutorService> {
+        private final ExecutorService executorService;
+
+        LifecycleAwareExecutorServiceProvider(ExecutorService executorService) {
+            this.executorService = executorService;
+        }
+
+        @Override
+        public ExecutorService get() {
+            return executorService;
+        }
+
+        @PreDestroy
+        public void stop() {
+            executorService.shutdownNow();
+        }
     }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org