@ApplicationScoped
public class FaultToleranceBean {
int i = 0;
@Retry(maxRetries = 2)
public Runnable doWork() {
Runnable mainService = () -> serviceA(); // This unreliable service sometimes succeeds but
// sometimes throws a RuntimeException
return mainService;
}
}
RetryPolicy rp = FaultToleranceFactory.getInstance(RetryPolicy.class).retryOn(RuntimeException.class)
.withDelay(2, TimeUnit.SECONDS)
.withMaxRetries(2);
public void myMethod() {
....
// Set up a Duration of 1 second
Duration duration = Duration.ofSeconds(1); //
// FaultTolerance retry policy with max of 3 retries and 1 millisecond between retries
retryPolicy = retryPolicy.retryOn(Exception.class).withDelay(duration).withMaxRetries(3);
// Create an Execution object. Configure it to connect to a "Primary", with our RetryPolicy
// and with a fallback to connect to a "Backup"
Executor executor = FaultToleranceProvider.getFaultToleranceType(Executor.class);
// Main Service
Callable<Connection> mainService = () -> connectToPrimary();
// FaultTolerance with fallback to a Backup
executor.with(retryPolicy).get(mainService);
...
}
protected Connection connectToPrimary() throws ConnectException {
System.out.println("Main Service has been called");
out.println("Main Service has been called");
if (true)
throw new ConnectException();
// Shouldn't get here
return null;
}
In CDI-based approach, the annotations apply to the method connectToPrimary not myMethod.
public void myMethod() {
...
Connection c = connectToPrimary()
}
@Retry(maxRetries=3, delay=1)
protected Connection connectToPrimary() throws ConnectException {
System.out.println("Main Service has been called");
out.println("Main Service has been called");
if (true)
throw new ConnectException();
// Shouldn't get here
return null;
}
To view this discussion on the web visit https://groups.google.com/d/msgid/microprofile/69e2640b-00dc-4ec9-a96f-b644d09765ce%40googlegroups.com.--
You received this message because you are subscribed to the Google Groups "MicroProfile" group.
To unsubscribe from this group and stop receiving emails from it, send an email to microprofile+unsubscribe@googlegroups.com.
To post to this group, send email to microp...@googlegroups.com.
Ken
To unsubscribe from this group and stop receiving emails from it, send an email to microprofile...@googlegroups.com.
E.g.:
@Retry
public Runnable remoteAction() {...}
Calling remoteAction().run() would apply retry policy around the action automatically.
This adds flexibility and supports functional programming nicely. The same concept could be applied with JavaSE by providing the higher order functions (functional interceptors) e.g. as static methods to wrap runnables.
With JavaSE:
Runnable actionWithRetries = Intercept.retry(new Runnable() {...});
actionWithRetries.run(); // would be intercepted by the retry higher order function
I'll try creating pull request to give more examples.
--Ondrej
public class MyGateway {
@Workflow(
name = "display-book-info",
calls = {
@CallReference("#connectToPrimary()"),
@CallReference("#restCall()")
}
)
@GET()
@Path("frontend-gateway")
@Produces("application/json")
public String myGatewayMethod() {
Callable<Connection> dbCall = this::connectToPrimary;
Callable<String> restCall = this::restCall;
// ...
return null;
}
@Retry(maxRetries=3, delay=1)
protected Connection connectToPrimary() throws ConnectException {
return null;
}
@Asynchronous
@CircuitBreaker(delay = 5, delayUnit = ChronoUnit.SECONDS)
protected String restCall() throws Exception {
return null;
}
}
public class MyAbstractGateway {
@ServiceReference(name="dbResults", endpoint = "/books")
WebTarget dbResults;
@Asynchronous
@ServiceReference(name="images", endpoint = "/books/images")
WebTarget images;
@ServiceReference(name="reviews", endpoint = "/books/reviews")
WebTarget reviews;
@Workflow(
name = "display-book-info",
services = {
@ServiceReference(name="dbResults", endpoint = "/books"),
@ServiceReference(name="images", endpoint = "/books/images"),
@ServiceReference(name="reviews", endpoint = "/books/reviews")
}
)
@GET
@Path("frontend-gateway")
@Produces("application/json")
public String myGatewayMethod() {
dbResults.request().get(...);
return null;
}
}
public class MyAbstractGateway {
@Retry(maxRetries=3, delay=1)
@ServiceReference(name="dbResults", endpoint = "/books")
WebTarget dbResults;
@Asynchronous
@CircuitBreaker(delay = 5, delayUnit = ChronoUnit.SECONDS)
@ServiceReference(name="images", endpoint = "/books/images")
WebTarget images;
@Retry(maxRetries=3, delay=1)
@ServiceReference(name="reviews", endpoint = "/books/reviews")
WebTarget reviews;
Ok, I'm beginning to see the syntax. I feel there needs to be more of a description about the workflow a method is invoking, and this has only increased given the service mesh for micro services announcement by Google, IBM, and Lyft, with support from Red Hat and others:https://github.com/istio
A question is how the MicroProfile effort fits into a service mesh architecture? I can see both externalization of fault-tolerance and injection of either policies or service proxies needing to be supported.
...
Ok, I'm beginning to see the syntax. I feel there needs to be more of a description about the workflow a method is invoking, and this has only increased given the service mesh for micro services announcement by Google, IBM, and Lyft, with support from Red Hat and others:https://github.com/istio
A question is how the MicroProfile effort fits into a service mesh architecture? I can see both externalization of fault-tolerance and injection of either policies or service proxies needing to be supported.
--
You received this message because you are subscribed to the Google Groups "MicroProfile" group.
To unsubscribe from this group and stop receiving emails from it, send an email to microprofile+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/microprofile/914a3b99-8b1b-4f1b-b8a6-6b72bc27c3a6%40googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "MicroProfile" group.
To unsubscribe from this group and stop receiving emails from it, send an email to microprofile+unsubscribe@googlegroups.com.
To post to this group, send email to microp...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/microprofile/93152a44-eca0-42a9-af7e-60510afc2f91%40googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to microprofile...@googlegroups.com.
To post to this group, send email to microp...@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to microprofile...@googlegroups.com.
destination: "ratings.default.svc.cluster.local"
route:
- tags:
version: v1
httpReqRetries:
simpleRetry:
attempts: 3