@Override public boolean waitAndCheck(AsyncJob job, String[] wakeupTopicsOnMessageBus, long checkIntervalInMilliSeconds, long timeoutInMiliseconds, Predicate predicate) { MessageDetector msgDetector = new MessageDetector(); String[] topics = Arrays.copyOf(wakeupTopicsOnMessageBus, wakeupTopicsOnMessageBus.length + 1); topics[topics.length - 1] = AsyncJob.Topics.JOB_STATE; msgDetector.open(_messageBus, topics); try { long startTick = System.currentTimeMillis(); while (timeoutInMiliseconds < 0 || System.currentTimeMillis() - startTick < timeoutInMiliseconds) { msgDetector.waitAny(checkIntervalInMilliSeconds); job = _jobDao.findById(job.getId()); if (job != null && job.getStatus().done()) { return true; } if (predicate.checkCondition()) { return true; } } } finally { msgDetector.close(); } return false; }