use of org.bboxdb.commons.concurrent.ExceptionSafeRunnable in project bboxdb by jnidzwetzki.
the class ClientConnectionHandler method sendNextResultsForQuery.
/**
* Send next results for the given query
* @param packageSequence
* @param
* @throws PackageEncodeException
* @throws IOException
*/
public void sendNextResultsForQuery(final short packageSequence, final short querySequence) throws IOException, PackageEncodeException {
if (!getActiveQueries().containsKey(querySequence)) {
logger.error("Unable to resume query {} - package {} - not found", querySequence, packageSequence);
writeResultPackage(new ErrorResponse(packageSequence, ErrorMessages.ERROR_QUERY_NOT_FOUND));
return;
}
final Runnable queryRunable = new ExceptionSafeRunnable() {
@Override
protected void runThread() throws IOException, PackageEncodeException {
final ClientQuery clientQuery = getActiveQueries().get(querySequence);
clientQuery.fetchAndSendNextTuples(packageSequence);
if (clientQuery.isQueryDone()) {
logger.info("Query {} is done with {} tuples, removing iterator ", querySequence, clientQuery.getTotalSendTuples());
clientQuery.close();
getActiveQueries().remove(querySequence);
}
}
@Override
protected void afterExceptionHook() {
try {
writeResultPackage(new ErrorResponse(packageSequence, ErrorMessages.ERROR_EXCEPTION));
} catch (IOException | PackageEncodeException e) {
logger.error("Unable to send result package", e);
}
}
};
// Submit the runnable to our pool
if (threadPool.isShutdown()) {
logger.warn("Thread pool is shutting down, don't execute query: {}", querySequence);
writeResultPackage(new ErrorResponse(packageSequence, ErrorMessages.ERROR_EXCEPTION));
} else {
getThreadPool().submit(queryRunable);
}
}
use of org.bboxdb.commons.concurrent.ExceptionSafeRunnable in project bboxdb by jnidzwetzki.
the class PackageRouter method performInsertPackageRoutingAsync.
/**
* Perform the routing task async
* @param packageSequence
* @param insertTupleRequest
* @param boundingBox
*/
public void performInsertPackageRoutingAsync(final short packageSequence, final InsertTupleRequest insertTupleRequest) {
final Runnable routeRunable = new ExceptionSafeRunnable() {
@Override
protected void runThread() {
boolean operationSuccess = true;
try {
final RoutingHeader routingHeader = insertTupleRequest.getRoutingHeader();
assert (routingHeader.isRoutedPackage()) : "Tuple is not a routed package";
if (!routingHeader.reachedFinalInstance()) {
routingHeader.dispatchToNextHop();
operationSuccess = sendInsertPackage(insertTupleRequest);
}
} catch (InterruptedException e) {
logger.error("Exception while routing package", e);
Thread.currentThread().interrupt();
operationSuccess = false;
} catch (PackageEncodeException e) {
logger.error("Exception while routing package", e);
operationSuccess = false;
}
if (operationSuccess) {
final SuccessResponse responsePackage = new SuccessResponse(packageSequence);
clientConnectionHandler.writeResultPackageNE(responsePackage);
} else {
final ErrorResponse responsePackage = new ErrorResponse(packageSequence, ErrorMessages.ERROR_ROUTING_FAILED);
clientConnectionHandler.writeResultPackageNE(responsePackage);
}
}
};
// Submit the runnable to our pool
if (threadPool.isShutdown()) {
logger.warn("Thread pool is shutting down, don't route package: {}", packageSequence);
final ErrorResponse responsePackage = new ErrorResponse(packageSequence, ErrorMessages.ERROR_QUERY_SHUTDOWN);
clientConnectionHandler.writeResultPackageNE(responsePackage);
} else {
threadPool.submit(routeRunable);
}
}
use of org.bboxdb.commons.concurrent.ExceptionSafeRunnable in project bboxdb by jnidzwetzki.
the class TestExceptionSafeRunnable method testWithException.
/**
* Test the runnable with an exception
* @throws InterruptedException
*/
@Test(timeout = 10000)
public void testWithException() throws InterruptedException {
final AtomicInteger runTheadCalls = new AtomicInteger(0);
final AtomicInteger beginHookCalls = new AtomicInteger(0);
final AtomicInteger endHookCalls = new AtomicInteger(0);
final AtomicInteger afterExceptionCalls = new AtomicInteger(0);
/**
* Because the methods are protected, we need a real mock here
* Mockito can't verify protected method calls
*/
final ExceptionSafeRunnable runnable = new ExceptionSafeRunnable() {
@Override
protected void runThread() throws Exception {
runTheadCalls.incrementAndGet();
throw new Exception("Exception");
}
@Override
protected void beginHook() {
beginHookCalls.incrementAndGet();
}
@Override
protected void endHook() {
endHookCalls.incrementAndGet();
}
@Override
protected void afterExceptionHook() {
afterExceptionCalls.incrementAndGet();
}
};
final Thread thread = new Thread(runnable);
thread.start();
thread.join();
Assert.assertEquals(1, runTheadCalls.get());
Assert.assertEquals(1, beginHookCalls.get());
Assert.assertEquals(0, endHookCalls.get());
Assert.assertEquals(1, afterExceptionCalls.get());
}
use of org.bboxdb.commons.concurrent.ExceptionSafeRunnable in project bboxdb by jnidzwetzki.
the class TestExceptionSafeRunnable method testDefaultImplementation1.
@Test(timeout = 60000)
public void testDefaultImplementation1() throws InterruptedException {
final ExceptionSafeRunnable runnable = new ExceptionSafeRunnable() {
@Override
protected void runThread() throws Exception {
}
};
final Thread thread = new Thread(runnable);
thread.start();
thread.join();
}
use of org.bboxdb.commons.concurrent.ExceptionSafeRunnable in project bboxdb by jnidzwetzki.
the class TestExceptionSafeRunnable method testWithoutException.
/**
* Test the runnable without an exception
* @throws InterruptedException
*/
@Test(timeout = 10000)
public void testWithoutException() throws InterruptedException {
final AtomicInteger runTheadCalls = new AtomicInteger(0);
final AtomicInteger beginHookCalls = new AtomicInteger(0);
final AtomicInteger endHookCalls = new AtomicInteger(0);
final AtomicInteger afterExceptionCalls = new AtomicInteger(0);
/**
* Because the methods are protected, we need a real mock here
* Mockito can't verify protected method calls
*/
final ExceptionSafeRunnable runnable = new ExceptionSafeRunnable() {
@Override
protected void runThread() throws Exception {
runTheadCalls.incrementAndGet();
}
@Override
protected void beginHook() {
beginHookCalls.incrementAndGet();
}
@Override
protected void endHook() {
endHookCalls.incrementAndGet();
}
@Override
protected void afterExceptionHook() {
afterExceptionCalls.incrementAndGet();
}
};
final Thread thread = new Thread(runnable);
thread.start();
thread.join();
Assert.assertEquals(1, runTheadCalls.get());
Assert.assertEquals(1, beginHookCalls.get());
Assert.assertEquals(1, endHookCalls.get());
Assert.assertEquals(0, afterExceptionCalls.get());
}
Aggregations