public void handleExchange(Endpoint endpoint, MessageExchange exchange, boolean add) { Set<String> exchanges = getKnownExchanges(endpoint); synchronized (exchanges) { if (add) { exchanges.add(exchange.getExchangeId()); } else { exchanges.remove(exchange.getExchangeId()); } exchanges.notifyAll(); } }
/** * Prepare an endpoint for shutdown by waiting until all its pending exchanges have been finished. * This method will wait no longer than the timeout specified (in milliseconds) * * @param endpoint the endpoint that is about to be shut down * @param timeout the maximum amount of time (in milliseconds) to wait * @throws InterruptedException */ public void prepareShutdown(Endpoint endpoint, long timeout) throws InterruptedException { Set<String> exchanges = getKnownExchanges(endpoint); long start = System.currentTimeMillis(); long interval = timeout / 3; // if a timeout has been set, we'll check 3 times within the timeout period synchronized (exchanges) { while (!exchanges.isEmpty()) { for (String id : exchanges) { logger.debug("Waiting for exchange {} in {}", id, endpoint); } exchanges.wait(interval); // if a timeout has been set, this would be a good time to check that long delta = System.currentTimeMillis() - start; if (timeout != 0 && delta >= timeout) { logger.debug(String.format("Gave up waiting for %s exchanges in %s after %s ms", exchanges.size(), endpoint, delta)); break; } } } }