Search in sources :

Example 11 with Context

use of io.smallrye.mutiny.Context in project smallrye-mutiny by smallrye.

the class MultiPublishOp method connect.

@Override
public void connect(ConnectableMultiConnection connection) {
    boolean doConnect;
    PublishSubscriber<T> ps;
    // we loop because concurrent connect/disconnect and termination may change the state
    for (; ; ) {
        // retrieve the current subscriber-to-source instance
        ps = current.get();
        // if there is none yet or the current has been disposed
        if (ps == null || ps.cancelled.get()) {
            // create a new subscriber-to-source
            MultiSubscriber<?> subscriber = connection.getSubscriber();
            Context context;
            if (subscriber instanceof ContextSupport) {
                context = ((ContextSupport) subscriber).context();
            } else {
                context = Context.empty();
            }
            PublishSubscriber<T> u = new PublishSubscriber<>(current, bufferSize, context);
            // try setting it as the current subscriber-to-source
            if (!current.compareAndSet(ps, u)) {
                // and created a new subscriber-to-source as well, retry
                continue;
            }
            ps = u;
        }
        // if connect() was called concurrently, only one of them should actually
        // connect to the source
        doConnect = !ps.shouldConnect.get() && ps.shouldConnect.compareAndSet(false, true);
        break;
    }
    if (connection != null) {
        connection.accept(ps);
    }
    if (doConnect) {
        upstream.subscribe(Infrastructure.onMultiSubscription(upstream, ps));
    }
}
Also used : Context(io.smallrye.mutiny.Context) ContextSupport(io.smallrye.mutiny.subscription.ContextSupport)

Example 12 with Context

use of io.smallrye.mutiny.Context in project smallrye-mutiny by smallrye.

the class ContextPassingTest method builderUsage.

@Test
void builderUsage() {
    Context context = Context.of("X-SPAN-ID", "1234");
    // tag::builderUsage[]
    Uni.createFrom().context(ctx -> makeRequest("db1", ctx.get("X-SPAN-ID"))).subscribe().with(context, item -> handleResponse(item), err -> handleFailure(err));
// end::builderUsage[]
}
Also used : Context(io.smallrye.mutiny.Context) Test(org.junit.jupiter.api.Test)

Example 13 with Context

use of io.smallrye.mutiny.Context in project smallrye-mutiny by smallrye.

the class ContextPassingTest method sampleUsage.

@Test
void sampleUsage() {
    Multi<Integer> pipeline = Multi.createFrom().range(1, 10);
    String customerId = "1234";
    // tag::contextSampleUsage[]
    Context context = Context.of("X-CUSTOMER-ID", customerId);
    pipeline.withContext((multi, ctx) -> multi.onItem().transformToUniAndMerge(item -> makeRequest(item, ctx.get("X-CUSTOMER-ID")))).subscribe().with(context, item -> handleResponse(item), err -> handleFailure(err));
// end::contextSampleUsage[]
}
Also used : Context(io.smallrye.mutiny.Context) Test(org.junit.jupiter.api.Test) Assertions(org.junit.jupiter.api.Assertions) Context(io.smallrye.mutiny.Context) Multi(io.smallrye.mutiny.Multi) Uni(io.smallrye.mutiny.Uni) Test(org.junit.jupiter.api.Test)

Example 14 with Context

use of io.smallrye.mutiny.Context in project smallrye-mutiny by smallrye.

the class ContextPassingTest method contextManipulation.

void contextManipulation() {
    // tag::contextManipulation[]
    // Create a context using key / value pairs
    Context context = Context.of("X-CUSTOMER-ID", "1234", "X-SPAN-ID", "foo-bar-baz");
    // Get an entry
    System.out.println(context.<String>get("X-SPAN-ID"));
    // Get an entry, use a supplier for a default value if the key is not present
    System.out.println(context.getOrElse("X-SPAN-ID", () -> "<no id>"));
    // Add an entry
    context.put("foo", "bar");
    // Remove an entry
    context.delete("foo");
// end::contextManipulation[]
}
Also used : Context(io.smallrye.mutiny.Context)

Example 15 with Context

use of io.smallrye.mutiny.Context in project smallrye-mutiny by smallrye.

the class MultiWithContext method subscribe.

@Override
@SuppressWarnings("unchecked")
public void subscribe(MultiSubscriber<? super O> downstream) {
    ParameterValidation.nonNull(downstream, "downstream");
    Context context;
    if (downstream instanceof ContextSupport) {
        context = ((ContextSupport) downstream).context();
    } else {
        context = Context.empty();
    }
    Multi<O> multi;
    try {
        multi = builder.apply((Multi<I>) upstream, context);
        if (multi == null) {
            downstream.onSubscribe(Subscriptions.CANCELLED);
            downstream.onFailure(new NullPointerException("The builder function returned null"));
            return;
        }
    } catch (Throwable err) {
        downstream.onSubscribe(Subscriptions.CANCELLED);
        downstream.onFailure(err);
        return;
    }
    Multi<O> pipelineWithContext = Infrastructure.onMultiCreation(multi);
    pipelineWithContext.subscribe().withSubscriber(downstream);
}
Also used : Context(io.smallrye.mutiny.Context) ContextSupport(io.smallrye.mutiny.subscription.ContextSupport) Multi(io.smallrye.mutiny.Multi)

Aggregations

Context (io.smallrye.mutiny.Context)15 Test (org.junit.jupiter.api.Test)9 UniSubscription (io.smallrye.mutiny.subscription.UniSubscription)5 RepeatedTest (org.junit.jupiter.api.RepeatedTest)5 IOException (java.io.IOException)4 ContextSupport (io.smallrye.mutiny.subscription.ContextSupport)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 Multi (io.smallrye.mutiny.Multi)2 UniEmitter (io.smallrye.mutiny.subscription.UniEmitter)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Uni (io.smallrye.mutiny.Uni)1 UniSubscriber (io.smallrye.mutiny.subscription.UniSubscriber)1 Assertions (org.junit.jupiter.api.Assertions)1