apoc.util.QueueUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of apoc-common Show documentation
Show all versions of apoc-common Show documentation
Data types package for Neo4j Procedures
package apoc.util;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
public class QueueUtil {
public static final int WAIT = 1;
public static final TimeUnit WAIT_UNIT = TimeUnit.SECONDS;
public static void put(BlockingQueue queue, T item, long timeoutSeconds) {
put(queue, item, timeoutSeconds, true, () -> {});
}
/**
* to be used instead of {@link BlockingQueue#put}
* @param queue
* @param item
* @param timeoutSeconds
* @param failWithExecption true if a {@link RuntimeException} should be thrown in case we couldn't add item into the queue within timeframe
* @param checkDuringOffering a callback supposed to throw an exception to terminate
* @param
*/
public static void put(BlockingQueue queue, T item, long timeoutSeconds, boolean failWithExecption, Runnable checkDuringOffering) {
try {
long timeoutTimestamp = System.currentTimeMillis() + timeoutSeconds * 1000;
while (true) {
if (System.currentTimeMillis() > timeoutTimestamp) break;
boolean success = queue.offer(item, WAIT, WAIT_UNIT);
if (success) {
return;
}
checkDuringOffering.run();
}
if (failWithExecption) {
throw new RuntimeException("Error queuing item before timeout of " + timeoutSeconds + " seconds");
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
/**
* to be used instead of {@link BlockingQueue#take}
* @param queue
* @param timeoutSeconds
* @param checkDuringPolling a callback supposed to throw an exception to terminate
* @param
* @return
*/
public static T take(BlockingQueue queue, long timeoutSeconds, Runnable checkDuringPolling) {
try {
long started = System.currentTimeMillis();
while (started + timeoutSeconds * 1000 > System.currentTimeMillis()) {
T polled = queue.poll(WAIT, WAIT_UNIT);
if (polled != null) {
return polled;
}
checkDuringPolling.run();
}
throw new RuntimeException("Error polling, timeout of " + timeoutSeconds + " seconds reached.");
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy