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));
}
}
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[]
}
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[]
}
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[]
}
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);
}
Aggregations