org.reactivestreams.Processor Java Examples

The following examples show how to use org.reactivestreams.Processor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: USetTest.java    From wurmloch-crdt with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldHandleRemoveCommands() {
    // given:
    final UUID uuid1 = UUID.randomUUID();
    final Processor<USet.USetCommand<UUID>, USet.USetCommand<UUID>> inputStream = ReplayProcessor.create();
    final TestSubscriber<CrdtCommand> subscriber = TestSubscriber.create();
    final USet<UUID> set = new USet<>("ID_1");
    set.subscribeTo(inputStream);
    set.subscribe(subscriber);

    final USet.AddCommand<UUID> command1 = new USet.AddCommand<>(set.getCrdtId(), uuid1);
    final USet.RemoveCommand<UUID> command2 = new USet.RemoveCommand<>(set.getCrdtId(), uuid1);

    // when:
    inputStream.onNext(command1);
    inputStream.onNext(command2);

    // then:
    assertThat(set, empty());
    assertThat(subscriber.valueCount(), is(2));
    subscriber.assertNotComplete();
    subscriber.assertNoErrors();
}
 
Example #2
Source File: RxBusSenderBuilder.java    From RxBus2 with Apache License 2.0 6 votes vote down vote up
private boolean sendToKeyBoundBus(RxQueueKey key, Object event)
{
    RxQueueKey keyToUse = key.clone();
    boolean send = false;
    Processor processor;
    if (mKey instanceof String)
        keyToUse.withId((String)mKey);
    else if (mKey instanceof Integer)
        keyToUse.withId((Integer)mKey);
    processor = RxBus.getInstance().getProcessor(keyToUse, false);

    // only send event, if processor exists => this means someone has at least once subscribed to it
    if (processor != null)
    {
        if (mCast == null)
            processor.onNext(event);
        else
            processor.onNext(mCast.cast(event));
        send = true;
    }
    return send;
}
 
Example #3
Source File: Engine.java    From smallrye-reactive-streams-operators with Apache License 2.0 6 votes vote down vote up
@Override
public <T, R> SubscriberWithCompletionStage<T, R> buildSubscriber(Graph graph) {
    Processor<T, T> processor = new ConnectableProcessor<>();
    Flowable<T> flowable = Flowable.fromPublisher(processor);
    for (Stage stage : graph.getStages()) {
        Operator operator = Stages.lookup(stage);
        if (operator instanceof ProcessorOperator) {
            flowable = applyProcessors(flowable, stage, (ProcessorOperator) operator);
        } else if (operator instanceof TerminalOperator) {
            CompletionStage<R> result = applySubscriber(Transformer.apply(flowable), stage,
                    (TerminalOperator) operator);
            return new DefaultSubscriberWithCompletionStage<>(processor, result);
        } else {
            throw new UnsupportedStageException(stage);
        }
    }

    throw new IllegalArgumentException("The graph does not have a valid final stage");
}
 
Example #4
Source File: TwoPSetTest.java    From wurmloch-crdt with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldHandleAddCommands() {
    // given:
    final Processor<TwoPSet.TwoPSetCommand<String>, TwoPSet.TwoPSetCommand<String>> inputStream = ReplayProcessor.create();
    final TestSubscriber<CrdtCommand> subscriber = TestSubscriber.create();
    final TwoPSet<String> set = new TwoPSet<>("ID_1");
    set.subscribeTo(inputStream);
    set.subscribe(subscriber);

    final TwoPSet.AddCommand<String> command1 = new TwoPSet.AddCommand<>(set.getCrdtId(), "1");
    final TwoPSet.AddCommand<String> command2 = new TwoPSet.AddCommand<>(set.getCrdtId(), "2");
    final TwoPSet.AddCommand<String> command3 = new TwoPSet.AddCommand<>(set.getCrdtId(), "1");

    // when:
    inputStream.onNext(command1);
    inputStream.onNext(command2);
    inputStream.onNext(command3);

    // then:
    assertThat(set, hasSize(2));
    assertThat(subscriber.valueCount(), is(2));
    subscriber.assertNotComplete();
    subscriber.assertNoErrors();
}
 
