|
| 1 | +package com.github.bordertech.taskmaster.service.impl; |
| 2 | + |
| 3 | +import com.github.bordertech.taskmaster.service.CallType; |
| 4 | +import com.github.bordertech.taskmaster.service.ResultHolder; |
| 5 | +import com.github.bordertech.taskmaster.service.ServiceAction; |
| 6 | +import com.github.bordertech.taskmaster.service.ServiceHelper; |
| 7 | +import com.github.bordertech.taskmaster.service.exception.AsyncServiceException; |
| 8 | +import com.github.bordertech.taskmaster.service.exception.ExceptionUtil; |
| 9 | +import com.github.bordertech.taskmaster.service.exception.RejectedServiceException; |
| 10 | +import java.io.Serializable; |
| 11 | +import javax.cache.Cache; |
| 12 | +import org.apache.commons.logging.Log; |
| 13 | +import org.apache.commons.logging.LogFactory; |
| 14 | + |
| 15 | +/** |
| 16 | + * Provide a starter implementation of ServiceHelper. |
| 17 | + */ |
| 18 | +public abstract class AbstractServiceHelper implements ServiceHelper { |
| 19 | + |
| 20 | + private static final Log LOGGER = LogFactory.getLog(AbstractServiceHelper.class); |
| 21 | + |
| 22 | + @Override |
| 23 | + public <S extends Serializable, T extends Serializable> ResultHolder<S, T> handleServiceCall(final S criteria, final ServiceAction<S, T> action) { |
| 24 | + // Check action provided |
| 25 | + if (action == null) { |
| 26 | + throw new IllegalArgumentException("No service action has been provided. "); |
| 27 | + } |
| 28 | + |
| 29 | + // Do service call |
| 30 | + try { |
| 31 | + T resp = action.service(criteria); |
| 32 | + return new ResultHolder(criteria, resp); |
| 33 | + } catch (Exception e) { |
| 34 | + return new ResultHolder(criteria, ExceptionUtil.getSerializableException(e)); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public <S extends Serializable, T extends Serializable> ResultHolder<S, T> handleServiceCallType(final Cache<String, ResultHolder> cache, |
| 40 | + final String cacheKey, final S criteria, final ServiceAction<S, T> action, final CallType callType) |
| 41 | + throws RejectedServiceException { |
| 42 | + return handleServiceCallType(cache, cacheKey, criteria, action, callType, null); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public <S extends Serializable, T extends Serializable> ResultHolder<S, T> handleServiceCallType(final Cache<String, ResultHolder> cache, |
| 47 | + final String cacheKey, final S criteria, final ServiceAction<S, T> action, final CallType callType, final String pool) |
| 48 | + throws RejectedServiceException { |
| 49 | + if (callType == null) { |
| 50 | + throw new IllegalArgumentException("Call type must be provided."); |
| 51 | + } |
| 52 | + // Refresh the Cache |
| 53 | + if (callType.isRefresh()) { |
| 54 | + cache.remove(cacheKey); |
| 55 | + } |
| 56 | + if (callType.isAsync()) { |
| 57 | + // ASync |
| 58 | + return handleAsyncServiceCall(cache, cacheKey, criteria, action, pool); |
| 59 | + } else { |
| 60 | + // Sync |
| 61 | + return handleCachedServiceCall(cache, cacheKey, criteria, action); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public <S extends Serializable, T extends Serializable> ResultHolder<S, T> handleCachedServiceCall(final Cache<String, ResultHolder> cache, |
| 67 | + final String cacheKey, final S criteria, final ServiceAction<S, T> action) { |
| 68 | + |
| 69 | + // Check cache and cache key provided |
| 70 | + if (cache == null) { |
| 71 | + throw new IllegalArgumentException("A cache must be provided."); |
| 72 | + } |
| 73 | + if (cacheKey == null) { |
| 74 | + throw new IllegalArgumentException("A cache key must be provided."); |
| 75 | + } |
| 76 | + |
| 77 | + // Check cache for result |
| 78 | + ResultHolder cached = cache.get(cacheKey); |
| 79 | + if (cached != null) { |
| 80 | + LOGGER.debug(buildCacheMessagePrefix(cache, cacheKey) + "Cached service call already in cache."); |
| 81 | + return cached; |
| 82 | + } |
| 83 | + // Do service call |
| 84 | + ResultHolder<S, T> result = handleServiceCall(criteria, action); |
| 85 | + // Save in the cache |
| 86 | + cache.put(cacheKey, result); |
| 87 | + |
| 88 | + return result; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public <S extends Serializable, T extends Serializable> ResultHolder<S, T> handleAsyncServiceCall(final Cache<String, ResultHolder> cache, |
| 93 | + final String cacheKey, final S criteria, final ServiceAction<S, T> action) throws RejectedServiceException { |
| 94 | + return handleAsyncServiceCall(cache, cacheKey, criteria, action, null); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public <S extends Serializable, T extends Serializable> ResultHolder<S, T> handleAsyncServiceCall(final Cache<String, ResultHolder> cache, |
| 99 | + final String cacheKey, final S criteria, final ServiceAction<S, T> action, final String pool) throws RejectedServiceException { |
| 100 | + |
| 101 | + // Check cache and cache key provided |
| 102 | + if (cache == null) { |
| 103 | + throw new IllegalArgumentException("A cache must be provided for async processing."); |
| 104 | + } |
| 105 | + if (cacheKey == null) { |
| 106 | + throw new IllegalArgumentException("A cache key must be provided for async processing. "); |
| 107 | + } |
| 108 | + // Check action provided |
| 109 | + if (action == null) { |
| 110 | + throw new IllegalArgumentException("No service action has been provided. "); |
| 111 | + } |
| 112 | + |
| 113 | + // Maybe already in the result cache |
| 114 | + ResultHolder cached = cache.get(cacheKey); |
| 115 | + if (cached != null) { |
| 116 | + LOGGER.debug(buildCacheMessagePrefix(cache, cacheKey) + "Async service call already in cache."); |
| 117 | + return cached; |
| 118 | + } |
| 119 | + |
| 120 | + // Check already processing |
| 121 | + if (isAsyncServiceRunning(cache, cacheKey, criteria, action, pool)) { |
| 122 | + LOGGER.debug(buildCacheMessagePrefix(cache, cacheKey) + "Async service call is already processing. A new service call will not be submitted."); |
| 123 | + try { |
| 124 | + return checkASyncResult(cache, cacheKey); |
| 125 | + } catch (AsyncServiceException e) { |
| 126 | + LOGGER.debug(buildCacheMessagePrefix(cache, cacheKey) + "Error checking Async service call. Will be ignored and submitted again."); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + // Submit the service call |
| 131 | + LOGGER.debug(buildCacheMessagePrefix(cache, cacheKey) + "Async service call will be submitted."); |
| 132 | + submitAsyncService(cache, cacheKey, criteria, action, pool); |
| 133 | + return null; |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * |
| 138 | + * @param cache the cache result holder |
| 139 | + * @param cacheKey the cache key being processed |
| 140 | + * @return the message suffix |
| 141 | + */ |
| 142 | + protected String buildCacheMessagePrefix(final Cache<String, ResultHolder> cache, final String cacheKey) { |
| 143 | + return "Cache [" + cache.getName() + "] and key [" + cacheKey + "]. "; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Check if the service is already running. |
| 148 | + * |
| 149 | + * @param cache the result holder cache |
| 150 | + * @param cacheKey the key for the result holder |
| 151 | + * @param criteria the criteria |
| 152 | + * @param action the service action |
| 153 | + * @param pool the service thread pool |
| 154 | + * @param <S> the criteria type |
| 155 | + * @param <T> the service response |
| 156 | + * @return true if task is already running |
| 157 | + */ |
| 158 | + protected abstract <S extends Serializable, T extends Serializable> boolean isAsyncServiceRunning(final Cache<String, ResultHolder> cache, |
| 159 | + final String cacheKey, final S criteria, final ServiceAction<S, T> action, final String pool); |
| 160 | + |
| 161 | + /** |
| 162 | + * Submit the async service call. |
| 163 | + * |
| 164 | + * @param cache the result holder cache |
| 165 | + * @param cacheKey the key for the result holder |
| 166 | + * @param criteria the criteria |
| 167 | + * @param action the service action |
| 168 | + * @param pool the service thread pool |
| 169 | + * @param <S> the criteria type |
| 170 | + * @param <T> the service response |
| 171 | + * @throws RejectedServiceException if the task cannot be scheduled for execution |
| 172 | + */ |
| 173 | + protected abstract <S extends Serializable, T extends Serializable> void submitAsyncService(final Cache<String, ResultHolder> cache, |
| 174 | + final String cacheKey, final S criteria, final ServiceAction<S, T> action, final String pool) |
| 175 | + throws RejectedServiceException; |
| 176 | + |
| 177 | +} |
0 commit comments