/** * Destroys a service handle quietly, swallows any exception occurred * * @param serviceHandle to destroy */ private void destroyService(ServiceHandle<S> serviceHandle) { try { _serviceFactory.destroy(serviceHandle.getEndPoint(), serviceHandle.getService()); } catch (Exception e) { // this should not happen, but if it does, swallow the exception and log it LOG.warn("Error destroying serviceHandle", e); } } }
/** * Retrieves a cached service instance for an end point that is not currently checked out. If no idle cached * instance is available and the cache is not full, a new one will be created, added to the cache, and then checked * out. Once the checked out instance is no longer in use, it should be returned by calling {@link #checkIn}. * * @param endPoint The end point to retrieve a cached service instance for. * @return A service handle that contains a cached service instance for the requested end point. * @throws NoCachedInstancesAvailableException If the cache has reached total maximum capacity, or maximum capacity * for the requested end point, and no connections that aren't already checked out are available. */ public ServiceHandle<S> checkOut(ServiceEndPoint endPoint) throws Exception { checkNotNull(endPoint); _requestCount.incrementAndGet(); try { long revision = _revisionNumber.incrementAndGet(); S service = _pool.borrowObject(endPoint); ServiceHandle<S> handle = new ServiceHandle<>(service, endPoint); // Remember the revision that we've checked this service out on in case we need to invalidate it later _checkedOutRevisions.put(handle, revision); return handle; } catch (NoSuchElementException e) { _missCount.incrementAndGet(); // This will happen if there are no available connections and there is no room for a new one, // or if a newly created connection is not valid. throw new NoCachedInstancesAvailableException(String.format("No cached instances available for endpoint: %s", endPoint)); } }
return callback.call(handle.getService()); } finally { timer.stop();
/** * Retrieves a cached service instance for an end point that is not currently checked out. If no idle cached * instance is available and the cache is not full, a new one will be created, added to the cache, and then checked * out. Once the checked out instance is no longer in use, it should be returned by calling {@link #checkIn}. * * @param endPoint The end point to retrieve a cached service instance for. * @return A service handle that contains a cached service instance for the requested end point. * @throws NoCachedInstancesAvailableException If the cache has reached total maximum capacity, or maximum capacity * for the requested end point, and no connections that aren't already checked out are available. */ public ServiceHandle<S> checkOut(ServiceEndPoint endPoint) throws Exception { checkNotNull(endPoint); _requestCount.incrementAndGet(); try { long revision = _revisionNumber.incrementAndGet(); S service = _pool.borrowObject(endPoint); ServiceHandle<S> handle = new ServiceHandle<>(service, endPoint); // Remember the revision that we've checked this service out on in case we need to invalidate it later _checkedOutRevisions.put(handle, revision); return handle; } catch (NoSuchElementException e) { _missCount.incrementAndGet(); // This will happen if there are no available connections and there is no room for a new one, // or if a newly created connection is not valid. throw new NoCachedInstancesAvailableException(String.format("No cached instances available for endpoint: %s", endPoint)); } }
return callback.call(handle.getService()); } finally { timer.stop();
/** * Destroys a service handle quietly, swallows any exception occurred * * @param serviceHandle to destroy */ private void destroyService(ServiceHandle<S> serviceHandle) { try { _serviceFactory.destroy(serviceHandle.getEndPoint(), serviceHandle.getService()); } catch (Exception e) { // this should not happen, but if it does, swallow the exception and log it LOG.warn("Error destroying serviceHandle", e); } } }
@Override public synchronized void close() { _isClosed = true; for (ServiceHandle<S> serviceHandle : _instancesPerEndpoint.values()) { _serviceFactory.destroy(serviceHandle.getEndPoint(), serviceHandle.getService()); } _instancesPerEndpoint = Maps.newHashMap(); _cleanupFuture.cancel(false); _cleanupExecutor.shutdownNow(); _metrics.close(); }
@Override public synchronized void close() { _isClosed = true; for (ServiceHandle<S> serviceHandle : _instancesPerEndpoint.values()) { _serviceFactory.destroy(serviceHandle.getEndPoint(), serviceHandle.getService()); } _instancesPerEndpoint = Maps.newHashMap(); _cleanupFuture.cancel(false); _cleanupExecutor.shutdownNow(); _metrics.close(); }
/** * Returns a service instance for an end point to the cache so that it may be used by other users. * * @param handle The service handle that is being checked in. * @throws Exception Never. */ @Override public void checkIn(ServiceHandle<S> handle) throws Exception { checkNotNull(handle); S service = handle.getService(); ServiceEndPoint endPoint = handle.getEndPoint(); // Figure out if we should check this revision in. If it was created before the last known invalid revision // for this particular end point, or the cache is closed, then we shouldn't check it in. Long invalidRevision = _invalidRevisions.get(endPoint); Long serviceRevision = _checkedOutRevisions.remove(handle); if (_isClosed || (invalidRevision != null && serviceRevision < invalidRevision)) { _pool.invalidateObject(endPoint, service); } else { _pool.returnObject(endPoint, service); } }
/** * Returns a service instance for an end point to the cache so that it may be used by other users. * * @param handle The service handle that is being checked in. * @throws Exception Never. */ @Override public void checkIn(ServiceHandle<S> handle) throws Exception { checkNotNull(handle); S service = handle.getService(); ServiceEndPoint endPoint = handle.getEndPoint(); // Figure out if we should check this revision in. If it was created before the last known invalid revision // for this particular end point, or the cache is closed, then we shouldn't check it in. Long invalidRevision = _invalidRevisions.get(endPoint); Long serviceRevision = _checkedOutRevisions.remove(handle); if (_isClosed || (invalidRevision != null && serviceRevision < invalidRevision)) { _pool.invalidateObject(endPoint, service); } else { _pool.returnObject(endPoint, service); } }