Package dev.avelar.mapnik
Class MapPool
java.lang.Object
dev.avelar.mapnik.MapPool
- All Implemented Interfaces:
AutoCloseable
A fixed number of identical maps shared by many threads. A
MapnikMap must not be used by two
threads at once, so a server that renders on request threads borrows one map, draws, and gives it
back. Every map is set up the same way by the function you pass in, usually by loading the style.
MapPool pool = new MapPool(8, 256, 256, map -> map.load(Paths.get("style.xml")));
byte[] png = pool.withMap(map -> {
map.zoomToBox(bounds);
return map.renderToPng();
});
pool.close();
For a server, use builder(int, int, int) to also set how long a request may wait for a map, and a warm-up
that draws once on each map before the pool is used. stats() reports how busy the pool is.
A map returns to the pool as you left it. Set size and bounds for each use, and do not change the
styles or layers inside withMap(java.util.function.Function<dev.avelar.mapnik.MapnikMap, T>), unless you do the same on every use.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classBuilds a pool; seebuilder(int, int, int).static final classThrown byborrow()andwithMap(java.util.function.Function<dev.avelar.mapnik.MapnikMap, T>)when no map came free within the pool's maximum wait.final classA map borrowed from the pool.static final classHow busy a pool has been since it was created. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintThe number of maps free right now.borrow()Wait for a free map.Wait up totimeoutfor a free map; throwsTimeoutExceptionif none comes free.static MapPool.Builderbuilder(int size, int width, int height) Start building a pool ofsizemaps ofwidthbyheightpixels.voidclose()Close the free maps now and the borrowed ones as they come back.intsize()The number of maps in the pool, borrowed or not.stats()A snapshot of how busy the pool has been.<T> TBorrow a map, runworkwith it and give it back, also whenworkthrows.
-
Constructor Details
-
MapPool
Createsizemaps ofwidthbyheightpixels and runsetupon each. If setup fails for one, the maps made so far are closed and the exception is thrown.
-
-
Method Details
-
builder
Start building a pool ofsizemaps ofwidthbyheightpixels. -
borrow
Wait for a free map. Close the lease when done, ideally with try-with-resources. Waits as long as the pool's maximum wait if it has one (then throwsMapPool.ExhaustedException), else as long as it takes. -
borrow
Wait up totimeoutfor a free map; throwsTimeoutExceptionif none comes free.- Throws:
TimeoutException
-
withMap
Borrow a map, runworkwith it and give it back, also whenworkthrows. -
size
public int size()The number of maps in the pool, borrowed or not. -
available
public int available()The number of maps free right now. -
stats
A snapshot of how busy the pool has been. -
close
public void close()Close the free maps now and the borrowed ones as they come back. Do not close while threads still need the pool: they get anIllegalStateException.- Specified by:
closein interfaceAutoCloseable
-