Skip to content

Commit 4cca07c

Browse files
Consolidate Configuration Properties
1 parent 293c319 commit 4cca07c

File tree

11 files changed

+361
-77
lines changed

11 files changed

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

3-
import com.github.bordertech.config.Config;
43
import com.github.bordertech.taskmaster.cache.CachingHelperProvider;
5-
import java.util.concurrent.TimeUnit;
64
import javax.cache.Cache;
75
import javax.cache.CacheManager;
86
import javax.cache.Caching;
@@ -19,30 +17,6 @@
1917
*/
2018
public class CachingHelperProviderDefault implements CachingHelperProvider {
2119

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) {
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-
4620
@Override
4721
public synchronized void closeCacheManager() {
4822
CachingProvider provider = Caching.getCachingProvider();
@@ -53,7 +27,12 @@ public synchronized void closeCacheManager() {
5327

5428
@Override
5529
public synchronized <K, V> Cache<K, V> getOrCreateCache(final String name, final Class<K> keyClass, final Class<V> valueClass) {
56-
return getOrCreateCache(name, keyClass, valueClass, getDefaultDuration());
30+
Cache<K, V> cache = Caching.getCache(name, keyClass, valueClass);
31+
if (cache == null) {
32+
Duration duration = CachingProperties.getCacheDuration(name);
33+
cache = getOrCreateCache(name, keyClass, valueClass, duration);
34+
}
35+
return cache;
5736
}
5837

5938
@Override
@@ -81,11 +60,4 @@ public synchronized <K, V> Cache<K, V> getOrCreateCache(final String name, final
8160
return cache;
8261
}
8362

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

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

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

3-
import com.github.bordertech.config.Config;
43
import com.github.bordertech.taskmaster.cache.CachingHelperProvider;
54
import java.net.URI;
65
import java.net.URISyntaxException;
@@ -27,7 +26,7 @@ public class CachingHelperProviderXmlConfig implements CachingHelperProvider {
2726

2827
static {
2928
// Load cache configfile location (default tm-cache.xml)
30-
String config = Config.getInstance().getString("bordertech.taskmaster.cache.config", "/tm-cache.xml");
29+
String config = CachingProperties.getConfigXmlLocation();
3130
LOGGER.info("Loading cache config [" + config + "].");
3231
URI uri;
3332
try {
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package com.github.bordertech.taskmaster.cache.impl;
2+
3+
import com.github.bordertech.config.Config;
4+
import java.util.concurrent.TimeUnit;
5+
import javax.cache.expiry.Duration;
6+
import org.apache.commons.configuration.Configuration;
7+
8+
/**
9+
* This class contains references to all constants and configuration options used by Caching.
10+
*/
11+
public final class CachingProperties {
12+
13+
private static final String DEFAULT_PREFIX = "bordertech.taskmaster.cache.default.";
14+
private static final String AMOUNT = "amount";
15+
private static final String UNIT = "unit";
16+
17+
/**
18+
* Private constructor for static class.
19+
*/
20+
private CachingProperties() {
21+
//No-impl
22+
}
23+
24+
/**
25+
* @return the config XML location
26+
*/
27+
public static String getConfigXmlLocation() {
28+
return get().getString("bordertech.taskmaster.cache.config", "/tm-cache.xml");
29+
}
30+
31+
/**
32+
* @return the default duration
33+
*/
34+
public static Duration getDefaultDuration() {
35+
return new Duration(getDefaultDurationUnit(), getDefaultDurationAmount());
36+
}
37+
38+
/**
39+
* @param cacheName the cache name
40+
* @return the default duration
41+
*/
42+
public static Duration getCacheDuration(final String cacheName) {
43+
return getCacheDuration(cacheName, getDefaultDuration());
44+
}
45+
46+
/**
47+
* @param cacheName the cache name
48+
* @param defaultDuration default duration
49+
* @return the cache duration
50+
*/
51+
public static Duration getCacheDuration(final String cacheName, final Duration defaultDuration) {
52+
TimeUnit unit = getCacheDurationUnit(cacheName, defaultDuration.getTimeUnit());
53+
Long amount = getCacheDurationAmount(cacheName, defaultDuration.getDurationAmount());
54+
return new Duration(unit, amount);
55+
}
56+
57+
/**
58+
* @return the default duration amount
59+
*/
60+
private static Long getDefaultDurationAmount() {
61+
return get().getLong(DEFAULT_PREFIX + AMOUNT, Long.valueOf("1800"));
62+
}
63+
64+
/**
65+
* @return the default time unit
66+
*/
67+
private static TimeUnit getDefaultDurationUnit() {
68+
String paramUnit = get().getString(DEFAULT_PREFIX + UNIT);
69+
return convertParamToTimeUnit(paramUnit, TimeUnit.SECONDS);
70+
}
71+
72+
/**
73+
* @param cacheName the cache name
74+
* @param defaultAmount the default amount
75+
* @return the cache duration amount
76+
*/
77+
private static Long getCacheDurationAmount(final String cacheName, final Long defaultAmount) {
78+
return get().getLong(getCachePrefix(cacheName) + AMOUNT, defaultAmount);
79+
}
80+
81+
/**
82+
* @param cacheName the cache name
83+
* @param defaultUnit the default unit
84+
* @return the cache time unit
85+
*/
86+
private static TimeUnit getCacheDurationUnit(final String cacheName, final TimeUnit defaultUnit) {
87+
String paramUnit = get().getString(getCachePrefix(cacheName) + UNIT);
88+
return convertParamToTimeUnit(paramUnit, defaultUnit);
89+
}
90+
91+
/**
92+
* @param cacheName the cache name
93+
* @return the cache parameter prefix
94+
*/
95+
private static String getCachePrefix(final String cacheName) {
96+
return "bordertech.taskmaster.cache." + cacheName + ".duration.";
97+
}
98+
99+
/**
100+
* Convert parameter to time unit.
101+
*
102+
* @param paramUnit the parameter value
103+
* @param defaultUnit the default unit
104+
* @return the time unit or null if o match
105+
*/
106+
private static TimeUnit convertParamToTimeUnit(final String paramUnit, final TimeUnit defaultUnit) {
107+
108+
if (paramUnit == null) {
109+
return defaultUnit;
110+
}
111+
112+
// Get unit
113+
switch (paramUnit) {
114+
case "d":
115+
return TimeUnit.DAYS;
116+
case "h":
117+
return TimeUnit.HOURS;
118+
case "m":
119+
return TimeUnit.MINUTES;
120+
case "s":
121+
return TimeUnit.SECONDS;
122+
case "mi":
123+
return TimeUnit.MILLISECONDS;
124+
case "n":
125+
return TimeUnit.NANOSECONDS;
126+
default:
127+
return defaultUnit;
128+
}
129+
}
130+
131+
/**
132+
* Shorthand convenience method to get the Configuration instance.
133+
*
134+
* @return the Configuration instance.
135+
*/
136+
private static Configuration get() {
137+
return Config.getInstance();
138+
}
139+
140+
}

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

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

3-
import com.github.bordertech.config.Config;
43
import com.github.bordertech.taskmaster.TaskFuture;
54
import com.github.bordertech.taskmaster.cache.CachingHelper;
65
import com.github.bordertech.taskmaster.exception.TaskMasterException;
@@ -29,18 +28,21 @@ public class TaskFutureWrapper<T extends Serializable> implements TaskFuture<T>
2928
private final String id = UUID.randomUUID().toString();
3029

3130
static {
32-
// Get default duration
33-
Long duration = Config.getInstance().getLong("bordertech.taskmaster.future.cache.duration", Long.valueOf("300"));
31+
// Get future task cache name
32+
String name = TaskMasterProperties.getFutureTaskCacheName();
33+
34+
// Get cache duration
35+
Duration duration = TaskMasterProperties.getFutureTaskCacheDuration();
3436

3537
// Setup cache config
3638
MutableConfiguration<String, Future> config = new MutableConfiguration<>();
3739
config.setTypes(String.class, Future.class);
38-
config.setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(new Duration(TimeUnit.SECONDS, duration)));
40+
config.setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(duration));
3941
// No need to serialize the result (Future is not serializable)
4042
config.setStoreByValue(false);
4143

4244
// Create Cache
43-
CACHE = CachingHelper.getOrCreateCache("bordertech-tm-future-task", String.class, Future.class, config);
45+
CACHE = CachingHelper.getOrCreateCache(name, String.class, Future.class, config);
4446
}
4547

4648
/**

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

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

3-
import com.github.bordertech.config.Config;
43
import com.github.bordertech.taskmaster.exception.TaskMasterException;
54
import java.util.HashMap;
65
import java.util.Map;
@@ -22,18 +21,15 @@ public final class TaskMasterPoolUtil {
2221

2322
private static final Log LOGGER = LogFactory.getLog(TaskMasterPoolUtil.class);
2423
private static final Map<String, ExecutorService> THREAD_POOLS = new HashMap<>();
25-
private static final String TP_PARAM_PREFIX = "bordertech.taskmaster.pool.";
26-
private static final int DEFAULT_MAX_THREADS = 20;
27-
private static final int DEFAULT_QUEUE_LENGTH = 0;
2824

2925
/**
3026
* Default thread pool name.
3127
*/
32-
public static final String DEFAULT_POOL = Config.getInstance().getString(TP_PARAM_PREFIX + "default", "default");
28+
private static final String DEFAULT_POOL = TaskMasterProperties.getDefaultThreadPoolName();
3329

3430
static {
3531
// Load thread pools
36-
String[] pools = Config.getInstance().getStringArray(TaskMasterPoolUtil.TP_PARAM_PREFIX + "names");
32+
String[] pools = TaskMasterProperties.getThreadPools();
3733
for (String pool : pools) {
3834
THREAD_POOLS.put(pool, TaskMasterPoolUtil.buildPool(pool));
3935
}
@@ -61,18 +57,15 @@ private static ExecutorService buildPool(final String pool) {
6157
// http://www.nurkiewicz.com/2014/11/executorservice-10-tips-and-tricks.html
6258

6359
// Get the pool type - defaults to cached
64-
String type = Config.getInstance().getString(TP_PARAM_PREFIX + pool + ".type", "cached");
60+
String type = TaskMasterProperties.getThreadPoolType(pool);
6561
switch (type.toLowerCase()) {
6662
case "single":
6763
return Executors.newSingleThreadExecutor();
6864
case "fixed":
6965
// Number of fixed threads
70-
int max = Config.getInstance().getInt(TP_PARAM_PREFIX + pool + ".max", DEFAULT_MAX_THREADS);
71-
if (max < 1) {
72-
max = DEFAULT_MAX_THREADS;
73-
}
66+
int max = TaskMasterProperties.getPoolMaxThreads(pool);
7467
// Length of pending queue
75-
int queue = Config.getInstance().getInt(TP_PARAM_PREFIX + pool + ".queue", DEFAULT_QUEUE_LENGTH);
68+
int queue = TaskMasterProperties.getPoolPendingQueueLength(pool);
7669
// Create executable with the appropriate queue type
7770
BlockingQueue<Runnable> blkQueue;
7871
if (queue < 0) {

0 commit comments

Comments
 (0)