Example #5
Source File: TwoPSetTest.java    From wurmloch-crdt with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldHandleRemoveCommandArrivesBeforeAddCommand() {
    // given:
    final Processor<TwoPSet.TwoPSetCommand<String>, TwoPSet.TwoPSetCommand<String>> inputStream = ReplayProcessor.create();
    final TestSubscriber<CrdtCommand> subscriber = TestSubscriber.create();
    final TwoPSet<String> set = new TwoPSet<>("ID_1");
    set.subscribeTo(inputStream);
    set.subscribe(subscriber);

    final TwoPSet.RemoveCommand<String> command1 = new TwoPSet.RemoveCommand<>(set.getCrdtId(), "1");
    final TwoPSet.AddCommand<String> command2 = new TwoPSet.AddCommand<>(set.getCrdtId(), "1");
    final TwoPSet.AddCommand<String> command3 = new TwoPSet.AddCommand<>(set.getCrdtId(), "1");

    // when:
    inputStream.onNext(command1);
    inputStream.onNext(command2);
    inputStream.onNext(command3);

    // then:
    assertThat(set, empty());
    assertThat(subscriber.valueCount(), is(1));
    subscriber.assertNotComplete();
    subscriber.assertNoErrors();
}
 
Example #6
Source File: GSetTest.java    From wurmloch-crdt with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldHandleAddCommands() {
    // given:
    final Processor<GSet.AddCommand<String>, GSet.AddCommand<String>> inputStream = ReplayProcessor.create();
    final TestSubscriber<CrdtCommand> subscriber = TestSubscriber.create();
    final GSet<String> set = new GSet<>("ID_1");
    set.subscribeTo(inputStream);
    set.subscribe(subscriber);

    final GSet.AddCommand<String> command1 = new GSet.AddCommand<>(set.getCrdtId(), "1");
    final GSet.AddCommand<String> command2 = new GSet.AddCommand<>(set.getCrdtId(), "2");
    final GSet.AddCommand<String> command3 = new GSet.AddCommand<>(set.getCrdtId(), "1");

    // when:
    inputStream.onNext(command1);
    inputStream.onNext(command2);
    inputStream.onNext(command3);

    // then:
    assertThat(set, hasSize(2));
    assertThat(subscriber.valueCount(), is(2));
    subscriber.assertNotComplete();
    subscriber.assertNoErrors();
}
 
Example #7
Source File: FluxWindow.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@Override
public void onError(Throwable t) {
	if (done) {
		Operators.onErrorDropped(t, actual.currentContext());
		return;
	}
	done = true;

	for (Processor<T, T> w : this) {
		w.onError(t);
	}
	clear();

	error = t;
	drain();
}
 
Example #8
Source File: SerializedProcessorTest.java    From smallrye-mutiny with Apache License 2.0 6 votes vote down vote up
@Test(invocationCount = 20)
public void verifyOnNextOnCompleteThreadSafety() {
    final Processor<Integer, Integer> processor = UnicastProcessor.<Integer> create().serialized();
    MultiAssertSubscriber<Integer> subscriber = MultiAssertSubscriber.create(100);
    processor.subscribe(subscriber);

    Runnable r1 = () -> {
        processor.onNext(1);
        processor.onComplete();
    };
    Runnable r2 = processor::onComplete;

    new Thread(r1).start();
    new Thread(r2).start();

    subscriber.await();
    subscriber
            .assertSubscribed()
            .assertCompletedSuccessfully();

    if (subscriber.items().size() != 0) {
        assertThat(subscriber.items()).containsExactly(1);
    }
}
 
Example #9
Source File: GSetTest.java    From wurmloch-crdt with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldHandleDuplicateCommands() {
    // given:
    final Processor<GSet.AddCommand<String>, GSet.AddCommand<String>> inputStream = ReplayProcessor.create();
    final TestSubscriber<CrdtCommand> subscriber = TestSubscriber.create();
    final GSet<String> set = new GSet<>("ID_1");
    set.subscribeTo(inputStream);
    set.subscribe(subscriber);

    final GSet.AddCommand<String> command = new GSet.AddCommand<>(set.getCrdtId(), "1");

    // when:
    inputStream.onNext(command);
    inputStream.onNext(command);

    // then:
    assertThat(set, hasSize(1));
    assertThat(subscriber.valueCount(), is(1));
    subscriber.assertNotComplete();
    subscriber.assertNoErrors();
}
 
