@Override
    public void flushAllEvents() {
        // Mark the shutdown as happening so we can stop new things being borrowed from the pool
        isShutdown.set(true);

        // now we just need to worry about maps already borrowed or anyone blocked waiting.

        // Wait for anyone blocked waiting to borrow. If we don't wait for these then when close
        // is called on the pool they will be interupted and thus the stats are lost
        securityContext.asProcessingUser(() -> {
            if (objectPool.getNumWaiters() > 0) {
                LOGGER.info("Waiting for SQLStatisticAggregateMaps waiters, active: {}, waiting: {}",
                        objectPool.getNumActive(),
                        objectPool.getNumWaiters());
                while (objectPool.getNumWaiters() > 0) {
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                        LOGGER.error("Thread interrupted while waiting for object pool waiters", e);
                    }
                }
            }

            // Anything in the pool will be flushed, any borrowed items will be flushed when they are returned
            LOGGER.info("Closing object pool");
            objectPool.close();

            if (objectPool.getNumActive() > 0) {
                LOGGER.info("Waiting for SQLStatisticAggregateMaps to be returned to the pool, active: {}, waiting: {}",
                        objectPool.getNumActive(),
                        objectPool.getNumWaiters());
                while (objectPool.getNumActive() > 0) {
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                        LOGGER.error("Thread interrupted while waiting for pool objects to be returned", e);
                    }
                }
            }

            // Now all our maps have been given to the cache, flush the cache.
            LOGGER.info("Flushing map cache");
            statisticCache.flush(true);
        });
    }
@return A predicate that will test if an event is within the processing age threshold, or if there
is no configured max age then process all events
stroom-statistics/stroom-statistics-impl-sql/src/main/java/stroom/statistics/impl/sql/SQLStatisticEventStore.java