-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create new strategies that can be used for both sync and async invoca…
…tions This usually consists of dropping the old synchronous implementation and renaming and adapting the old asynchronous implementation (`CompletionStage*`). Some of the new implementations still contain extra code for handling synchronous interrupts, mainly to keep all unit tests passing. The pseudo-async invocations (methods returning `java.util.concurrent.Future`) still have a few dedicated strategies (`FutureExecution`, `FutureTimeout`), but those are not supported in the programmatic API, which will benefit most from this unification. This commit also renames `InvocationContext` to `FaultToleranceContext` and `InvocationContextEvent` to `FaultToleranceEvent`.
- Loading branch information
Showing
111 changed files
with
3,760 additions
and
4,322 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
implementation/core/src/main/java/io/smallrye/faulttolerance/core/FaultToleranceContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package io.smallrye.faulttolerance.core; | ||
|
||
import java.util.Collection; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ConcurrentLinkedQueue; | ||
import java.util.concurrent.ConcurrentMap; | ||
import java.util.function.Consumer; | ||
import java.util.function.Supplier; | ||
|
||
public final class FaultToleranceContext<V> implements Supplier<Future<V>> { | ||
private final Supplier<Future<V>> delegate; | ||
private final boolean isAsync; | ||
|
||
public FaultToleranceContext(Supplier<Future<V>> delegate, boolean isAsync) { | ||
this.delegate = delegate; | ||
this.isAsync = isAsync; | ||
} | ||
|
||
@Override | ||
public Future<V> get() { | ||
return delegate.get(); | ||
} | ||
|
||
/** | ||
* Whether the guarded operation is truly asynchronous (that is, returns | ||
* a {@code CompletionStage} of the result, or some other asynchronous type). | ||
*/ | ||
public boolean isAsync() { | ||
return isAsync; | ||
} | ||
|
||
/** | ||
* Whether the guarded operation is synchronous. This includes pseudo-asynchronous | ||
* operations (that return a {@code Future} of the result). | ||
*/ | ||
public boolean isSync() { | ||
return !isAsync; | ||
} | ||
|
||
// arbitrary contextual data | ||
|
||
private final ConcurrentMap<Class<?>, Object> data = new ConcurrentHashMap<>(4); | ||
|
||
public <T> void set(Class<T> clazz, T object) { | ||
data.put(clazz, object); | ||
} | ||
|
||
public <T> T remove(Class<T> clazz) { | ||
return clazz.cast(data.remove(clazz)); | ||
} | ||
|
||
public <T> T get(Class<T> clazz) { | ||
return clazz.cast(data.get(clazz)); | ||
} | ||
|
||
public <T> T get(Class<T> clazz, T defaultValue) { | ||
T value = get(clazz); | ||
return value != null ? value : defaultValue; | ||
} | ||
|
||
// out-of-band communication between fault tolerance strategies in a single chain | ||
|
||
private final ConcurrentMap<Class<? extends FaultToleranceEvent>, Collection<Consumer<? extends FaultToleranceEvent>>> eventHandlers = new ConcurrentHashMap<>(); | ||
|
||
public <E extends FaultToleranceEvent> void registerEventHandler(Class<E> eventType, Consumer<E> handler) { | ||
eventHandlers.computeIfAbsent(eventType, ignored -> new ConcurrentLinkedQueue<>()).add(handler); | ||
} | ||
|
||
public <E extends FaultToleranceEvent> void fireEvent(E event) { | ||
Collection<Consumer<? extends FaultToleranceEvent>> handlers = eventHandlers.get(event.getClass()); | ||
if (handlers != null) { | ||
for (Consumer<? extends FaultToleranceEvent> handler : handlers) { | ||
@SuppressWarnings("unchecked") | ||
Consumer<E> consumer = (Consumer<E>) handler; | ||
consumer.accept(event); | ||
} | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...olerance/core/InvocationContextEvent.java → ...lttolerance/core/FaultToleranceEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package io.smallrye.faulttolerance.core; | ||
|
||
public interface InvocationContextEvent { | ||
public interface FaultToleranceEvent { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 0 additions & 61 deletions
61
implementation/core/src/main/java/io/smallrye/faulttolerance/core/InvocationContext.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.