All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bbottema.clusteredobjectpool.core.ResourcePools Maven / Gradle / Ivy

The newest version!
/*
 * Copyright © 2019 Benny Bottema ([email protected])
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.bbottema.clusteredobjectpool.core;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import org.bbottema.clusteredobjectpool.util.CompositeFuturesAsFutureTask;
import org.bbottema.genericobjectpool.PoolableObject;
import org.bbottema.genericobjectpool.util.Timeout;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.Future;

/**
 * Serves to hide some methods that iterate over a cluster of pools.
 */
@RequiredArgsConstructor
@ToString
class ResourcePools {
	@Getter(AccessLevel.PACKAGE)
	private final Collection> clusterCollection;
	private final Collection> poolsShuttingDown = new ArrayList<>();
	
	@SuppressWarnings("UnusedReturnValue")
	Future shutdownPool(@Nullable PoolKey key) {
		final List> poolsShuttingDownFuture = new ArrayList<>();
		for (Iterator> iterator = clusterCollection.iterator(); iterator.hasNext(); ) {
			ResourcePool poolInCluster = iterator.next();
			if (key == null || poolInCluster.getPoolKey().equals(key)) {
				poolsShuttingDownFuture.add(poolInCluster.clearPool());
				poolsShuttingDown.add(poolInCluster);
				iterator.remove();
			}
		}
		return CompositeFuturesAsFutureTask.ofFutures(poolsShuttingDownFuture);
	}
	
	boolean containsPool(PoolKey poolKey) {
		return findResourcePool(poolKey) != null;
	}
	
	void add(ResourcePool resourcePool) {
		clusterCollection.add(resourcePool);
	}
	
	@Nullable
	PoolableObject claimResource(PoolKey poolKey, Timeout claimTimeout) throws InterruptedException {
		ResourcePool resourcePool = findResourcePool(poolKey);
		if (resourcePool == null) {
			throw new IllegalArgumentException("Couldn't find resource pool with key: " + poolKey);
		}
		return resourcePool.claim(claimTimeout);
	}
	
	@Nullable
	private ResourcePool findResourcePool(PoolKey poolKey) {
		for (ResourcePool resourcePool : clusterCollection) {
			if (resourcePool.getPoolKey().equals(poolKey)) {
				return resourcePool;
			}
		}
		return null;
	}
	
	int currentlyAllocated() {
		int total = 0;
		for (ResourcePool resourcePool : clusterCollection) {
			total += resourcePool.getPoolMetrics().getCurrentlyAllocated();
		}
		for (ResourcePool resourcePool : poolsShuttingDown) {
			total += resourcePool.getPoolMetrics().getCurrentlyAllocated();
		}
		return total;
	}
	
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy