Search in sources :

Example 1 with CheckReturnValue

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));
    }
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) IntervalMulti(io.smallrye.mutiny.operators.multi.builders.IntervalMulti) CheckReturnValue(io.smallrye.common.annotation.CheckReturnValue)

Example 2 with CheckReturnValue

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);
        });
    });
}
Also used : Uni(io.smallrye.mutiny.Uni) Cancellable(io.smallrye.mutiny.subscription.Cancellable) CompositeException(io.smallrye.mutiny.CompositeException) CheckReturnValue(io.smallrye.common.annotation.CheckReturnValue)

Example 3 with CheckReturnValue

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;
}
Also used : Context(io.vertx.core.Context) Message(org.eclipse.microprofile.reactive.messaging.Message) CheckReturnValue(io.smallrye.common.annotation.CheckReturnValue) MutinyEmitter(io.smallrye.reactive.messaging.MutinyEmitter) ProviderExceptions.ex(io.smallrye.reactive.messaging.providers.i18n.ProviderExceptions.ex) Vertx(io.vertx.core.Vertx) CompletableFuture(java.util.concurrent.CompletableFuture) ProviderLogging(io.smallrye.reactive.messaging.providers.i18n.ProviderLogging) Cancellable(io.smallrye.mutiny.subscription.Cancellable) Context(io.vertx.core.Context) ContextAwareMessage(io.smallrye.reactive.messaging.providers.locals.ContextAwareMessage) Uni(io.smallrye.mutiny.Uni) CheckReturnValue(io.smallrye.common.annotation.CheckReturnValue)

Example 4 with CheckReturnValue

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);
        });
    });
}
Also used : AmqpConnection(io.vertx.mutiny.amqp.AmqpConnection) AMQPLogging.log(io.smallrye.reactive.messaging.amqp.i18n.AMQPLogging.log) CheckReturnValue(io.smallrye.common.annotation.CheckReturnValue)

Example 5 with CheckReturnValue

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);
}
Also used : AmqpConnection(io.vertx.mutiny.amqp.AmqpConnection) CheckReturnValue(io.smallrye.common.annotation.CheckReturnValue)

Aggregations

CheckReturnValue (io.smallrye.common.annotation.CheckReturnValue)5 Uni (io.smallrye.mutiny.Uni)2 Cancellable (io.smallrye.mutiny.subscription.Cancellable)2 AmqpConnection (io.vertx.mutiny.amqp.AmqpConnection)2 CompositeException (io.smallrye.mutiny.CompositeException)1 IntervalMulti (io.smallrye.mutiny.operators.multi.builders.IntervalMulti)1 MutinyEmitter (io.smallrye.reactive.messaging.MutinyEmitter)1 AMQPLogging.log (io.smallrye.reactive.messaging.amqp.i18n.AMQPLogging.log)1 ProviderExceptions.ex (io.smallrye.reactive.messaging.providers.i18n.ProviderExceptions.ex)1 ProviderLogging (io.smallrye.reactive.messaging.providers.i18n.ProviderLogging)1 ContextAwareMessage (io.smallrye.reactive.messaging.providers.locals.ContextAwareMessage)1 Context (io.vertx.core.Context)1 Vertx (io.vertx.core.Vertx)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 Message (org.eclipse.microprofile.reactive.messaging.Message)1