Skip to content

Commit

Permalink
V2.1.0 (#3)
Browse files Browse the repository at this point in the history
* new change to passivate object and minor changes to configs

* updated test factory
  • Loading branch information
venkata-kishore authored Dec 24, 2024
1 parent 85b50e8 commit 3362fdd
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</parent>

<artifactId>simple-object-pool</artifactId>
<version>2.0.0</version>
<version>2.1.0</version>
<packaging>jar</packaging>

<properties>
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/today/bonfire/oss/sop/PooledObjectFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ public interface PooledObjectFactory<T extends PoolObject> {
*/
void activateObject(T obj);


/**
* Passivates the pooled object.
* This method is called when the object is being returned to the pool,
* and is responsible for any necessary passivation or cleaning-up before returning to pool.
*
* @param obj the object to passivate, must not be null.
*/
void passivateObject(T obj);

/**
* Validates the pooled object to determine if it can be safely borrowed.
* This method should return true if the object is in a valid state for
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/today/bonfire/oss/sop/SimpleObjectPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ private void evictionRun() {
}

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

factory.passivateObject(obj);
borrowedObjects.remove(pooledEntity.id());
pooledEntity.markIdle();
idleObjects.add(pooledEntity);
Expand Down Expand Up @@ -441,7 +442,7 @@ public void returnObject(T obj) throws PoolObjectException {
@Override
public void close() {
if (!scheduler.isShutdown()) {
log.info("Closing object pool");
log.info("Closing object pool. Current pool size: {}", currentPoolSize.get());
scheduler.shutdown();
} else {
log.warn("Trying to close an Object pool that is already closed");
Expand Down
32 changes: 26 additions & 6 deletions src/main/java/today/bonfire/oss/sop/SimpleObjectPoolConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public class SimpleObjectPoolConfig {
*/
private final long waitingForObjectTimeout;

private SimpleObjectPoolConfig(Builder builder) {
protected SimpleObjectPoolConfig(Builder builder) {
this.maxPoolSize = builder.maxPoolSize;
this.minPoolSize = builder.minPoolSize;
this.fairness = builder.fairness;
Expand Down Expand Up @@ -195,7 +195,7 @@ public enum EvictionPolicy {
public static class Builder {
private int maxPoolSize = 8;
private int minPoolSize = 0;
private boolean fairness = true;
private boolean fairness = false;
private boolean testOnCreate = false;
private boolean testOnBorrow = true;
private boolean testOnReturn = false;
Expand Down Expand Up @@ -223,6 +223,9 @@ public Builder maxPoolSize(int maxPoolSize) {

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

/**
* Sets the timeout duration for waiting for an object to become available in the pool.
* The default value is 10 seconds if not set.
* <p>
* 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
* but will throw an exception instead.
* <p>
* if you want to wait indefinitely then use a very large value in the range of hours or days.
* Setting a very high value is not recommended, rather handle the cases with much shorter timeouts.
*
* @param waitingForObjectTimeout The waiting for object timeout duration.
* @return This {@code Builder} instance.
Expand All @@ -404,7 +420,7 @@ public Builder waitingForObjectTimeout(Duration waitingForObjectTimeout) {
* This method will throw an exception if any of the configuration settings are invalid.
* It will also log warnings if any of the settings may result in unexpected behavior.
*/
public void validate() {
protected void validate() {
if (maxPoolSize < 1) {
throw new IllegalArgumentException("maxPoolSize must be greater than 0");
}
Expand Down Expand Up @@ -466,7 +482,7 @@ public void validate() {

}

public SimpleObjectPoolConfig build() {
protected Builder prepareAndCheck() {
if (numValidationsPerEvictionRun == null) {
numValidationsPerEvictionRun = maxPoolSize;
}
Expand All @@ -476,9 +492,13 @@ public SimpleObjectPoolConfig build() {
maxRetries = 1;
}
}
SimpleObjectPoolConfig config = new SimpleObjectPoolConfig(this);
validate();
return config;
return this;
}

public SimpleObjectPoolConfig build() {
prepareAndCheck();
return new SimpleObjectPoolConfig(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ public TestPoolObject createObject() {
// Do nothing
}

@Override public void passivateObject(TestPoolObject obj) {
// Do nothing
}

@Override
public boolean isObjectValidForBorrow(TestPoolObject obj) {
if (validationDelayMillis > 0) {
Expand Down

0 comments on commit 3362fdd

Please sign in to comment.