Example #10
Source File: SerializedProcessorTest.java    From smallrye-mutiny with Apache License 2.0 6 votes vote down vote up
@Test(invocationCount = 50)
public void verifyOnErrorThreadSafety() {
    Exception failure = new Exception("boom");
    final Processor<Integer, Integer> processor = UnicastProcessor.<Integer> create().serialized();
    MultiAssertSubscriber<Integer> subscriber = MultiAssertSubscriber.create(100);
    processor.subscribe(subscriber);

    Runnable r1 = () -> processor.onError(failure);
    Runnable r2 = () -> processor.onError(failure);

    new Thread(r1).start();
    new Thread(r2).start();

    subscriber
            .await()
            .assertSubscribed()
            .assertHasFailedWith(Exception.class, "boom");
}
 
Example #11
Source File: SerializedProcessorTest.java    From smallrye-mutiny with Apache License 2.0 6 votes vote down vote up
@Test(invocationCount = 20)
public void verifyOnSubscribeOnCompleteThreadSafety() {
    final Processor<Integer, Integer> processor = UnicastProcessor.<Integer> create().serialized();
    MultiAssertSubscriber<Integer> subscriber = MultiAssertSubscriber.create(100);
    processor.subscribe(subscriber);

    Runnable r1 = () -> {
        processor.onNext(1);
        processor.onComplete();
    };
    Runnable r2 = () -> processor.onSubscribe(new Subscriptions.EmptySubscription());

    new Thread(r1).start();
    new Thread(r2).start();

    subscriber.await();
    subscriber
            .assertSubscribed()
            .assertCompletedSuccessfully();

    if (subscriber.items().size() != 0) {
        assertThat(subscriber.items()).containsExactly(1);
    }
}
 
Example #12
Source File: ProcessorMediator.java    From smallrye-reactive-messaging with Apache License 2.0 5 votes vote down vote up
private void processMethodReturningAProcessorOfMessages() {
    Processor<Message<?>, Message<?>> result = Objects.requireNonNull(invoke(),
            msg.methodReturnedNull(configuration.methodAsString()));

    this.processor = ReactiveStreams.<Message<?>> builder()
            .flatMapCompletionStage(msg -> {
                if (configuration.getAcknowledgment() == Acknowledgment.Strategy.PRE_PROCESSING) {
                    return msg.ack().thenApply((x -> msg));
                } else {
                    return CompletableFuture.completedFuture(msg);
                }
            })
            .via(result)
            .buildRs();
}
 
Example #13
Source File: PublisherWindow.java    From reactive-streams-commons with Apache License 2.0 5 votes vote down vote up
@Override
public void onError(Throwable t) {
    if (done) {
        UnsignalledExceptions.onErrorDropped(t);
        return;
    }
    Processor<T, T> w = window;
    if (w != null) {
        window = null;
        w.onError(t);
    }
    
    actual.onError(t);
}
 
Example #14
Source File: StatsdMeterRegistryTest.java    From micrometer with Apache License 2.0 5 votes vote down vote up
private Consumer<String> toSink(Processor<String, String> lines, int numLines) {
    AtomicInteger latch = new AtomicInteger(numLines);
    return l -> {
        lines.onNext(l);
        if (latch.decrementAndGet() == 0) {
            lines.onComplete();
        }
    };
}
 
Example #15
Source File: BeanWithMessageProcessors.java    From smallrye-reactive-messaging with Apache License 2.0 5 votes vote down vote up
@Incoming(PRE_ACKNOWLEDGMENT)
@Acknowledgment(Acknowledgment.Strategy.PRE_PROCESSING)
@Outgoing("sink-" + PRE_ACKNOWLEDGMENT)
public Processor<Message<String>, Message<String>> processorWithPreAck() {
    return ReactiveStreams.<Message<String>> builder()
            .flatMap(m -> ReactiveStreams.of(Message.of(m.getPayload()), Message.of(m.getPayload())))
            .peek(m -> processed(PRE_ACKNOWLEDGMENT, m))
            .buildRs();
}
 
