.
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.redisson.client.RedisConnectionException;
import org.redisson.misc.RPromise;
import org.redisson.misc.RedissonPromise;
import java.util.function.BiConsumer;
@Slf4j
public class RedissonPromiseTest {
public static void main(String[] args) throws Exception {
RPromise<FooOneResult> promise = new RedissonPromise<FooOneResult>();
// 注册回调
{
BiConsumer callback = new BiConsumer<FooOneResult, Throwable>() {
@Override
public void accept(FooOneResult fooOneResult, Throwable throwable) {
if (throwable != null) {
log.error("出现异常", throwable);
return;
}
log.info("fooOneResult = {}", fooOneResult);
}
};
promise.onComplete(callback);
}
// 执行
{
doRun(promise, true);
doRun(promise, false);
}
}
private static void doRun(RPromise<FooOneResult> promise, boolean mockSuccess) {
if (promise.isDone()) {
return;
}
if (mockSuccess) {
FooOneResult fooOneResult = new FooOneResult();
if (!promise.trySuccess(fooOneResult)) {
}
} else {
RedisConnectionException cause = new RedisConnectionException("fooOneResult is not active!");
promise.tryFailure(cause);
}
}
@Data
static class FooOneResult {
private Integer id;
private String name;
}
}
.