Skip to content

Commit

Permalink
Inline LettuceFutures.translateException(…) to prevent capturing lamb…
Browse files Browse the repository at this point in the history
…da instance creation
  • Loading branch information
mp911de committed Jul 23, 2017
1 parent 9691d2a commit 7fd4529
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 27 deletions.
20 changes: 19 additions & 1 deletion src/main/java/com/lambdaworks/redis/AbstractRedisClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,25 @@ public void shutdown() {
* @param timeUnit the unit of {@code quietPeriod} and {@code timeout}
*/
public void shutdown(long quietPeriod, long timeout, TimeUnit timeUnit) {
LettuceFutures.translateException(() -> shutdownAsync(quietPeriod, timeout, timeUnit).get(), () -> null);

try {
shutdownAsync(quietPeriod, timeout, timeUnit).get();
} catch (RuntimeException e) {
throw e;
} catch (ExecutionException e) {

if (e.getCause() instanceof RedisCommandExecutionException) {
throw new RedisCommandExecutionException(e.getCause().getMessage(), e.getCause());
}

throw new RedisException(e.getCause());
} catch (InterruptedException e) {

Thread.currentThread().interrupt();
throw new RedisCommandInterruptedException(e);
} catch (Exception e) {
throw new RedisCommandExecutionException(e);
}
}

/**
Expand Down
51 changes: 25 additions & 26 deletions src/main/java/com/lambdaworks/redis/LettuceFutures.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2011-2016 the original author or authors.
* Copyright 2011-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,8 +15,10 @@
*/
package com.lambdaworks.redis;

import java.util.concurrent.*;
import java.util.function.Supplier;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
* Utility to {@link #awaitAll(long, TimeUnit, Future[])} futures until they are done and to synchronize future execution using
Expand All @@ -28,7 +30,6 @@
public class LettuceFutures {

private LettuceFutures() {

}

/**
Expand All @@ -42,8 +43,7 @@ private LettuceFutures() {
*/
public static boolean awaitAll(long timeout, TimeUnit unit, Future<?>... futures) {

return translateException(() -> {

try {
long nanos = unit.toNanos(timeout);
long time = System.nanoTime();

Expand All @@ -61,7 +61,24 @@ public static boolean awaitAll(long timeout, TimeUnit unit, Future<?>... futures
}

return true;
}, () -> false);
} catch (RuntimeException e) {
throw e;
} catch (TimeoutException e) {
return false;
} catch (ExecutionException e) {

if (e.getCause() instanceof RedisCommandExecutionException) {
throw new RedisCommandExecutionException(e.getCause().getMessage(), e.getCause());
}

throw new RedisException(e.getCause());
} catch (InterruptedException e) {

Thread.currentThread().interrupt();
throw new RedisCommandInterruptedException(e);
} catch (Exception e) {
throw new RedisCommandExecutionException(e);
}
}

/**
Expand Down Expand Up @@ -95,33 +112,15 @@ public static <T> T awaitOrCancel(RedisFuture<T> cmd, long timeout, TimeUnit uni
@Deprecated
public static <T> T await(long timeout, TimeUnit unit, RedisFuture<T> cmd) {

return translateException(() -> {

try {
if (!cmd.await(timeout, unit)) {
cmd.cancel(true);
throw new RedisCommandTimeoutException();
}

return cmd.get();
}, () -> null);
}

/**
* Execute a {@link Callable} applying exception translation.
*
* @param callable the {@link Callable} to call.
* @param otherwiseTimeout {@link Supplier} for a fallback value when a {@link TimeoutException} is caught.
* @return result value of the {@link Callable} or {@link Supplier#get()} if a {@link TimeoutException} occurs.
* @since 4.4
*/
protected static <T> T translateException(Callable<T> callable, Supplier<T> otherwiseTimeout) {

try {
return callable.call();
} catch (RuntimeException e) {
throw e;
} catch (TimeoutException e) {
return otherwiseTimeout.get();
} catch (ExecutionException e) {

if (e.getCause() instanceof RedisCommandExecutionException) {
Expand Down

0 comments on commit 7fd4529

Please sign in to comment.