Skip to content

Commit

Permalink
Examples improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
jhalterman committed Jan 6, 2019
1 parent 72fd446 commit 58d7e4a
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 24 deletions.
Expand Up @@ -15,6 +15,7 @@
*/
package net.jodah.failsafe.internal.util;

import net.jodah.failsafe.util.Unchecked;
import net.jodah.failsafe.util.concurrent.Scheduler;

import java.util.concurrent.*;
Expand Down
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License
*/
package net.jodah.failsafe.internal.util;
package net.jodah.failsafe.util;

import java.util.concurrent.Callable;
import java.util.function.Supplier;
Expand All @@ -23,14 +23,14 @@
*
* @author Jonathan Halterman
*/
final class Unchecked {
public final class Unchecked {
private Unchecked() {
}

/**
* Returns a Supplier for the {@code callable} that wraps and re-throws any checked Exception in a RuntimeException.
*/
static <T> Supplier<T> supplier(Callable<T> callable) {
public static <T> Supplier<T> supplier(Callable<T> callable) {
return () -> {
try {
return callable.call();
Expand All @@ -39,4 +39,17 @@ static <T> Supplier<T> supplier(Callable<T> callable) {
}
};
}

/**
* Returns a Runnable for the {@code callable} that wraps and re-throws any checked Exception in a RuntimeException.
*/
public static <T> Runnable runnable(Callable<T> callable) {
return () -> {
try {
callable.call();
} catch (Exception e) {
throw e instanceof RuntimeException ? (RuntimeException) e : new RuntimeException(e);
}
};
}
}
8 changes: 4 additions & 4 deletions src/test/java/net/jodah/failsafe/examples/AsyncExample.java
Expand Up @@ -15,18 +15,18 @@
*/
package net.jodah.failsafe.examples;

import net.jodah.failsafe.Failsafe;
import net.jodah.failsafe.RetryPolicy;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import net.jodah.failsafe.Failsafe;
import net.jodah.failsafe.RetryPolicy;

public class AsyncExample {
static ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);
static RetryPolicy retryPolicy = new RetryPolicy().withDelay(100, TimeUnit.MILLISECONDS).withJitter(.25);
static RetryPolicy<Object> retryPolicy = new RetryPolicy<>().withDelay(100, TimeUnit.MILLISECONDS).withJitter(.25);
static Service service = new Service();

public static class Service {
Expand Down
13 changes: 4 additions & 9 deletions src/test/java/net/jodah/failsafe/examples/NettyExample.java
Expand Up @@ -15,29 +15,24 @@
*/
package net.jodah.failsafe.examples;

import java.util.concurrent.TimeUnit;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import net.jodah.failsafe.Failsafe;
import net.jodah.failsafe.RetryPolicy;

import java.util.concurrent.TimeUnit;

public class NettyExample {
static final String HOST = System.getProperty("host", "127.0.0.1");
static final int PORT = Integer.parseInt(System.getProperty("port", "8007"));
static final int SIZE = Integer.parseInt(System.getProperty("size", "256"));

public static void main(String... args) throws Throwable {
EventLoopGroup group = new NioEventLoopGroup();
Bootstrap bootstrap = createBootstrap(group);
RetryPolicy retryPolicy = new RetryPolicy().withDelay(1, TimeUnit.SECONDS);
RetryPolicy<Object> retryPolicy = new RetryPolicy<>().withDelay(1, TimeUnit.SECONDS);

Failsafe.with(retryPolicy).with(group).runAsyncExecution(
execution -> bootstrap.connect(HOST, PORT).addListener((ChannelFutureListener) channelFuture -> {
Expand Down
11 changes: 3 additions & 8 deletions src/test/java/net/jodah/failsafe/examples/VertxExample.java
Expand Up @@ -20,6 +20,7 @@
import io.vertx.core.eventbus.ReplyFailure;
import net.jodah.failsafe.Failsafe;
import net.jodah.failsafe.RetryPolicy;
import net.jodah.failsafe.util.Unchecked;
import net.jodah.failsafe.util.concurrent.DefaultScheduledFuture;
import net.jodah.failsafe.util.concurrent.Scheduler;

Expand All @@ -30,19 +31,13 @@ public class VertxExample {
static Vertx vertx = Vertx.vertx();

/** Create RetryPolicy to handle Vert.x failures */
static RetryPolicy retryPolicy = new RetryPolicy<>()
static RetryPolicy<Object> retryPolicy = new RetryPolicy<>()
.handleIf((ReplyException failure) -> ReplyFailure.RECIPIENT_FAILURE.equals(failure.failureType())
|| ReplyFailure.TIMEOUT.equals(failure.failureType()));

/** Adapt Vert.x timer to a Failsafe Scheduler */
static Scheduler scheduler = (callable, delay, unit) -> {
Runnable runnable = () -> {
try {
callable.call();
} catch (Exception ignore) {
}
};

Runnable runnable = Unchecked.runnable(callable);
return new DefaultScheduledFuture<Object>() {
long timerId;

Expand Down

0 comments on commit 58d7e4a

Please sign in to comment.