use of io.smallrye.common.annotation.CheckReturnValue in project smallrye-mutiny by smallrye.
the class MultiTimePeriod method every.
@CheckReturnValue
public Multi<Long> every(Duration duration) {
validate(duration, "duration");
ScheduledExecutorService executorService = this.executor;
if (executorService == null) {
executorService = Infrastructure.getDefaultWorkerPool();
}
if (initialDelay != null) {
return Infrastructure.onMultiCreation(new IntervalMulti(initialDelay, duration, executorService));
} else {
return Infrastructure.onMultiCreation(new IntervalMulti(duration, executorService));
}
}
use of io.smallrye.common.annotation.CheckReturnValue 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.common.annotation.CheckReturnValue in project smallrye-reactive-messaging by smallrye.
the class MutinyEmitterImpl method send.
@Override
@CheckReturnValue
public Uni<Void> send(T payload) {
if (payload == null) {
throw ex.illegalArgumentForNullValue();
}
// If we are running on a Vert.x I/O thread, we need to capture the context to switch back
// during the emission.
Context context = Vertx.currentContext();
Uni<Void> uni = Uni.createFrom().emitter(e -> emit(ContextAwareMessage.of(payload).withAck(() -> {
e.complete(null);
return CompletableFuture.completedFuture(null);
}).withNack(reason -> {
e.fail(reason);
return CompletableFuture.completedFuture(null);
})));
if (context != null) {
uni = uni.emitOn(runnable -> context.runOnContext(x -> runnable.run()));
}
return uni;
}
use of io.smallrye.common.annotation.CheckReturnValue in project smallrye-reactive-messaging by smallrye.
the class ConnectionHolder method getOrEstablishConnection.
@CheckReturnValue
public Uni<AmqpConnection> getOrEstablishConnection() {
return Uni.createFrom().item(() -> {
CurrentConnection connection = holder.get();
if (connection != null && connection.connection != null && !connection.connection.isDisconnected()) {
return connection.connection;
} else {
return null;
}
}).onItem().ifNull().switchTo(() -> {
// we don't have a connection, try to connect.
Integer retryInterval = configuration.getReconnectInterval();
Integer retryAttempts = configuration.getReconnectAttempts();
CurrentConnection reference = holder.get();
if (reference != null && reference.connection != null && !reference.connection.isDisconnected()) {
AmqpConnection connection = reference.connection;
return Uni.createFrom().item(connection);
}
return client.connect().onSubscription().invoke(s -> log.establishingConnection()).onItem().transform(conn -> {
log.connectionEstablished();
holder.set(new CurrentConnection(conn, Vertx.currentContext()));
conn.exceptionHandler(t -> {
holder.set(null);
log.connectionFailure(t);
// The callback failure allows propagating the failure downstream,
// as we are disconnected from the flow.
Consumer<Throwable> c;
synchronized (this) {
c = callback;
}
if (c != null) {
c.accept(t);
}
});
// handle the case we are already disconnected.
if (conn.isDisconnected() || holder.get() == null) {
// Throwing the exception would trigger a retry.
holder.set(null);
throw ex.illegalStateConnectionDisconnected();
}
return conn;
}).onFailure().invoke(log::unableToConnectToBroker).onFailure().retry().withBackOff(ofSeconds(1), ofSeconds(retryInterval)).atMost(retryAttempts).onFailure().invoke(t -> {
holder.set(null);
log.unableToRecoverFromConnectionDisruption(t);
});
});
}
use of io.smallrye.common.annotation.CheckReturnValue in project smallrye-reactive-messaging by smallrye.
the class ConnectionHolder method isConnected.
@CheckReturnValue
public Uni<Boolean> isConnected() {
CurrentConnection connection = holder.get();
if (connection == null) {
return Uni.createFrom().item(false);
}
AmqpConnection underlying = connection.connection;
if (underlying == null) {
return Uni.createFrom().item(false);
}
return Uni.createFrom().item(() -> !underlying.isDisconnected()).runSubscriptionOn(connection.context::runOnContext);
}
Aggregations