本文整理汇总了Java中io.vertx.circuitbreaker.CircuitBreaker.create方法的典型用法代码示例。如果您正苦于以下问题:Java CircuitBreaker.create方法的具体用法?Java CircuitBreaker.create怎么用?Java CircuitBreaker.create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.vertx.circuitbreaker.CircuitBreaker
的用法示例。
在下文中一共展示了CircuitBreaker.create方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: example2
import io.vertx.circuitbreaker.CircuitBreaker; //导入方法依赖的package包/类
public void example2(Vertx vertx) {
CircuitBreaker breaker = CircuitBreaker.create("my-circuit-breaker", vertx,
new CircuitBreakerOptions().setMaxFailures(5).setTimeout(2000)
);
breaker.<String>execute(future -> {
vertx.createHttpClient().getNow(8080, "localhost", "/", response -> {
if (response.statusCode() != 200) {
future.fail("HTTP error");
} else {
response
.exceptionHandler(future::fail)
.bodyHandler(buffer -> {
future.complete(buffer.toString());
});
}
});
}).setHandler(ar -> {
// Do something with the result
});
}
示例2: example3
import io.vertx.circuitbreaker.CircuitBreaker; //导入方法依赖的package包/类
public void example3(Vertx vertx) {
CircuitBreaker breaker = CircuitBreaker.create("my-circuit-breaker", vertx,
new CircuitBreakerOptions().setMaxFailures(5).setTimeout(2000)
);
breaker.executeWithFallback(
future -> {
vertx.createHttpClient().getNow(8080, "localhost", "/", response -> {
if (response.statusCode() != 200) {
future.fail("HTTP error");
} else {
response
.exceptionHandler(future::fail)
.bodyHandler(buffer -> {
future.complete(buffer.toString());
});
}
});
}, v -> {
// Executed when the circuit is opened
return "Hello";
})
.setHandler(ar -> {
// Do something with the result
});
}
示例3: testOk
import io.vertx.circuitbreaker.CircuitBreaker; //导入方法依赖的package包/类
@Test
@Repeat(5)
public void testOk() {
breaker = CircuitBreaker.create("test", vertx, new CircuitBreakerOptions());
assertThat(breaker.state()).isEqualTo(CircuitBreakerState.CLOSED);
AtomicBoolean operationCalled = new AtomicBoolean();
AtomicReference<String> completionCalled = new AtomicReference<>();
breaker.<String>execute(fut -> {
operationCalled.set(true);
fut.complete("hello");
}).setHandler(ar -> completionCalled.set(ar.result()));
await().until(operationCalled::get);
await().until(() -> completionCalled.get().equalsIgnoreCase("hello"));
}
示例4: testWithUserFutureOk
import io.vertx.circuitbreaker.CircuitBreaker; //导入方法依赖的package包/类
@Test
@Repeat(5)
public void testWithUserFutureOk() {
breaker = CircuitBreaker.create("test", vertx, new CircuitBreakerOptions());
assertThat(breaker.state()).isEqualTo(CircuitBreakerState.CLOSED);
AtomicBoolean operationCalled = new AtomicBoolean();
AtomicReference<String> completionCalled = new AtomicReference<>();
Future<String> userFuture = Future.future();
userFuture.setHandler(ar ->
completionCalled.set(ar.result()));
breaker.executeAndReport(userFuture, fut -> {
operationCalled.set(true);
fut.complete("hello");
});
await().until(operationCalled::get);
await().until(() -> completionCalled.get().equalsIgnoreCase("hello"));
}
示例5: testWithFailedCommands
import io.vertx.circuitbreaker.CircuitBreaker; //导入方法依赖的package包/类
@Test
public void testWithFailedCommands(TestContext tc) {
breaker = CircuitBreaker.create("some-circuit-breaker", vertx);
Async async = tc.async();
Future<Void> command1 = breaker.execute(commandThatFails());
Future<Void> command2 = breaker.execute(commandThatWorks());
Future<Void> command3 = breaker.execute(commandThatWorks());
Future<Void> command4 = breaker.execute(commandThatFails());
CompositeFuture.join(command1, command2, command3, command4)
.setHandler(ar -> {
assertThat(metrics())
.contains("name", "some-circuit-breaker")
.contains("state", CircuitBreakerState.CLOSED.name())
.contains("totalErrorCount", 2) // Failure + Timeout + Exception
.contains("totalSuccessCount", 2)
.contains("totalTimeoutCount", 0)
.contains("totalExceptionCount", 0)
.contains("totalFailureCount", 2)
.contains("totalOperationCount", 4)
.contains("totalSuccessPercentage", 50)
.contains("totalErrorPercentage", 50);
async.complete();
});
}
示例6: addCircuitBreaker
import io.vertx.circuitbreaker.CircuitBreaker; //导入方法依赖的package包/类
public void addCircuitBreaker(Vertx vertx, long timeout, int maxfail, long resettimeout){
if(this.isDev)
throw new IllegalArgumentException("Can't set circuitbreaker for dev node.");
breaker = CircuitBreaker.create(app.name+"-"+host+":"+port, vertx,
new CircuitBreakerOptions()
.setMaxFailures(maxfail) // number of failure before opening the circuit
.setTimeout(timeout) // consider a failure if the operation does not succeed in time
.setFallbackOnFailure(false) // do we call the fallback on failure
.setResetTimeout(resettimeout) // time spent in open state before attempting to re-try
);
}
示例7: testWithFailingOperationWithFuture
import io.vertx.circuitbreaker.CircuitBreaker; //导入方法依赖的package包/类
@Test
public void testWithFailingOperationWithFuture() {
breaker = CircuitBreaker.create("test", vertx, new CircuitBreakerOptions()
.setFallbackOnFailure(true));
AtomicInteger result = new AtomicInteger();
Future<Integer> operationResultFuture = Future.future();
operationResultFuture.setHandler(ar -> result.set(ar.result()));
breaker.executeAndReportWithFallback(operationResultFuture, MyAsyncOperations::fail, t -> -1);
await().untilAtomic(result, is(-1));
}
示例8: testWithRetrySetToFive
import io.vertx.circuitbreaker.CircuitBreaker; //导入方法依赖的package包/类
@Test
public void testWithRetrySetToFive() {
CircuitBreaker breaker = CircuitBreaker.create("my-circuit-breaker", vertx,
new CircuitBreakerOptions().setMaxFailures(5).setMaxRetries(5));
AtomicInteger counter = new AtomicInteger();
breaker.execute(future -> {
counter.incrementAndGet();
future.fail("FAILED");
}).setHandler(ar -> {
});
await().untilAtomic(counter, is(6));
}
示例9: testWithRetrySetToZero
import io.vertx.circuitbreaker.CircuitBreaker; //导入方法依赖的package包/类
@Test
public void testWithRetrySetToZero() {
CircuitBreaker breaker = CircuitBreaker.create("my-circuit-breaker", vertx,
new CircuitBreakerOptions().setMaxFailures(5).setMaxRetries(0));
AtomicInteger counter = new AtomicInteger();
breaker.execute(future -> {
counter.incrementAndGet();
future.fail("FAILED");
}).setHandler(ar -> {
});
await().untilAtomic(counter, is(1));
}
示例10: testWithoutRetry
import io.vertx.circuitbreaker.CircuitBreaker; //导入方法依赖的package包/类
@Test
public void testWithoutRetry() {
CircuitBreaker breaker = CircuitBreaker.create("my-circuit-breaker", vertx,
new CircuitBreakerOptions().setMaxFailures(5));
AtomicInteger counter = new AtomicInteger();
breaker.execute(future -> {
counter.incrementAndGet();
future.fail("FAILED");
}).setHandler(ar -> {
});
await().untilAtomic(counter, is(1));
}
示例11: testWithOperationWithCompletionHandler
import io.vertx.circuitbreaker.CircuitBreaker; //导入方法依赖的package包/类
@Test
public void testWithOperationWithCompletionHandler() {
breaker = CircuitBreaker.create("test", vertx, new CircuitBreakerOptions());
AtomicInteger result = new AtomicInteger();
breaker.executeCommandWithFallback(fut -> {
MyAsyncOperations.operation(1, 1, fut.completer());
}, v -> 0, ar -> result.set(ar.result()));
await().untilAtomic(result, is(2));
}
示例12: testWithOperationWithFuture
import io.vertx.circuitbreaker.CircuitBreaker; //导入方法依赖的package包/类
@Test
public void testWithOperationWithFuture() {
breaker = CircuitBreaker.create("test", vertx, new CircuitBreakerOptions()
.setFallbackOnFailure(true));
AtomicInteger result = new AtomicInteger();
Future<Integer> operationResultFuture = Future.future();
operationResultFuture.setHandler(ar -> {
result.set(ar.result());
});
breaker.executeAndReport(operationResultFuture, future -> MyAsyncOperations.operation(future, 1, 1));
await().untilAtomic(result, is(2));
}
示例13: testWithOperationWithHandler
import io.vertx.circuitbreaker.CircuitBreaker; //导入方法依赖的package包/类
@Test
public void testWithOperationWithHandler() {
breaker = CircuitBreaker.create("test", vertx, new CircuitBreakerOptions());
AtomicInteger result = new AtomicInteger();
breaker.<Integer>executeWithFallback(fut -> {
MyAsyncOperations.operation(1, 1, fut.completer());
}, v -> 0)
.setHandler(ar -> result.set(ar.result()));
await().untilAtomic(result, is(2));
}
示例14: testWhenFallbackThrowsAnException
import io.vertx.circuitbreaker.CircuitBreaker; //导入方法依赖的package包/类
@Test
public void testWhenFallbackThrowsAnException() {
CircuitBreakerOptions options = new CircuitBreakerOptions()
.setResetTimeout(5000)
.setFallbackOnFailure(true);
breaker = CircuitBreaker.create("test", vertx, options);
assertThat(breaker.state()).isEqualTo(CircuitBreakerState.CLOSED);
List<AsyncResult<String>> results = new ArrayList<>();
for (int i = 0; i < options.getMaxFailures(); i++) {
breaker.<String>executeWithFallback(
future -> future.fail("expected failure"),
t -> {
throw new RuntimeException("boom");
})
.setHandler(results::add);
}
await().until(() -> results.size() == options.getMaxFailures());
results.forEach(ar -> {
assertThat(ar.failed()).isTrue();
assertThat(ar.cause()).isNotNull().hasMessage("boom");
});
results.clear();
await().until(() -> breaker.state() == CircuitBreakerState.OPEN);
breaker.<String>executeWithFallback(
future -> future.fail("expected failure"),
t -> {
throw new RuntimeException("boom");
})
.setHandler(results::add);
await().until(() -> results.size() == 1);
results.forEach(ar -> {
assertThat(ar.failed()).isTrue();
assertThat(ar.cause()).isNotNull().hasMessage("boom");
});
}
示例15: testTimeout
import io.vertx.circuitbreaker.CircuitBreaker; //导入方法依赖的package包/类
@Test
public void testTimeout() {
CircuitBreakerOptions options = new CircuitBreakerOptions().setTimeout(100).setMaxFailures(2);
breaker = CircuitBreaker.create("test", vertx, options);
assertThat(breaker.state()).isEqualTo(CircuitBreakerState.CLOSED);
AtomicInteger count = new AtomicInteger();
for (int i = 0; i < options.getMaxFailures(); i++) {
breaker.execute(future ->
client.getNow(8080, "localhost", "/long", response -> {
count.incrementAndGet();
future.complete();
}));
}
await().untilAtomic(count, is(options.getMaxFailures()));
assertThat(breaker.state()).isEqualTo(CircuitBreakerState.OPEN);
Future<String> result = Future.future();
breaker.executeAndReportWithFallback(result, future ->
client.getNow(8080, "localhost", "/long", response -> {
System.out.println("Got response");
future.complete();
}), v -> "fallback");
await().until(() -> result.result().equals("fallback"));
assertThat(breaker.state()).isEqualTo(CircuitBreakerState.OPEN);
}