org.fusesource.stomp.client.Promise Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of stompjms-client Show documentation
Show all versions of stompjms-client Show documentation
STOMP-JMS is a JMS implementation using STOMP as the wire protocol
/**
* Copyright (C) 2010-2011, FuseSource Corp. All rights reserved.
*
* http://fusesource.com
*
* The software in this package is published under the terms of the
* CDDL license a copy of which has been included with this distribution
* in the license.txt file.
*/
package org.fusesource.stomp.client;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
/**
*
*
*
* @author Hiram Chirino
*/
public class Promise extends Callback implements Future {
private final CountDownLatch latch = new CountDownLatch(1);
Throwable error;
T value;
@Override
public void onFailure(Throwable value) {
error = value;
latch.countDown();
}
@Override
public void onSuccess(T value) {
this.value = value;
latch.countDown();
}
public T await(long amount, TimeUnit unit) throws Exception {
latch.await(amount, unit);
return get();
}
public T await() throws Exception {
latch.await();
return get();
}
private T get() throws Exception {
Throwable e = error;
if( e !=null ) {
if( e instanceof RuntimeException ) {
throw (RuntimeException) e;
} else if( e instanceof Exception) {
throw (Exception) e;
} else if( e instanceof Error) {
throw (Error) e;
} else {
// don't expect to hit this case.
throw new RuntimeException(e);
}
}
return value;
}
}