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 java.lang.Object implements ICachedLoaderJob<K>, java.lang.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 java.util.concurrent.ThreadPoolExecutor |
DEFAULT_DOWNLOAD_JOB_DISPATCHER |
protected static long |
DEFAULT_EXPIRE_TIME |
private java.util.concurrent.ThreadPoolExecutor |
downloadJobExecutor |
protected static long |
EXPIRE_TIME_SERVER_LIMIT |
private java.lang.Runnable |
finishTask |
private boolean |
force |
private java.util.Map<java.lang.String,java.lang.String> |
headers |
private static java.util.concurrent.ConcurrentMap<java.lang.String,java.util.Set<ICachedLoaderListener>> |
inProgress |
private static java.util.logging.Logger |
log |
protected long |
now |
private int |
readTimeout |
static IntegerProperty |
THREAD_LIMIT
maximum download threads that will be started
|
private static java.util.concurrent.ConcurrentMap<java.lang.String,java.lang.Boolean> |
useHead |
Constructor and Description |
---|
JCSCachedTileLoaderJob(org.apache.commons.jcs.access.behavior.ICacheAccess<K,V> cache,
int connectTimeout,
int readTimeout,
java.util.Map<java.lang.String,java.lang.String> headers) |
JCSCachedTileLoaderJob(org.apache.commons.jcs.access.behavior.ICacheAccess<K,V> cache,
int connectTimeout,
int readTimeout,
java.util.Map<java.lang.String,java.lang.String> headers,
java.util.concurrent.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
|
protected java.lang.String |
getServerKey() |
private java.net.HttpURLConnection |
getURLConnection(java.net.URL url) |
private java.net.URL |
getUrlNoException() |
void |
handleJobCancellation()
Marks this job as canceled
|
protected boolean |
isCacheElementValid() |
private boolean |
isCacheValidUsingHead() |
protected boolean |
isObjectLoadable() |
protected boolean |
isResponseLoadable(java.util.Map<java.lang.String,java.util.List<java.lang.String>> headerFields,
int responseCode,
byte[] raw)
Check if the object is loadable.
|
private boolean |
loadObject() |
protected CacheEntryAttributes |
parseHeaders(java.net.URLConnection urlConn) |
void |
run()
implements the main algorithm for fetching
|
void |
setFinishedTask(java.lang.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
private static final java.util.logging.Logger log
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 java.util.concurrent.ThreadPoolExecutor DEFAULT_DOWNLOAD_JOB_DISPATCHER
private static java.util.concurrent.ConcurrentMap<java.lang.String,java.util.Set<ICachedLoaderListener>> inProgress
private static java.util.concurrent.ConcurrentMap<java.lang.String,java.lang.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 java.util.Map<java.lang.String,java.lang.String> headers
private java.util.concurrent.ThreadPoolExecutor downloadJobExecutor
private java.lang.Runnable finishTask
private boolean force
public JCSCachedTileLoaderJob(org.apache.commons.jcs.access.behavior.ICacheAccess<K,V> cache, int connectTimeout, int readTimeout, java.util.Map<java.lang.String,java.lang.String> headers, java.util.concurrent.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, java.util.Map<java.lang.String,java.lang.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 java.io.IOException
ICachedLoaderJob
submit
in interface ICachedLoaderJob<K>
listener
- cache loader listenerforce
- true if the load should skip all the caches (local & remote)java.io.IOException
- on failure from getUrl() callprotected void executionFinished()
protected boolean isObjectLoadable()
protected boolean cacheAsEmpty()
protected java.lang.String getServerKey()
public void run()
ICachedLoaderJob
run
in interface java.lang.Runnable
run
in interface ICachedLoaderJob<K>
private void finishLoading(ICachedLoaderListener.LoadResult result)
protected boolean isCacheElementValid()
private boolean loadObject()
protected boolean isResponseLoadable(java.util.Map<java.lang.String,java.util.List<java.lang.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(java.net.URLConnection urlConn)
private java.net.HttpURLConnection getURLConnection(java.net.URL url) throws java.io.IOException
java.io.IOException
private boolean isCacheValidUsingHead() throws java.io.IOException
java.io.IOException
public void cancelOutstandingTasks()
public void setFinishedTask(java.lang.Runnable runnable)
runnable
- that will be executedpublic void handleJobCancellation()
private java.net.URL getUrlNoException()