Example #16
Source File: RxJava2ProcProxy.java    From RHub with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected <T> Flowable<T> filter(Processor processor, final Class<T> filterClass) {
    return ((Flowable) processor).filter(new Predicate() {
        @Override
        public boolean test(Object o) {
            return filterClass.isAssignableFrom(o.getClass());
        }
    });
}
 
Example #17
Source File: StatsdMeterRegistryTest.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@ParameterizedTest
@EnumSource(StatsdFlavor.class)
void gaugeLineProtocol(StatsdFlavor flavor) {
    final AtomicInteger n = new AtomicInteger(2);
    final StatsdConfig config = configWithFlavor(flavor);

    String line = null;
    switch (flavor) {
        case ETSY:
            line = "myGauge.myTag.val.statistic.value:2|g";
            break;
        case DATADOG:
            line = "my.gauge:2|g|#statistic:value,my.tag:val";
            break;
        case TELEGRAF:
            line = "my_gauge,statistic=value,my_tag=val:2|g";
            break;
        case SYSDIG:
            line = "my.gauge#statistic=value,my.tag=val:2|g";
            break;
        default:
            fail("Unexpected flavor");
    }

    StepVerifier
            .withVirtualTime(() -> {
                final Processor<String, String> lines = lineProcessor();
                registry = StatsdMeterRegistry.builder(config)
                        .clock(clock)
                        .lineSink(toSink(lines))
                        .build();

                registry.gauge("my.gauge", Tags.of("my.tag", "val"), n);
                return lines;
            })
            .then(() -> clock.add(config.step()))
            .thenAwait(config.step())
            .expectNext(line)
            .verifyComplete();
}
 
Example #18
Source File: DefaultWebSocketHttpResponse.java    From netty-reactive-streams with Apache License 2.0 5 votes vote down vote up
public DefaultWebSocketHttpResponse(HttpVersion version, HttpResponseStatus status, boolean validateHeaders,
                                    Processor<WebSocketFrame, WebSocketFrame> processor,
                                    WebSocketServerHandshakerFactory handshakerFactory) {
    super(version, status, validateHeaders);
    this.processor = processor;
    this.handshakerFactory = handshakerFactory;
}
 
Example #19
Source File: MultiWindowOp.java    From smallrye-mutiny with Apache License 2.0 5 votes vote down vote up
@Override
public void onFailure(Throwable failure) {
    Subscription subscription = upstream.getAndSet(CANCELLED);
    if (subscription != CANCELLED) {
        Processor<T, T> proc = processor;
        if (proc != null) {
            processor = null;
            proc.onError(failure);
        }
        downstream.onFailure(failure);
    }
}
 
Example #20
Source File: ProcessorStageFactory.java    From smallrye-reactive-streams-operators with Apache License 2.0 5 votes vote down vote up
@Override
public <I, O> ProcessingStage<I, O> create(Engine engine, Stage.ProcessorStage stage) {
    Processor<I, O> processor = Casts.cast(Objects.requireNonNull(
            Objects.requireNonNull(stage).getRsProcessor()));

    return source -> Flowable.defer(() -> {
        Flowable<O> flowable = Flowable.fromPublisher(processor);
        source.safeSubscribe(processor);
        return flowable;
    });
}
 
Example #21
Source File: AkkaProcProxy.java    From RHub with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected Publisher hide(Processor processor) {
    return (Publisher) Source
            .fromPublisher(processor)
            .runWith(Sink.asPublisher(AsPublisher.WITH_FANOUT), mat);
}
 
Example #22
Source File: StatsdMeterRegistryTest.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@ParameterizedTest
@EnumSource(StatsdFlavor.class)
void counterLineProtocol(StatsdFlavor flavor) {
    String line = null;
    switch (flavor) {
        case ETSY:
            line = "myCounter.myTag.val.statistic.count:2|c";
            break;
        case DATADOG:
            line = "my.counter:2|c|#statistic:count,my.tag:val";
            break;
        case TELEGRAF:
            line = "my_counter,statistic=count,my_tag=val:2|c";
            break;
        case SYSDIG:
            line = "my.counter#statistic=count,my.tag=val:2|c";
            break;
        default:
            fail("Unexpected flavor");
    }

    final Processor<String, String> lines = lineProcessor();
    registry = StatsdMeterRegistry.builder(configWithFlavor(flavor))
            .clock(clock)
            .lineSink(toSink(lines))
            .build();

    StepVerifier.create(lines)
            .then(() -> registry.counter("my.counter", "my.tag", "val").increment(2.1))
            .expectNext(line)
            .verifyComplete();
}
 
