Skip to content

Commit 3362fdd

Browse files
V2.1.0 (#3)
* new change to passivate object and minor changes to configs * updated test factory
1 parent 85b50e8 commit 3362fdd

File tree

5 files changed

+44
-9
lines changed

5 files changed

+44
-9
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
</parent>
1212

1313
<artifactId>simple-object-pool</artifactId>
14-
<version>2.0.0</version>
14+
<version>2.1.0</version>
1515
<packaging>jar</packaging>
1616

1717
<properties>

src/main/java/today/bonfire/oss/sop/PooledObjectFactory.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ public interface PooledObjectFactory<T extends PoolObject> {
2929
*/
3030
void activateObject(T obj);
3131

32+
33+
/**
34+
* Passivates the pooled object.
35+
* This method is called when the object is being returned to the pool,
36+
* and is responsible for any necessary passivation or cleaning-up before returning to pool.
37+
*
38+
* @param obj the object to passivate, must not be null.
39+
*/
40+
void passivateObject(T obj);
41+
3242
/**
3343
* Validates the pooled object to determine if it can be safely borrowed.
3444
* This method should return true if the object is in a valid state for

src/main/java/today/bonfire/oss/sop/SimpleObjectPool.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ private void evictionRun() {
171171
}
172172

173173
if (!objectsToDestroy.isEmpty()) {
174-
log.info("Evicted {} idle objects. Current pool size: {}, Idle: {}, Borrowed: {}",
174+
log.debug("Evicted {} idle objects. Current pool size: {}, Idle: {}, Borrowed: {}",
175175
objectsToDestroy.size(), currentPoolSize(), idleObjectCount(), borrowedObjectsCount());
176176
objectsToDestroy.clear();
177177
}
@@ -406,6 +406,7 @@ public void returnObject(T obj, boolean broken) throws PoolObjectException {
406406
log.warn("Returned broken or invalid entity with id {} and destroyed it.", pooledEntity.id());
407407
} else {
408408

409+
factory.passivateObject(obj);
409410
borrowedObjects.remove(pooledEntity.id());
410411
pooledEntity.markIdle();
411412
idleObjects.add(pooledEntity);
@@ -441,7 +442,7 @@ public void returnObject(T obj) throws PoolObjectException {
441442
@Override
442443
public void close() {
443444
if (!scheduler.isShutdown()) {
444-
log.info("Closing object pool");
445+
log.info("Closing object pool. Current pool size: {}", currentPoolSize.get());
445446
scheduler.shutdown();
446447
} else {
447448
log.warn("Trying to close an Object pool that is already closed");

src/main/java/today/bonfire/oss/sop/SimpleObjectPoolConfig.java

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public class SimpleObjectPoolConfig {
133133
*/
134134
private final long waitingForObjectTimeout;
135135

136-
private SimpleObjectPoolConfig(Builder builder) {
136+
protected SimpleObjectPoolConfig(Builder builder) {
137137
this.maxPoolSize = builder.maxPoolSize;
138138
this.minPoolSize = builder.minPoolSize;
139139
this.fairness = builder.fairness;
@@ -195,7 +195,7 @@ public enum EvictionPolicy {
195195
public static class Builder {
196196
private int maxPoolSize = 8;
197197
private int minPoolSize = 0;
198-
private boolean fairness = true;
198+
private boolean fairness = false;
199199
private boolean testOnCreate = false;
200200
private boolean testOnBorrow = true;
201201
private boolean testOnReturn = false;
@@ -223,6 +223,9 @@ public Builder maxPoolSize(int maxPoolSize) {
223223

224224
/**
225225
* Sets the minimum number of objects that the pool should maintain.
226+
* Note setting a positive value does not guarantee that the pool will have at least the specified number
227+
* of objects rather it suggests that the pool should attempt to maintain the specified number of objects.
228+
* Especially in case of evictionsruns the pool may not be able to maintain the specified number of objects.
226229
*
227230
* @param minPoolSize The minimum pool size.
228231
* @return This {@code Builder} instance.
@@ -323,6 +326,12 @@ public Builder durationBetweenEvictionsRuns(Duration durationBetweenEvictionsRun
323326
/**
324327
* Sets the timeout duration for an object from its creation time to be considered for
325328
* eviction.
329+
* <br>
330+
* If set to 0 then eviction will be immediate.
331+
* <p>
332+
* Note: if you want eviction only based on object validation then set
333+
* this to a very high value in the range of hours or days.
334+
* </p>
326335
*
327336
* @param objEvictionTimeout The object idle timeout duration.
328337
* @return This {@code Builder} instance.
@@ -389,6 +398,13 @@ public Builder retryCreationDelay(Duration retryCreationDelay) {
389398

390399
/**
391400
* Sets the timeout duration for waiting for an object to become available in the pool.
401+
* The default value is 10 seconds if not set.
402+
* <p>
403+
* If the timeout is set to 0 or negative values then the pool will not wait for an object to become available if the pool is empty
404+
* but will throw an exception instead.
405+
* <p>
406+
* if you want to wait indefinitely then use a very large value in the range of hours or days.
407+
* Setting a very high value is not recommended, rather handle the cases with much shorter timeouts.
392408
*
393409
* @param waitingForObjectTimeout The waiting for object timeout duration.
394410
* @return This {@code Builder} instance.
@@ -404,7 +420,7 @@ public Builder waitingForObjectTimeout(Duration waitingForObjectTimeout) {
404420
* This method will throw an exception if any of the configuration settings are invalid.
405421
* It will also log warnings if any of the settings may result in unexpected behavior.
406422
*/
407-
public void validate() {
423+
protected void validate() {
408424
if (maxPoolSize < 1) {
409425
throw new IllegalArgumentException("maxPoolSize must be greater than 0");
410426
}
@@ -466,7 +482,7 @@ public void validate() {
466482

467483
}
468484

469-
public SimpleObjectPoolConfig build() {
485+
protected Builder prepareAndCheck() {
470486
if (numValidationsPerEvictionRun == null) {
471487
numValidationsPerEvictionRun = maxPoolSize;
472488
}
@@ -476,9 +492,13 @@ public SimpleObjectPoolConfig build() {
476492
maxRetries = 1;
477493
}
478494
}
479-
SimpleObjectPoolConfig config = new SimpleObjectPoolConfig(this);
480495
validate();
481-
return config;
496+
return this;
497+
}
498+
499+
public SimpleObjectPoolConfig build() {
500+
prepareAndCheck();
501+
return new SimpleObjectPoolConfig(this);
482502
}
483503
}
484504
}

src/test/java/today/bonfire/oss/sop/TestPooledObjectFactory.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ public TestPoolObject createObject() {
4949
// Do nothing
5050
}
5151

52+
@Override public void passivateObject(TestPoolObject obj) {
53+
// Do nothing
54+
}
55+
5256
@Override
5357
public boolean isObjectValidForBorrow(TestPoolObject obj) {
5458
if (validationDelayMillis > 0) {

0 commit comments

Comments
 (0)