private void fetch() {
        try {
            securityContext.asProcessingUser(() -> {
                try {
                    while (!stopping.get()) {
                        needsTasks.set(false);
                        taskContextFactory.context("Fetch Tasks", taskContext -> {
                            try {
                                doFetch(taskContext);
                            } catch (final RuntimeException e) {
                                LOGGER.error(e.getMessage(), e);
                            }
                        }).run();

                        // If we don't need more tasks right now then lock and await.
                        if (!needsTasks.get()) {
                            lock.lockInterruptibly();
                            try {
                                // Check that we still don't need tasks since locking.
                                if (!needsTasks.get()) {
                                    // Wait up to 10 seconds for a task to complete.
                                    if (condition.await(10, TimeUnit.SECONDS)) {
                                        LOGGER.trace("fetch woken up");
                                    } else {
                                        LOGGER.trace("fetch await timeout");
                                    }
                                }
                            } finally {
                                lock.unlock();
                            }
                        }
                    }
                } catch (final InterruptedException e) {
                    LOGGER.debug(e::getMessage, e);
                    Thread.currentThread().interrupt();
                }
            });
        } catch (final RuntimeException e) {
            LOGGER.error("Unable to fetch task!", e);
        }
    }
Tries to fetch tasks asynchronously if we aren't already fetching tasks.
If we are it will make sure we immediately try and fetch tasks again
after the previous fetch.
stroom-job/stroom-job-impl/src/main/java/stroom/job/impl/DistributedTaskFetcher.java