Package stormpot

Stormpot

See: Description

Package stormpot Description

Stormpot

Stormpot is a generic, thread-safe and fast object pooling library.

The object pools themselves implement the Pool interface, or one or both of the LifecycledPool or ResizablePool interfaces, or even the LifecycledResizablePool interface. The things you actually want to pool must all implement the Poolable interface, and you must also provide an implementation of the Allocator interface as a factory to create your pooled objects.

Simplest Possible Usage

Stormpot has an invasive API which means that there is a minimum of things your code needs to do to use it. However, the requirements are quite benign, as this section is all about showing you. The objects that you manage with a Stormpot pool needs to implement the Poolable interface. The absolute minimum amount of code required for its implementation is this:

// MyPoolable.java - minimum Poolable implementation
import stormpot.Poolable;
import stormpot.Slot;

public class MyPoolable implements Poolable {
  private final Slot slot;
  public MyPoolable(Slot slot) {
    this.slot = slot;
  }

  public void release() {
    slot.release(this);
  }
}

The object in essence just needs to keep its Slot instance around, and give itself as a parameter to the Slot#release method. Now that we have a class of objects to pool, we need some way to tell Stormpot how to create them. We do this by implementing the Allocator interface:

// MyAllocator.java - minimum Allocator implementation
import stormpot.Allocator;
import stormpot.Slot;

public class MyAllocator implements Allocator<MyPoolable> {
  public MyPoolable allocate(Slot slot) throws Exception {
    return new MyPoolable(slot);
  }

  public void deallocate(MyPoolable poolable) throws Exception {
    // Nothing to do here
    // But it's a perfect place to close sockets, files, etc.
  }
}

That’s it. Given a slot, create a MyPoolable. Or given a MyPoolable, deallocate it. And that is actually all the parts we need to start using Stormpot. All that is left is a little bit of configuration:

    MyAllocator allocator = new MyAllocator();
    Config<MyPoolable> config = new Config<MyPoolable>().setAllocator(allocator);
    Pool<MyPoolable> pool = new BlazePool<MyPoolable>(config);
    Timeout timeout = new Timeout(1, TimeUnit.SECONDS);

    MyPoolable object = pool.claim(timeout);
    try {
      // Do stuff with 'object'.
      // Note that 'claim' will return 'null' if it times out!
    } finally {
      if (object != null) {
        object.release();
      }
    }

Create a Config object and set the allocator, then create a pool with the configuration and off we go! The blocking methods Pool#claim and Completion#await both take Timeout objects as arguments. These are immutable, and can easily be put in static final constants. Note that claim returns null if the timeout elapses before an object can be claimed. Also, using try-finally is a great way to make sure that you don’t forget to release the objects back into the pool. Stormpot cannot prevent objects from leaking in user code, so if you lose the reference to an object that hasn’t been released back into the pool, it will not come back on its own. Leaked objects can also cause problems with shutting the pool down, because the shut down procedure does not complete until all allocated objects are deallocated. So if you have leaked objects, shutting the pool down will never complete normally. You can still just halt the JVM, though.

Copyright © 2011-2014–2015. All rights reserved.