Skip to content

Commit 296aebc

Browse files
Merge pull request #25 from BorderTech/refactor-submodules
TaskMaster shutdownNow and CacheHelper closeCache
2 parents 09b91ce + dce7da9 commit 296aebc

File tree

7 files changed

+76
-23
lines changed

7 files changed

+76
-23
lines changed

taskmaster-cache-helper/src/main/java/com/github/bordertech/taskmaster/cache/CachingHelperProvider.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
*/
1515
public interface CachingHelperProvider {
1616

17+
/**
18+
* Close and release the cache resources.
19+
*/
20+
void closeCacheManager();
21+
1722
/**
1823
* Create a cache with the specified duration.
1924
*

taskmaster-cache-helper/src/main/java/com/github/bordertech/taskmaster/cache/impl/CachingHelperProviderDefault.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import javax.cache.configuration.MutableConfiguration;
99
import javax.cache.expiry.AccessedExpiryPolicy;
1010
import javax.cache.expiry.Duration;
11+
import javax.cache.spi.CachingProvider;
1112

1213
/**
1314
* Default CachingHelperProvider implementation using JSR107 provider.
@@ -16,6 +17,14 @@
1617
*/
1718
public class CachingHelperProviderDefault implements CachingHelperProvider {
1819

20+
@Override
21+
public synchronized void closeCacheManager() {
22+
CachingProvider provider = Caching.getCachingProvider();
23+
if (provider != null && !provider.getCacheManager().isClosed()) {
24+
provider.getCacheManager().close();
25+
}
26+
}
27+
1928
@Override
2029
public synchronized <K, V> Cache<K, V> getOrCreateCache(final String name, final Class<K> keyClass,
2130
final Class<V> valueClass, final Duration duration) {

taskmaster-cache-helper/src/main/java/com/github/bordertech/taskmaster/cache/impl/CachingHelperProviderXmlConfig.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ public class CachingHelperProviderXmlConfig implements CachingHelperProvider {
3838
MANAGER = cachingProvider.getCacheManager(uri, CachingHelperProviderXmlConfig.class.getClassLoader());
3939
}
4040

41+
@Override
42+
public synchronized void closeCacheManager() {
43+
CachingProvider provider = Caching.getCachingProvider();
44+
if (provider != null && !provider.getCacheManager().isClosed()) {
45+
provider.getCacheManager().close();
46+
}
47+
}
48+
4149
@Override
4250
public synchronized <K, V> Cache<K, V> getOrCreateCache(final String name, final Class<K> keyClass,
4351
final Class<V> valueClass, final Duration duration) {

taskmaster-cache-helper/src/main/java/com/github/bordertech/taskmaster/cache/servlet/CachingProviderListener.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.github.bordertech.taskmaster.cache.servlet;
22

3-
import javax.cache.Caching;
4-
import javax.cache.spi.CachingProvider;
3+
import com.github.bordertech.taskmaster.cache.CachingHelper;
54
import javax.servlet.ServletContextEvent;
65
import javax.servlet.ServletContextListener;
76

@@ -17,11 +16,7 @@ public void contextInitialized(final ServletContextEvent sce) {
1716

1817
@Override
1918
public void contextDestroyed(final ServletContextEvent sce) {
20-
// TODO Include this in CacheHelperProvider Interface
21-
CachingProvider provider = Caching.getCachingProvider();
22-
if (provider != null && !provider.getCacheManager().isClosed()) {
23-
provider.getCacheManager().close();
24-
}
19+
CachingHelper.getProvider().closeCacheManager();
2520
}
2621

2722
}

taskmaster-core/src/main/java/com/github/bordertech/taskmaster/TaskMasterProvider.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,18 @@ public interface TaskMasterProvider {
1515
* accepted. Invocation has no additional effect if already shut down.
1616
*
1717
* @throws SecurityException if a security manager exists and shutting down this ExecutorService may manipulate
18-
* threads that the caller is not permitted to modify because it does not hold {@link
19-
* java.lang.RuntimePermission}<tt>("modifyThread")</tt>, or the security manager's <tt>checkAccess</tt> method
20-
* denies access.
18+
* threads that the caller is not permitted to modify because it does not hold permission
2119
*/
2220
void shutdown();
2321

22+
/**
23+
* Attempts to stop all actively executing tasks, halts the processing of waiting tasks.
24+
*
25+
* @throws SecurityException if a security manager exists and shutting down this ExecutorService may manipulate
26+
* threads that the caller is not permitted to modify because it does not hold permission
27+
*/
28+
void shutdownNow();
29+
2430
/**
2531
* Submits a Runnable task for execution and returns a Future representing that task. The Future's <tt>get</tt>
2632
* method will return the given result upon successful completion.

taskmaster-core/src/main/java/com/github/bordertech/taskmaster/impl/TaskMasterPoolUtil.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.bordertech.taskmaster.impl;
22

33
import com.github.bordertech.config.Config;
4+
import com.github.bordertech.taskmaster.exception.TaskMasterException;
45
import java.util.HashMap;
56
import java.util.Map;
67
import java.util.concurrent.ArrayBlockingQueue;
@@ -11,12 +12,15 @@
1112
import java.util.concurrent.SynchronousQueue;
1213
import java.util.concurrent.ThreadPoolExecutor;
1314
import java.util.concurrent.TimeUnit;
15+
import org.apache.commons.logging.Log;
16+
import org.apache.commons.logging.LogFactory;
1417

1518
/**
1619
* TaskMaster ExecutorService thread pool utility.
1720
*/
1821
public final class TaskMasterPoolUtil {
1922

23+
private static final Log LOGGER = LogFactory.getLog(TaskMasterPoolUtil.class);
2024
private static final Map<String, ExecutorService> THREAD_POOLS = new HashMap<>();
2125
private static final String TP_PARAM_PREFIX = "bordertech.taskmaster.pool.";
2226
private static final int DEFAULT_MAX_THREADS = 20;
@@ -91,10 +95,20 @@ private static ExecutorService buildPool(final String pool) {
9195
/**
9296
* Shutdown the thread pools.
9397
*/
94-
public static void shutdown() {
95-
for (ExecutorService exec : THREAD_POOLS.values()) {
96-
// TODO This logic needs to be put into TaskMaster
97-
exec.shutdownNow();
98+
public static void shutdownNow() {
99+
boolean error = false;
100+
// Shutdown the threads in the pool
101+
for (Map.Entry<String, ExecutorService> pools : THREAD_POOLS.entrySet()) {
102+
try {
103+
pools.getValue().shutdownNow();
104+
LOGGER.info("Shutdown thread pool [" + pools.getKey() + "].");
105+
} catch (Exception e) {
106+
error = true;
107+
LOGGER.error("Could not shutdown thread pool [" + pools.getKey() + "]. " + e.getMessage(), e);
108+
}
109+
}
110+
if (error) {
111+
throw new TaskMasterException("Errors occured shutdowning thread pools.");
98112
}
99113
}
100114

@@ -110,14 +124,13 @@ public static synchronized ExecutorService getPool(final String poolName) {
110124
if (pool == null) {
111125
throw new IllegalStateException("Pool [" + name + "] has not been defined.");
112126
}
113-
// TODO This logic needs to be put into TaskMaster
114127
// Check if terminated (reactivate)
115128
if (pool.isTerminated()) {
116-
// TODO LOG Warning and maybe control this via flag
117129
// Check not interrupted for some reason (maybe server shutting down)
118130
if (Thread.currentThread().isInterrupted()) {
119131
throw new IllegalStateException("Pool [" + name + "] has terminated and thread is interrupted.");
120132
}
133+
LOGGER.info("Thread pool [" + name + "] is terminated. Will be built again.");
121134
pool = TaskMasterPoolUtil.buildPool(name);
122135
THREAD_POOLS.put(name, pool);
123136
}

taskmaster-core/src/main/java/com/github/bordertech/taskmaster/impl/TaskMasterProviderExecutorService.java

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package com.github.bordertech.taskmaster.impl;
22

3-
import com.github.bordertech.taskmaster.exception.RejectedTaskException;
43
import com.github.bordertech.taskmaster.TaskFuture;
4+
import com.github.bordertech.taskmaster.TaskMasterProvider;
5+
import com.github.bordertech.taskmaster.exception.RejectedTaskException;
56
import java.util.concurrent.ExecutorService;
67
import java.util.concurrent.Future;
78
import java.util.concurrent.RejectedExecutionException;
89
import javax.inject.Singleton;
9-
import com.github.bordertech.taskmaster.TaskMasterProvider;
1010

1111
/**
1212
* Handle running tasks via {@link ExecutorService}.
@@ -22,12 +22,10 @@ public void shutdown() {
2222
shutdownNow();
2323
}
2424

25-
/**
26-
* TODO This needs to be put in TaskMaster.
27-
*/
25+
@Override
2826
public void shutdownNow() {
2927
// Provide an immediate shutdown of threads without running the waiting threads.
30-
TaskMasterPoolUtil.shutdown();
28+
TaskMasterPoolUtil.shutdownNow();
3129
}
3230

3331
@Override
@@ -37,7 +35,18 @@ public <T> TaskFuture<T> submit(final Runnable task, final T result) throws Reje
3735

3836
@Override
3937
public <T> TaskFuture<T> submit(final Runnable task, final T result, final String pool) throws RejectedTaskException {
40-
ExecutorService exec = TaskMasterPoolUtil.getPool(pool);
38+
if (task == null) {
39+
throw new IllegalArgumentException("Task cannot be null");
40+
}
41+
if (result == null) {
42+
throw new IllegalArgumentException("Result cannot be null");
43+
}
44+
if (pool == null) {
45+
throw new IllegalArgumentException("Pool cannot be null");
46+
}
47+
// Get the executor
48+
ExecutorService exec = getPool(pool);
49+
// Submit the task
4150
try {
4251
Future<T> future = exec.submit(task, result);
4352
return new TaskFutureWrapper<>(future);
@@ -46,4 +55,12 @@ public <T> TaskFuture<T> submit(final Runnable task, final T result, final Strin
4655
}
4756
}
4857

58+
/**
59+
* @param pool the pool to execute the task in
60+
* @return the ExecutorService for this pool
61+
*/
62+
protected ExecutorService getPool(final String pool) {
63+
return TaskMasterPoolUtil.getPool(pool);
64+
}
65+
4966
}

0 commit comments

Comments
 (0)