use of org.spf4j.recyclable.ObjectDisposeException in project spf4j by zolyfarkas.
the class SharingObjectPool method tryDispose.
@Override
public synchronized boolean tryDispose(final long timeoutMillis) throws ObjectDisposeException, InterruptedException {
if (!closed) {
long deadline = System.currentTimeMillis() + timeoutMillis;
closed = true;
ObjectDisposeException exres = null;
Iterator<SharedObject<T>> iterator = pooledObjects.iterator();
while (iterator.hasNext()) {
SharedObject<T> so = iterator.next();
try {
while (so.getNrTimesShared() > 0) {
long waitFor = deadline - System.currentTimeMillis();
if (waitFor > 0) {
this.wait(waitFor);
} else {
return false;
}
}
T o = so.getObject();
o2QueueRefMap.remove(o);
iterator.remove();
nrObjects--;
factory.dispose(o);
} catch (ObjectDisposeException ex) {
if (exres == null) {
exres = ex;
} else {
ex.addSuppressed(exres);
exres = ex;
}
}
}
if (exres != null) {
throw exres;
}
}
return true;
}
use of org.spf4j.recyclable.ObjectDisposeException in project spf4j by zolyfarkas.
the class SimpleSmartObjectPool method tryDispose.
@Override
public boolean tryDispose(final long timeoutMillis) throws ObjectDisposeException, InterruptedException {
factory.dispose(sample);
long deadlineNanos = TimeSource.nanoTime() + TimeUnit.MILLISECONDS.toNanos(timeoutMillis);
lock.lock();
try {
maxSize = 0;
List<Pair<ObjectBorower<T>, T>> returnedObjects = new ArrayList<>();
for (Entry<ObjectBorower<T>, Collection<T>> b : borrowedObjects.asMap().entrySet()) {
ObjectBorower<T> borrower = b.getKey();
final int nrObjects = b.getValue().size();
for (int i = 0; i < nrObjects; i++) {
Either<Action, T> object = borrower.tryRequestReturnObject();
if (object.isRight()) {
returnedObjects.add(Pair.of(borrower, object.getRight()));
}
}
}
for (Pair<ObjectBorower<T>, T> objectAndBorrower : returnedObjects) {
T object = objectAndBorrower.getSecond();
if (!borrowedObjects.remove(objectAndBorrower.getFirst(), object)) {
throw new IllegalStateException("Returned Object hasn't been borrowed " + object);
}
availableObjects.add(object);
}
ObjectDisposeException exception = disposeReturnedObjects(null);
while (!borrowedObjects.isEmpty()) {
long waitTimeNanos = deadlineNanos - TimeSource.nanoTime();
if (waitTimeNanos <= 0) {
return false;
}
if (!available.await(waitTimeNanos, TimeUnit.NANOSECONDS)) {
return false;
}
exception = disposeReturnedObjects(exception);
}
if (exception != null) {
throw exception;
}
return true;
} catch (InterruptedException | RuntimeException e) {
throw e;
} finally {
lock.unlock();
}
}
use of org.spf4j.recyclable.ObjectDisposeException in project spf4j by zolyfarkas.
the class SimpleSmartObjectPool method disposeReturnedObjects.
@CheckReturnValue
private ObjectDisposeException disposeReturnedObjects(final ObjectDisposeException exception) {
ObjectDisposeException result = exception;
for (T obj : availableObjects) {
try {
factory.dispose(obj);
} catch (ObjectDisposeException ex) {
if (result == null) {
result = ex;
} else {
result = Throwables.suppress(ex, result);
}
} catch (Exception ex) {
if (result == null) {
result = new ObjectDisposeException(ex);
} else {
result = Throwables.suppress(new ObjectDisposeException(ex), result);
}
}
}
availableObjects.clear();
return result;
}
use of org.spf4j.recyclable.ObjectDisposeException in project spf4j by zolyfarkas.
the class ObjectHolder method validateObjectIfNotBorrowed.
public synchronized void validateObjectIfNotBorrowed() throws ObjectDisposeException {
if (!borrowed && obj != null) {
boolean isValid;
Exception vex = null;
try {
isValid = factory.validate(obj, null);
} catch (RuntimeException ex) {
isValid = false;
} catch (Exception ex) {
isValid = false;
}
if (!isValid) {
LOG.warn("Validation of {} failed, detail {} ", obj, vex);
T object = obj;
obj = null;
factory.dispose(object);
}
}
}
use of org.spf4j.recyclable.ObjectDisposeException in project spf4j by zolyfarkas.
the class ObjectHolder method returnObject.
public synchronized void returnObject(final T object, @Nullable final Exception e) {
if (!borrowed || object != obj) {
throw new IllegalStateException("Cannot return something that was " + "not borrowed from here " + object);
}
borrowed = false;
if (e != null) {
boolean isValid;
Exception vex = null;
try {
isValid = factory.validate(object, e);
} catch (Exception ex) {
isValid = false;
vex = ex;
}
if (!isValid) {
LOG.warn("Validation of {} failed, detail {}", obj, vex, e);
obj = null;
try {
factory.dispose(object);
} catch (ObjectDisposeException ex1) {
LOG.warn("Failed to dispose {}", object, ex1);
} catch (RuntimeException ex1) {
LOG.error("Failed to dispose {}", object, ex1);
}
}
}
}
Aggregations