bbottema / generic-object-pool Goto Github PK
View Code? Open in Web Editor NEWA flexible thread-safe generic object pool
A flexible thread-safe generic object pool
Hey there, found your projecto in the Stormpot issues. Looks great. However, it looks like whatever version that's currently deployed depends on a jacoco agent to run. The only way I could get it up and going was to either include the agent in every project, or fork your project and remove the build plugin. Here's my fork:
https://github.com/regbo/generic-object-pool-jacoco-removed
The message I am getting is (can't bring up full stack trace at the moment):
Caused by: java.lang.ClassNotFoundException: org.jacoco.agent.rt.internal_c13123e.Offline+
I'm not familiar with jacoco but apparently there is a way to deploy "releases" and when you don't the above error may occur.
See bbottema/simple-java-mail#387
Deallocation is handled by 3rd party, but if it throws an exception, it breaks the pool's cleanup cycle and leaves threads in the air never deallocating them after the exception occurred once.
As mentioned from another issue I came here from Stormpot. I have a resource that takes a while to spin up, and it's best done in parallel. It looks like your project does it all on one thread.
To overcome this I created two wrapper classes below. The "map" methods allow me to take something like a CompletableFuture, and make sure all claims from a pool are mapped. That way I can allocate a future and block elsewhere if need be.
If possible, I think adding a method like this to your GenericObjectPool class would be great:
default <U> Pool<U, X> map(Function<? super X, U> mapper);
Here are my wrappers (both use immutables):
@Value.Immutable
@Value.Style(stagedBuilder = true)
public interface Pool<T, X> {
GenericObjectPool<X> getGenericObjectPool();
Function<? super X, T> getMapper();
default <U> Pool<U, X> map(Function<? super X, U> mapper) {
return ImmutablePool.<U, X>builder().genericObjectPool(getGenericObjectPool()).mapper(mapper).build();
}
/**
* @param timeout
* @return
* @throws InterruptedException
* @throws IllegalStateException
* @see org.bbottema.genericobjectpool.GenericObjectPool#claim(org.bbottema.genericobjectpool.util.Timeout)
*/
@SuppressWarnings("resource")
@Nullable
default Poolo<T> claim(Timeout timeout) throws InterruptedException, IllegalStateException {
var po = getGenericObjectPool().claim(timeout);
if (po == null)
return null;
var poolo = new Poolo<X>() {
@Override
public PoolableObject<?> getPoolableObject() {
return po;
}
@Override
public X getAllocatedObject() {
return po.getAllocatedObject();
}
};
return poolo.map(getMapper());
}
public static <X> ImmutablePool<X, X> build(GenericObjectPool<X> genericObjectPool) {
return build(genericObjectPool, Function.identity());
}
public static <T, X> ImmutablePool<T, X> build(GenericObjectPool<X> genericObjectPool,
Function<? super X, T> mapper) {
return ImmutablePool.<T, X>builder().genericObjectPool(genericObjectPool).mapper(mapper).build();
}
public static abstract class Abs<T, X> implements Pool<T, X> {
private final GenericObjectPool<X> genericObjectPool;
public Abs(GenericObjectPool<X> genericObjectPool) {
super();
this.genericObjectPool = Objects.requireNonNull(genericObjectPool);
}
@Override
public GenericObjectPool<X> getGenericObjectPool() {
return genericObjectPool;
}
}
public static class Impl<X> extends Pool.Abs<X, X> {
public Impl(GenericObjectPool<X> genericObjectPool) {
super(genericObjectPool);
}
@Override
public Function<? super X, X> getMapper() {
return Function.identity();
}
}
// *****************************delegates
And
@Value.Immutable
@Value.Style(stagedBuilder = true)
public interface Poolo<T> extends AutoCloseable {
PoolableObject<?> getPoolableObject();
/**
* @return
* @see org.bbottema.genericobjectpool.PoolableObject#getAllocatedObject()
*/
@Nullable
T getAllocatedObject();
@SuppressWarnings("unchecked")
default <U> Poolo<U> map(Function<? super T, U> mapper) {
Objects.requireNonNull(mapper);
var allocatedObject = getAllocatedObject();
var allocatedObjectUpdated = mapper.apply(allocatedObject);
if (allocatedObject == allocatedObjectUpdated)
return (Poolo<U>) this;
return ImmutablePoolo.<U>builder()
.poolableObject(getPoolableObject())
.allocatedObject(allocatedObjectUpdated)
.build();
}
@Override
default void close() {
this.getPoolableObject().release();
}
// **********************delegates
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.