Class MapPool

java.lang.Object
dev.avelar.mapnik.MapPool
All Implemented Interfaces:
AutoCloseable

public final class MapPool extends Object implements 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.

  • Constructor Details

    • MapPool

      public MapPool(int size, int width, int height, Consumer<MapnikMap> setup)
      Create size maps of width by height pixels and run setup on each. If setup fails for one, the maps made so far are closed and the exception is thrown.
  • Method Details

    • builder

      public static MapPool.Builder builder(int size, int width, int height)
      Start building a pool of size maps of width by height pixels.
    • borrow

      public MapPool.Lease 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 throws MapPool.ExhaustedException), else as long as it takes.
    • borrow

      public MapPool.Lease borrow(long timeout, TimeUnit unit) throws TimeoutException
      Wait up to timeout for a free map; throws TimeoutException if none comes free.
      Throws:
      TimeoutException
    • withMap

      public <T> T withMap(Function<MapnikMap,T> work)
      Borrow a map, run work with it and give it back, also when work throws.
    • 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

      public MapPool.Stats 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 an IllegalStateException.
      Specified by:
      close in interface AutoCloseable