Skip to content

Commit d03fb46

Browse files
Initial refactor of ServiceHelper and add helper methods to TaskMaster
1 parent 6d8a908 commit d03fb46

File tree

25 files changed

+788
-645
lines changed

25 files changed

+788
-645
lines changed

pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
<properties>
1919
<bt.qa.skip>false</bt.qa.skip>
20+
<dependency-check.skip>true</dependency-check.skip>
2021
</properties>
2122

2223
<description>
@@ -98,7 +99,7 @@
9899
<dependency>
99100
<groupId>org.ehcache</groupId>
100101
<artifactId>ehcache</artifactId>
101-
<version>3.7.1</version>
102+
<version>3.6.3</version>
102103
</dependency>
103104

104105
<!-- Common Lang3 -->

taskmaster-cache-ehcache/src/main/java/com/github/bordertech/taskmaster/ehcache/CachingHelperProviderEhCache.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,8 @@ public CachingHelperProviderEhCache() {
4040
}
4141

4242
@Override
43-
protected synchronized <K, V> Cache<K, V> handleGetCache(final String name, final Class<K> keyClass,
44-
final Class<V> valueClass) {
45-
Cache<K, V> cache = super.handleGetCache(name, keyClass, valueClass);
43+
public <K, V> Cache<K, V> getOrCreateCache(final String name, final Class<K> keyClass, final Class<V> valueClass) {
44+
Cache<K, V> cache = super.getOrCreateCache(name, keyClass, valueClass);
4645
if (!caches.containsKey(name)) {
4746
configCachePropertyValues(name, cache);
4847
caches.put(name, new ImmutableTriple(name, keyClass, valueClass));

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import com.github.bordertech.didums.Didums;
44
import com.github.bordertech.taskmaster.cache.impl.CachingHelperProviderDefault;
5+
import javax.cache.Cache;
6+
import javax.cache.configuration.Configuration;
7+
import javax.cache.expiry.Duration;
58

69
/**
710
* Caching helper based on JSR 107.
@@ -28,4 +31,55 @@ public static CachingHelperProvider getProvider() {
2831
return PROVIDER;
2932
}
3033

34+
/**
35+
* Close and release the cache resources.
36+
*/
37+
public static void closeCacheManager() {
38+
PROVIDER.closeCacheManager();
39+
}
40+
41+
/**
42+
* Create a cache with the default configuration.
43+
*
44+
* @param name the cache name
45+
* @param keyClass the key class type
46+
* @param valueClass the value class type
47+
* @param <K> the cache entry key type
48+
* @param <V> the cache entry value value
49+
* @return the cache instance
50+
*/
51+
public static <K, V> Cache<K, V> getOrCreateCache(final String name, final Class<K> keyClass, final Class<V> valueClass) {
52+
return PROVIDER.getOrCreateCache(name, keyClass, valueClass);
53+
}
54+
55+
/**
56+
* Create a cache with the specified duration.
57+
*
58+
* @param name the cache name
59+
* @param keyClass the key class type
60+
* @param valueClass the value class type
61+
* @param duration the cache entry duration
62+
* @param <K> the cache entry key type
63+
* @param <V> the cache entry value value
64+
* @return the cache instance
65+
*/
66+
public static <K, V> Cache<K, V> getOrCreateCache(final String name, final Class<K> keyClass, final Class<V> valueClass, final Duration duration) {
67+
return PROVIDER.getOrCreateCache(name, keyClass, valueClass, duration);
68+
}
69+
70+
/**
71+
* Create a cache with the specified configuration.
72+
*
73+
* @param <K> the cache entry key type
74+
* @param <V> the cache entry value value
75+
* @param name the cache name
76+
* @param keyClass the key class type
77+
* @param valueClass the value class type
78+
* @param config the cache configuration
79+
* @return the cache instance
80+
*/
81+
public static <K, V> Cache<K, V> getOrCreateCache(final String name, final Class<K> keyClass, final Class<V> valueClass, final Configuration<K, V> config) {
82+
return PROVIDER.getOrCreateCache(name, keyClass, valueClass, config);
83+
}
84+
3185
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ public interface CachingHelperProvider {
1919
*/
2020
void closeCacheManager();
2121

22+
/**
23+
* Create a cache with the default configuration.
24+
*
25+
* @param name the cache name
26+
* @param keyClass the key class type
27+
* @param valueClass the value class type
28+
* @param <K> the cache entry key type
29+
* @param <V> the cache entry value value
30+
* @return the cache instance
31+
*/
32+
<K, V> Cache<K, V> getOrCreateCache(final String name, final Class<K> keyClass, final Class<V> valueClass);
33+
2234
/**
2335
* Create a cache with the specified duration.
2436
*

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

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

3+
import com.github.bordertech.config.Config;
34
import com.github.bordertech.taskmaster.cache.CachingHelperProvider;
5+
import java.util.concurrent.TimeUnit;
46
import javax.cache.Cache;
57
import javax.cache.CacheManager;
68
import javax.cache.Caching;
@@ -17,6 +19,30 @@
1719
*/
1820
public class CachingHelperProviderDefault implements CachingHelperProvider {
1921

22+
private static final Duration DEFAULT_DURATION;
23+
24+
static {
25+
Long interval = Config.getInstance().getLong("bordertech.taskmaster.caching.default.duration", Long.valueOf("1800"));
26+
String unitType = Config.getInstance().getString("bordertech.taskmaster.caching.default.unit", "s");
27+
28+
// Get unit
29+
TimeUnit unit;
30+
switch (unitType.toLowerCase()) {
31+
case "d":
32+
unit = TimeUnit.DAYS;
33+
break;
34+
case "h":
35+
unit = TimeUnit.HOURS;
36+
break;
37+
case "m":
38+
unit = TimeUnit.MINUTES;
39+
break;
40+
default:
41+
unit = TimeUnit.SECONDS;
42+
}
43+
DEFAULT_DURATION = new Duration(unit, interval);
44+
}
45+
2046
@Override
2147
public synchronized void closeCacheManager() {
2248
CachingProvider provider = Caching.getCachingProvider();
@@ -25,6 +51,11 @@ public synchronized void closeCacheManager() {
2551
}
2652
}
2753

54+
@Override
55+
public <K, V> Cache<K, V> getOrCreateCache(final String name, final Class<K> keyClass, final Class<V> valueClass) {
56+
return getOrCreateCache(name, keyClass, valueClass, getDefaultDuration());
57+
}
58+
2859
@Override
2960
public synchronized <K, V> Cache<K, V> getOrCreateCache(final String name, final Class<K> keyClass,
3061
final Class<V> valueClass, final Duration duration) {
@@ -50,4 +81,11 @@ public synchronized <K, V> Cache<K, V> getOrCreateCache(final String name, final
5081
return cache;
5182
}
5283

84+
/**
85+
* @return the default duration
86+
*/
87+
protected Duration getDefaultDuration() {
88+
return DEFAULT_DURATION;
89+
}
90+
5391
}

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

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,33 +51,24 @@ public synchronized void closeCacheManager() {
5151
}
5252
}
5353

54+
@Override
55+
public <K, V> Cache<K, V> getOrCreateCache(final String name, final Class<K> keyClass, final Class<V> valueClass) {
56+
// Return the pre configured cache
57+
return MANAGER.getCache(name, keyClass, valueClass);
58+
}
59+
5460
@Override
5561
public synchronized <K, V> Cache<K, V> getOrCreateCache(final String name, final Class<K> keyClass,
5662
final Class<V> valueClass, final Duration duration) {
5763
// Ignore duration
58-
return handleGetCache(name, keyClass, valueClass);
64+
return getOrCreateCache(name, keyClass, valueClass);
5965
}
6066

6167
@Override
6268
public synchronized <K, V> Cache<K, V> getOrCreateCache(final String name, final Class<K> keyClass,
6369
final Class<V> valueClass, final Configuration<K, V> config) {
6470
// Ignore config
65-
return handleGetCache(name, keyClass, valueClass);
66-
}
67-
68-
/**
69-
* Get the pre-configured cache.
70-
*
71-
* @param name the cache name
72-
* @param keyClass the cache key class
73-
* @param valueClass the cache entry class
74-
* @return the cache instance
75-
* @param <K> the cache key type
76-
* @param <V> the cache entry type
77-
*/
78-
protected synchronized <K, V> Cache<K, V> handleGetCache(final String name, final Class<K> keyClass,
79-
final Class<V> valueClass) {
80-
return MANAGER.getCache(name, keyClass, valueClass);
71+
return getOrCreateCache(name, keyClass, valueClass);
8172
}
8273

8374
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public void contextInitialized(final ServletContextEvent sce) {
1616

1717
@Override
1818
public void contextDestroyed(final ServletContextEvent sce) {
19-
CachingHelper.getProvider().closeCacheManager();
19+
CachingHelper.closeCacheManager();
2020
}
2121

2222
}

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

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

33
import com.github.bordertech.didums.Didums;
4+
import com.github.bordertech.taskmaster.exception.RejectedTaskException;
45
import com.github.bordertech.taskmaster.impl.TaskMasterProviderExecutorService;
56

67
/**
@@ -38,4 +39,56 @@ public static TaskMasterProvider getProvider() {
3839
return PROVIDER;
3940
}
4041

42+
/**
43+
* Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be
44+
* accepted. Invocation has no additional effect if already shut down.
45+
*
46+
* @throws SecurityException if a security manager exists and shutting down this ExecutorService may manipulate
47+
* threads that the caller is not permitted to modify because it does not hold permission
48+
*/
49+
public static void shutdown() {
50+
PROVIDER.shutdown();
51+
}
52+
53+
/**
54+
* Attempts to stop all actively executing tasks, halts the processing of waiting tasks.
55+
*
56+
* @throws SecurityException if a security manager exists and shutting down this ExecutorService may manipulate
57+
* threads that the caller is not permitted to modify because it does not hold permission
58+
*/
59+
public static void shutdownNow() {
60+
PROVIDER.shutdownNow();
61+
}
62+
63+
/**
64+
* Submits a Runnable task for execution and returns a Future representing that task. The Future's <tt>get</tt>
65+
* method will return the given result upon successful completion.
66+
* <p>
67+
* Uses the default thread pool.
68+
* </p>
69+
*
70+
* @param <T> the type for the future
71+
* @param task the task to submit
72+
* @param result the result to return
73+
* @return a Future representing pending completion of the task
74+
* @throws RejectedTaskException if the task cannot be scheduled for execution
75+
*/
76+
public static <T> TaskFuture<T> submit(final Runnable task, final T result) throws RejectedTaskException {
77+
return PROVIDER.submit(task, result);
78+
}
79+
80+
/**
81+
* Submits a Runnable task for execution and returns a Future representing that task. The Future's <tt>get</tt>
82+
* method will return the given result upon successful completion.
83+
*
84+
* @param <T> the type for the future
85+
* @param task the task to submit
86+
* @param result the result to return
87+
* @param pool the thread pool name, or null if no pool
88+
* @return a Future representing pending completion of the task
89+
* @throws RejectedTaskException if the task cannot be scheduled for execution
90+
*/
91+
public static <T> TaskFuture<T> submit(final Runnable task, final T result, final String pool) throws RejectedTaskException {
92+
return PROVIDER.submit(task, result, pool);
93+
}
4194
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class TaskFutureWrapper<T> implements TaskFuture<T> {
3939
config.setStoreByValue(false);
4040

4141
// Create Cache
42-
CACHE = CachingHelper.getProvider().getOrCreateCache("bordertech-tm-future-task", String.class, Future.class, config);
42+
CACHE = CachingHelper.getOrCreateCache("bordertech-tm-future-task", String.class, Future.class, config);
4343
}
4444

4545
/**

taskmaster-core/src/main/java/com/github/bordertech/taskmaster/servlet/TaskContextListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@ public void contextInitialized(final ServletContextEvent servletContextEvent) {
4141
@Override
4242
public void contextDestroyed(final ServletContextEvent servletContextEvent) {
4343
// Shutdown the task master
44-
TaskMaster.getProvider().shutdown();
44+
TaskMaster.shutdown();
4545
}
4646
}

0 commit comments

Comments
 (0)