* @param exchange the exchange to send
* @param timeout time to wait in milliseconds
* @return <code>true</code> if the exchange has been processed succesfully
*/
public boolean sendSync(Exchange exchange, long timeout) {
InternalExchange e = (InternalExchange) exchange;
Semaphore lock = e.getRole() == Role.Consumer ? e.getConsumerLock(true)
: e.getProviderLock(true);
dispatch(e);
Thread thread = Thread.currentThread();
String original = thread.getName();
try {
if (timeout > 0) {
if (!lock.tryAcquire(timeout, TimeUnit.MILLISECONDS)) {
throw new TimeoutException();
}
} else {
thread.setName(original + " (waiting for exchange " + exchange.getId() + ")");
lock.acquire();
}
e.setRole(e.getRole() == Role.Consumer ? Role.Provider : Role.Consumer);
} catch (InterruptedException ex) {
exchange.setError(ex);
for (ExchangeListener l : nmr.getListenerRegistry().getListeners(ExchangeListener.class)) {
l.exchangeFailed(exchange);
}