K
- cache entry key typeV
- cache value type
Generic loader for HTTP based tiles. Uses custom attribute, to check, if entry has expired
according to HTTP headers sent with tile. If so, it tries to verify using Etags
or If-Modified-Since / Last-Modified.
If the tile is not valid, it will try to download it from remote service and put it
to cache. If remote server will fail it will try to use stale entry.
This class will keep only one Job running for specified tile. All others will just finish, but
listeners will be gathered and notified, once download job will be finishedpublic abstract class JCSCachedTileLoaderJob<K,V extends CacheEntry> extends Object implements ICachedLoaderJob<K>, Runnable
Modifier and Type | Field and Description |
---|---|
protected static long |
ABSOLUTE_EXPIRE_TIME_LIMIT |
protected CacheEntryAttributes |
attributes |
private org.apache.commons.jcs.access.behavior.ICacheAccess<K,V> |
cache |
protected V |
cacheData |
private org.apache.commons.jcs.engine.behavior.ICacheElement<K,V> |
cacheElement |
private int |
connectTimeout |
private static ThreadPoolExecutor |
DEFAULT_DOWNLOAD_JOB_DISPATCHER |
protected static long |
DEFAULT_EXPIRE_TIME |
private ThreadPoolExecutor |
downloadJobExecutor |
protected static long |
EXPIRE_TIME_SERVER_LIMIT |
private Runnable |
finishTask |
private boolean |
force |
private Map<String,String> |
headers |
private static ConcurrentMap<String,Set<ICachedLoaderListener>> |
inProgress |
private static Logger |
log |
protected long |
now |
private int |
readTimeout |
static IntegerProperty |
THREAD_LIMIT
maximum download threads that will be started
|
private static ConcurrentMap<String,Boolean> |
useHead |
Constructor and Description |
---|
JCSCachedTileLoaderJob(org.apache.commons.jcs.access.behavior.ICacheAccess<K,V> cache,
int connectTimeout,
int readTimeout,
Map<String,String> headers) |
JCSCachedTileLoaderJob(org.apache.commons.jcs.access.behavior.ICacheAccess<K,V> cache,
int connectTimeout,
int readTimeout,
Map<String,String> headers,
ThreadPoolExecutor downloadJobExecutor) |
Modifier and Type | Method and Description |
---|---|
protected boolean |
cacheAsEmpty()
Simple implementation.
|
void |
cancelOutstandingTasks()
TODO: move to JobFactory
cancels all outstanding tasks in the queue.
|
protected abstract V |
createCacheEntry(byte[] content) |
private void |
ensureCacheElement() |
protected void |
executionFinished()
This method is run when job has finished
|
private void |
finishLoading(ICachedLoaderListener.LoadResult result) |
V |
get()
fetches object from cache, or returns null when object is not found
|
private HttpClient |
getRequest(String requestMethod,
boolean noCache) |
protected String |
getServerKey() |
private URL |
getUrlNoException() |
void |
handleJobCancellation()
Marks this job as canceled
|
protected boolean |
isCacheElementValid() |
private boolean |
isCacheValidUsingHead() |
protected boolean |
isObjectLoadable() |
protected boolean |
isResponseLoadable(Map<String,List<String>> headerFields,
int responseCode,
byte[] raw)
Check if the object is loadable.
|
private boolean |
loadObject() |
protected CacheEntryAttributes |
parseHeaders(HttpClient.Response urlConn) |
void |
run()
implements the main algorithm for fetching
|
void |
setFinishedTask(Runnable runnable)
Sets a job, that will be run, when job will finish execution
|
void |
submit(ICachedLoaderListener listener,
boolean force)
Submit job for background fetch, and listener will be fed with value object
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
getCacheKey, getUrl
protected static final long DEFAULT_EXPIRE_TIME
protected static final long EXPIRE_TIME_SERVER_LIMIT
protected static final long ABSOLUTE_EXPIRE_TIME_LIMIT
public static final IntegerProperty THREAD_LIMIT
private static ThreadPoolExecutor DEFAULT_DOWNLOAD_JOB_DISPATCHER
private static ConcurrentMap<String,Set<ICachedLoaderListener>> inProgress
private static ConcurrentMap<String,Boolean> useHead
protected long now
private org.apache.commons.jcs.access.behavior.ICacheAccess<K,V extends CacheEntry> cache
private org.apache.commons.jcs.engine.behavior.ICacheElement<K,V extends CacheEntry> cacheElement
protected V extends CacheEntry cacheData
protected CacheEntryAttributes attributes
private int connectTimeout
private int readTimeout
private ThreadPoolExecutor downloadJobExecutor
private Runnable finishTask
private boolean force
public JCSCachedTileLoaderJob(org.apache.commons.jcs.access.behavior.ICacheAccess<K,V> cache, int connectTimeout, int readTimeout, Map<String,String> headers, ThreadPoolExecutor downloadJobExecutor)
cache
- cache instance that we will work onheaders
- HTTP headers to be sent together with requestreadTimeout
- when connecting to remote resourceconnectTimeout
- when connecting to remote resourcedownloadJobExecutor
- that will be executing the jobspublic JCSCachedTileLoaderJob(org.apache.commons.jcs.access.behavior.ICacheAccess<K,V> cache, int connectTimeout, int readTimeout, Map<String,String> headers)
cache
- cache instance that we will work onheaders
- HTTP headers to be sent together with requestreadTimeout
- when connecting to remote resourceconnectTimeout
- when connecting to remote resourceprivate void ensureCacheElement()
public V get()
ICachedLoaderJob
get
in interface ICachedLoaderJob<K>
public void submit(ICachedLoaderListener listener, boolean force) throws IOException
ICachedLoaderJob
submit
in interface ICachedLoaderJob<K>
listener
- cache loader listenerforce
- true if the load should skip all the caches (local & remote)IOException
- on failure from getUrl() callprotected void executionFinished()
protected boolean isObjectLoadable()
protected boolean cacheAsEmpty()
protected String getServerKey()
public void run()
ICachedLoaderJob
run
in interface Runnable
run
in interface ICachedLoaderJob<K>
private void finishLoading(ICachedLoaderListener.LoadResult result)
protected boolean isCacheElementValid()
private boolean loadObject()
protected boolean isResponseLoadable(Map<String,List<String>> headerFields, int responseCode, byte[] raw)
headerFields
- headers sent by serverresponseCode
- http status coderaw
- data read from serverprotected abstract V createCacheEntry(byte[] content)
protected CacheEntryAttributes parseHeaders(HttpClient.Response urlConn)
private HttpClient getRequest(String requestMethod, boolean noCache) throws IOException
IOException
private boolean isCacheValidUsingHead() throws IOException
IOException
public void cancelOutstandingTasks()
public void setFinishedTask(Runnable runnable)
runnable
- that will be executedpublic void handleJobCancellation()
private URL getUrlNoException()