@Override public void recycle(final T object) { recycle(object, null); }
@Override public void recycle(final T object, final Exception e) { if (e != null) { if (asyncValidate) { DefaultExecutor.INSTANCE.execute(new AbstractRunnable(true) { @Override public void doRun() { validate(object, e); } }); } else { validate(object, e); } } else { returnToQueue(object); } }
@Test @SuppressFBWarnings("PRMC_POSSIBLY_REDUNDANT_METHOD_CALLS") public void testSharingPool2() throws ObjectCreationException, ObjectBorrowException, InterruptedException, TimeoutException { SharingObjectPool<String> pool = createTestPool(); String obj = pool.get(); Assert.assertEquals("O0", obj); pool.recycle(obj); String obj2 = pool.get(); Assert.assertEquals("O0", obj2); }
public SharingObjectPool(final Factory<T> factory, final int coreSize, final int maxSize, final boolean asyncValidate) throws ObjectCreationException { if (maxSize <= 0) { throw new IllegalArgumentException("max size must be greater than zero and not " + maxSize); } if (maxSize < coreSize) { throw new IllegalArgumentException("max size must be greater than core size and not " + maxSize + " < " + coreSize); } this.factory = factory; this.pooledObjects = new UpdateablePriorityQueue<>(maxSize, SH_COMP); this.nrObjects = 0; this.closed = false; this.asyncValidate = asyncValidate; o2QueueRefMap = new IdentityHashMap<>(maxSize); for (int i = 0; i < coreSize; i++) { createObject(0); } this.maxSize = maxSize; }
@SuppressFBWarnings("REC_CATCH_EXCEPTION") private synchronized void validate(final T object, final Exception e) { if (o2QueueRefMap.containsKey(object)) { // element still in queue boolean isValid; try { isValid = factory.validate(object, e); // validate } catch (Exception ex) { ex.addSuppressed(e); LOG.warn("Validation failed for {}", object, ex); isValid = false; } if (!isValid) { // remove from pool UpdateablePriorityQueue.ElementRef qref = o2QueueRefMap.remove(object); nrObjects--; qref.remove(); } else { returnToQueue(object); } } }
@SuppressFBWarnings("SIC_INNER_SHOULD_BE_STATIC_ANON") public static SharingObjectPool<String> createTestPool() throws ObjectCreationException { return new SharingObjectPool<>(new RecyclingSupplier.Factory<String>() { private int i = 0; @Override public String create() { return "O" + (i++); } @Override public void dispose(final String object) { } @Override public boolean validate(final String object, final Exception e) { throw new UnsupportedOperationException("Not supported yet."); } }, 0, 4); }
@Test @SuppressFBWarnings("PRMC_POSSIBLY_REDUNDANT_METHOD_CALLS") public void testSharingPool() throws ObjectCreationException, ObjectBorrowException, InterruptedException, TimeoutException { SharingObjectPool<String> pool = createTestPool(); String obj = pool.get(); Assert.assertEquals("O0", obj); String obj2 = pool.get(); Assert.assertEquals("O1", obj2); pool.recycle(obj); String obj3 = pool.get(); Assert.assertEquals("O0", obj3); }
@Override @Nullable public synchronized T tryGet(final long deadlineNanos) throws ObjectBorrowException, ObjectCreationException { if (closed) { throw new ObjectBorrowException("Reclycler is closed " + this); } UpdateablePriorityQueue<SharedObject<T>>.ElementRef peekEntry = pooledObjects.peekEntry(); if (peekEntry != null) { final SharedObject<T> elem = peekEntry.getElem(); if (elem.getNrTimesShared() == 0) { elem.inc(); peekEntry.elementMutated(); return elem.getObject(); } else if (nrObjects < maxSize) { return createObject(1); } else { return elem.getObject(); } } else { return createObject(1); } }