Example #23
Source File: BeanWithMessageProcessors.java    From smallrye-reactive-messaging with Apache License 2.0 5 votes vote down vote up
@Incoming(DEFAULT_ACKNOWLEDGMENT)
@Outgoing("sink-" + DEFAULT_ACKNOWLEDGMENT)
public Processor<Message<String>, Message<String>> processorWithAutoAck() {
    return ReactiveStreams.<Message<String>> builder()
            .flatMap(m -> ReactiveStreams.of(Message.of(m.getPayload()), Message.of(m.getPayload())).onComplete(
                    m::ack))
            .peek(m -> processed(DEFAULT_ACKNOWLEDGMENT, m))
            .buildRs();
}
 
Example #24
Source File: BeanWithMessageProcessors.java    From smallrye-reactive-messaging with Apache License 2.0 5 votes vote down vote up
@Incoming(MANUAL_ACKNOWLEDGMENT)
@Acknowledgment(Acknowledgment.Strategy.MANUAL)
@Outgoing("sink-" + MANUAL_ACKNOWLEDGMENT)
public Processor<Message<String>, Message<String>> processorWithAck() {
    return ReactiveStreams.<Message<String>> builder()
            .flatMapCompletionStage(m -> m.ack().thenApply(x -> m))
            .flatMap(m -> ReactiveStreams.of(Message.of(m.getPayload()), Message.of(m.getPayload())))
            .peek(m -> processed(MANUAL_ACKNOWLEDGMENT, m))
            .buildRs();
}
 
Example #25
Source File: AkkaStreamsUtil.java    From netty-reactive-streams with Apache License 2.0 5 votes vote down vote up
public static <In, Out> Processor<In, Out> flowToProcessor(Flow<In, Out, ?> flow, Materializer materializer) {
    Pair<Subscriber<In>, Publisher<Out>> pair =
            Source.<In>asSubscriber()
                    .via(flow)
                    .toMat(Sink.<Out>asPublisher(AsPublisher.WITH_FANOUT), Keep.<Subscriber<In>, Publisher<Out>>both())
                    .run(materializer);

    return new DelegateProcessor<>(pair.first(), pair.second());
}
 
Example #26
Source File: PublisherWindow.java    From reactive-streams-commons with Apache License 2.0 5 votes vote down vote up
@Override
public void onComplete() {
    if (done) {
        return;
    }

    for (Processor<T, T> w : windows) {
        w.onComplete();
    }
    windows.clear();
    
    done = true;
    drain();
}
 
Example #27
Source File: FluxWindow.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Override
public void onError(Throwable t) {
	if (done) {
		Operators.onErrorDropped(t, actual.currentContext());
		return;
	}
	done = true;
	Processor<T, T> w = window;
	if (w != null) {
		window = null;
		w.onError(t);
	}

	actual.onError(t);
}
 
Example #28
Source File: AkkaStreamsUtil.java    From netty-reactive-streams with Apache License 2.0 5 votes vote down vote up
public static <In, Out> Flow<In, Out, ?> processorToFlow(final Processor<In, Out> processor) {
    return Flow.fromProcessor(new Creator<Processor<In, Out>>() {
        @Override
        public Processor<In, Out> create() throws Exception {
            return processor;
        }
    });
}
 
Example #29
Source File: AkkaProcProxy.java    From RHub with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected <T> Publisher<T> filter(Processor processor, final Class<T> filterClass) {
    Source src = Source.fromPublisher(processor)
            .filter(o -> filterClass.isAssignableFrom(o.getClass()));
    return (Publisher<T>) src.runWith(Sink.asPublisher(AsPublisher.WITH_FANOUT), mat);
}
 
Example #30
Source File: SubjectPerf.java    From akarnokd-misc with Apache License 2.0 5 votes vote down vote up
final void run(Processor<Integer, Integer> subject, Blackhole bh) {
    subject.subscribe(new PerfConsumer(bh));
    int e = count;
    for (int i = 0; i < e; i++) {
        subject.onNext(1);
    }
    subject.onComplete();
    bh.consume(subject);
}