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.
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
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.
I am using simple java mail to send email, and observed that couple of times my program stuck, after checking the thread dump I found that multiple threads are stuck at below Stracktrace:
Simple Java Mail async mail sender, executor 1 / thread 1" #86 prio=5 os_prio=0 cpu=4130.48ms elapsed=1948.25s tid=0x0000fff5d8142aa0 nid=0x139 waiting on condition [0x0000ffed3718d000]
java.lang.Thread.State: WAITING (parking)
at jdk.internal.misc.Unsafe.park([email protected]/Native Method)
- parking to wait for <merged>(a java.util.concurrent.locks.ReentrantLock$NonfairSync)
at java.util.concurrent.locks.LockSupport.park([email protected]/LockSupport.java:211)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquire([email protected]/AbstractQueuedSynchronizer.java:715)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquire([email protected]/AbstractQueuedSynchronizer.java:938)
at java.util.concurrent.locks.ReentrantLock$Sync.lock([email protected]/ReentrantLock.java:153)
at java.util.concurrent.locks.ReentrantLock.lock([email protected]/ReentrantLock.java:322)
at org.bbottema.genericobjectpool.GenericObjectPool.claim(GenericObjectPool.java:91)
at org.bbottema.clusteredobjectpool.core.ResourcePool.claim(ResourcePool.java:44)
at org.bbottema.clusteredobjectpool.core.ResourceClusters.claimResourceFromCluster(ResourceClusters.java:124)
at org.simplejavamail.internal.batchsupport.BatchSupport.acquireTransport(BatchSupport.java:113)
at org.simplejavamail.mailer.internal.util.TransportRunner.sendUsingConnectionPool(TransportRunner.java:84)
at org.simplejavamail.mailer.internal.util.TransportRunner.runOnSessionTransport(TransportRunner.java:72)
at org.simplejavamail.mailer.internal.util.TransportRunner.sendMessage(TransportRunner.java:48)
at org.simplejavamail.mailer.internal.SendMailClosure.executeClosure(SendMailClosure.java:82)
at org.simplejavamail.mailer.internal.AbstractProxyServerSyncingClosure.run(AbstractProxyServerSyncingClosure.java:56)
at org.simplejavamail.mailer.internal.MailerImpl.sendMail(MailerImpl.java:345)
at org.simplejavamail.mailer.internal.MailerImpl.sendMail(MailerImpl.java:331)
we are using
Simple Java Mail 6.6.1 and
Generic Object Pool : 1.0.5
Is there any recent fix done in latest version 2.0.1 to fix these kind of issues ?
thanks,
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.