source: josm/trunk/src/org/openstreetmap/josm/data/imagery/TMSCachedTileLoader.java@ 8734

Last change on this file since 8734 was 8734, checked in by simon04, 9 years ago

see #11843 - Give all started threads sensible names

Utils#newThreadFactory creates a ThreadFactory to be used when
obtaining a new Executor via Executors.new….

  • Property svn:eol-style set to native
File size: 5.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.imagery;
3
4import java.io.IOException;
5import java.util.Map;
6import java.util.concurrent.ThreadPoolExecutor;
7import java.util.concurrent.TimeUnit;
8
9import org.apache.commons.jcs.access.behavior.ICacheAccess;
10import org.openstreetmap.gui.jmapviewer.Tile;
11import org.openstreetmap.gui.jmapviewer.interfaces.CachedTileLoader;
12import org.openstreetmap.gui.jmapviewer.interfaces.TileCache;
13import org.openstreetmap.gui.jmapviewer.interfaces.TileJob;
14import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
15import org.openstreetmap.gui.jmapviewer.interfaces.TileLoaderListener;
16import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
17import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry;
18import org.openstreetmap.josm.data.cache.HostLimitQueue;
19import org.openstreetmap.josm.data.preferences.IntegerProperty;
20import org.openstreetmap.josm.tools.Utils;
21
22/**
23 * @author Wiktor Niesiobędzki
24 *
25 * Wrapper class that bridges between JCS cache and Tile Loaders
26 *
27 */
28public class TMSCachedTileLoader implements TileLoader, CachedTileLoader, TileCache {
29
30 protected final ICacheAccess<String, BufferedImageCacheEntry> cache;
31 protected final int connectTimeout;
32 protected final int readTimeout;
33 protected final Map<String, String> headers;
34 protected final TileLoaderListener listener;
35
36 /**
37 * overrides the THREAD_LIMIT in superclass, as we want to have separate limit and pool for TMS
38 */
39
40 public static final IntegerProperty THREAD_LIMIT = new IntegerProperty("imagery.tms.tmsloader.maxjobs", 25);
41
42 /**
43 * Limit definition for per host concurrent connections
44 */
45 public static final IntegerProperty HOST_LIMIT = new IntegerProperty("imagery.tms.tmsloader.maxjobsperhost", 6);
46
47
48 /**
49 * separate from JCS thread pool for TMS loader, so we can have different thread pools for default JCS
50 * and for TMS imagery
51 */
52 private static ThreadPoolExecutor DEFAULT_DOWNLOAD_JOB_DISPATCHER = getNewThreadPoolExecutor("TMS-downloader-%d");
53
54
55 private ThreadPoolExecutor downloadExecutor = DEFAULT_DOWNLOAD_JOB_DISPATCHER;
56
57 /**
58 * Constructor
59 * @param listener called when tile loading has finished
60 * @param cache of the cache
61 * @param connectTimeout to remote resource
62 * @param readTimeout to remote resource
63 * @param headers HTTP headers to be sent along with request
64 * @throws IOException when cache initialization fails
65 */
66 public TMSCachedTileLoader(TileLoaderListener listener, ICacheAccess<String, BufferedImageCacheEntry> cache,
67 int connectTimeout, int readTimeout, Map<String, String> headers) throws IOException {
68 this.cache = cache;
69 this.connectTimeout = connectTimeout;
70 this.readTimeout = readTimeout;
71 this.headers = headers;
72 this.listener = listener;
73 }
74
75 /**
76 * @param nameFormat see {@link Utils#newThreadFactory(String, int)}
77 * @param workers number of worker thread to keep
78 * @return new ThreadPoolExecutor that will use a @see HostLimitQueue based queue
79 */
80 public static ThreadPoolExecutor getNewThreadPoolExecutor(String nameFormat, int workers) {
81 return new ThreadPoolExecutor(
82 workers, // keep the thread number constant
83 workers, // do not this number of threads
84 30, // keepalive for thread
85 TimeUnit.SECONDS,
86 new HostLimitQueue(HOST_LIMIT.get().intValue()),
87 Utils.newThreadFactory(nameFormat, Thread.NORM_PRIORITY)
88 );
89 }
90
91 /**
92 * @param name name of threads
93 * @return new ThreadPoolExecutor that will use a @see HostLimitQueue based queue, with default number of threads
94 */
95 public static ThreadPoolExecutor getNewThreadPoolExecutor(String name) {
96 return getNewThreadPoolExecutor(name, THREAD_LIMIT.get().intValue());
97 }
98
99 @Override
100 public TileJob createTileLoaderJob(Tile tile) {
101 return new TMSCachedTileLoaderJob(listener, tile, cache,
102 connectTimeout, readTimeout, headers, getDownloadExecutor());
103 }
104
105 @Override
106 public void clearCache(TileSource source) {
107 this.cache.remove(source.getName() + ":");
108 }
109
110 @Override
111 public Tile getTile(TileSource source, int x, int y, int z) {
112 return createTileLoaderJob(new Tile(source, x, y, z)).getTile();
113 }
114
115 @Override
116 public void addTile(Tile tile) {
117 createTileLoaderJob(tile).getTile();
118 }
119
120 @Override
121 public int getTileCount() {
122 return 0;
123 }
124
125 @Override
126 public void clear() {
127 cache.clear();
128 }
129
130 /**
131 * @return cache statistics as string
132 */
133 public String getStats() {
134 return cache.getStats();
135 }
136
137 /**
138 * cancels all outstanding tasks in the queue. This rollbacks the state of the tiles in the queue
139 * to loading = false / loaded = false
140 */
141 public void cancelOutstandingTasks() {
142 for (Runnable r: downloadExecutor.getQueue()) {
143 if (downloadExecutor.remove(r) && r instanceof TMSCachedTileLoaderJob) {
144 ((TMSCachedTileLoaderJob) r).handleJobCancellation();
145 }
146 }
147 }
148
149 /**
150 * Sets the download executor that will be used to download tiles instead of default one.
151 * You can use {@link #getNewThreadPoolExecutor} to create a new download executor with separate
152 * queue from default.
153 *
154 * @param downloadExecutor download executor that will be used to download tiles
155 */
156 public void setDownloadExecutor(ThreadPoolExecutor downloadExecutor) {
157 this.downloadExecutor = downloadExecutor;
158 }
159
160 /**
161 * @return download executor that is used by this factory
162 */
163 public ThreadPoolExecutor getDownloadExecutor() {
164 return downloadExecutor;
165 }
166}
Note: See TracBrowser for help on using the repository browser.