use of io.smallrye.mutiny.Uni in project sandbox by 5733d9e2be6485d52ffa08870cabdee0.
the class AWSVaultServiceImpl method createOrReplace.
@Override
public Uni<Void> createOrReplace(EventBridgeSecret secret) {
CreateSecretRequest createSecretRequest = CreateSecretRequest.builder().name(secret.getId()).secretString(Json.encode(secret.getValues())).build();
return Uni.createFrom().future(asyncClient.createSecret(createSecretRequest)).replaceWithVoid().onFailure(e -> !(e instanceof ResourceExistsException)).retry().withJitter(DEFAULT_JITTER).withBackOff(DEFAULT_BACKOFF).atMost(MAX_RETRIES).onFailure(ResourceExistsException.class).recoverWithUni(() -> replaceSecret(secret)).onFailure().transform(e -> new VaultException("Could not replace secret '%s' in AWS Vault", e)).invoke(() -> {
CACHE.put(secret.getId(), secret);
LOGGER.debug("Secret '{}' created in AWS Vault", secret.getId());
});
}
use of io.smallrye.mutiny.Uni in project smallrye-mutiny by smallrye.
the class MultiOnFailure method call.
/**
* Produces a new {@link Multi} invoking the given @{code action} when a {@code failure} event is received.
* <p>
* Unlike {@link #invoke(Consumer)}, the passed function returns a {@link Uni}. When the produced {@code Uni} sends
* its result, the result is discarded, and the original {@code failure} is forwarded downstream. If the produced
* {@code Uni} fails, a {@link io.smallrye.mutiny.CompositeException} composed by the original failure and the
* caught failure is propagated downstream.
* <p>
* If the asynchronous action throws an exception, this exception is propagated downstream as a
* {@link io.smallrye.mutiny.CompositeException} composed with the original failure and the caught exception.
* <p>
* This method preserves the order of the items, meaning that the downstream received the items in the same order
* as the upstream has emitted them.
*
* @param action the function taking the failure and returning a {@link Uni}, must not be {@code null}
* @return the new {@link Multi}
*/
@CheckReturnValue
public Multi<T> call(Function<Throwable, Uni<?>> action) {
Function<Throwable, Uni<?>> actual = Infrastructure.decorate(nonNull(action, "action"));
return recoverWithMulti(failure -> {
Uni<?> uni = actual.apply(failure);
if (uni == null) {
throw new NullPointerException("The `action` produced a `null` Uni");
}
return Multi.createFrom().emitter(emitter -> {
Cancellable cancellable = uni.subscribe().with(success -> emitter.fail(failure), subFailure -> emitter.fail(new CompositeException(failure, subFailure)));
emitter.onTermination(cancellable::cancel);
});
});
}
use of io.smallrye.mutiny.Uni in project smallrye-mutiny by smallrye.
the class MultiFromResourceFromUniTest method testThatOnFailureFinalizerIsNotCallIfResourceSupplierThrowsAnException.
@Test
public void testThatOnFailureFinalizerIsNotCallIfResourceSupplierThrowsAnException() {
AssertSubscriber<Integer> subscriber = AssertSubscriber.create(20);
Supplier<Uni<Integer>> supplier = () -> {
throw new NullPointerException("boom");
};
AtomicInteger onFailure = new AtomicInteger();
AtomicInteger onComplete = new AtomicInteger();
AtomicInteger onCancellation = new AtomicInteger();
BiFunction<Integer, Throwable, Uni<Void>> onFailureCallback = (s, f) -> {
onFailure.set(s);
return Uni.createFrom().voidItem();
};
Function<Integer, Uni<Void>> onCompletionCallback = s -> {
onComplete.set(s);
return Uni.createFrom().voidItem();
};
Function<Integer, Uni<Void>> onCancellationCallback = s -> {
onCancellation.set(s);
return Uni.createFrom().voidItem();
};
Multi.createFrom().resourceFromUni(supplier, r -> Multi.createFrom().range(r, 11)).withFinalizer(onCompletionCallback, onFailureCallback, onCancellationCallback).subscribe(subscriber);
subscriber.assertFailedWith(NullPointerException.class, "boom");
assertThat(onFailure).hasValue(0);
assertThat(onCancellation).hasValue(0);
assertThat(onComplete).hasValue(0);
}
use of io.smallrye.mutiny.Uni in project smallrye-mutiny by smallrye.
the class ResourceMulti method subscribe.
@Override
public void subscribe(MultiSubscriber<? super I> subscriber) {
R resource;
try {
resource = resourceSupplier.get();
if (resource == null) {
throw new IllegalArgumentException(ParameterValidation.SUPPLIER_PRODUCED_NULL);
}
} catch (Throwable e) {
Subscriptions.fail(subscriber, e);
return;
}
Publisher<? extends I> stream;
try {
stream = streamSupplier.apply(resource);
if (stream == null) {
throw new IllegalArgumentException(ParameterValidation.SUPPLIER_PRODUCED_NULL);
}
} catch (Throwable e) {
try {
Uni<Void> uni = onFailure.apply(resource, e);
if (uni == null) {
Subscriptions.fail(subscriber, new NullPointerException("Unable to call the finalizer - it returned `null`"));
} else {
uni.subscribe().with(completed -> Subscriptions.fail(subscriber, e), failed -> Subscriptions.fail(subscriber, new CompositeException(e, failed)));
}
} catch (Throwable ex) {
Subscriptions.fail(subscriber, new CompositeException(e, ex));
return;
}
Subscriptions.fail(subscriber, e);
return;
}
ResourceSubscriber<I, R> us = new ResourceSubscriber<>(subscriber, resource, onCompletion, onFailure, onCancellation);
stream.subscribe(us);
}
use of io.smallrye.mutiny.Uni in project smallrye-mutiny by smallrye.
the class MultiRepetitionTest method testWithEmitter.
@Test
public void testWithEmitter() {
AtomicInteger shared = new AtomicInteger();
Multi<Integer> multi = Multi.createBy().repeating().<Integer>uni(emitter -> emitter.complete(shared.incrementAndGet())).atMost(2);
assertThat(shared).hasValue(0);
AssertSubscriber<Integer> subscriber = multi.subscribe().withSubscriber(AssertSubscriber.create(1));
subscriber.assertItems(1);
assertThat(shared).hasValue(1);
subscriber.request(1);
subscriber.assertCompleted().assertItems(1, 2);
}
Aggregations