akka.japi.Pair Java Examples
The following examples show how to use
akka.japi.Pair.
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: MergeSortedAsPairTest.java From ditto with Eclipse Public License 2.0 | 6 votes |
@Test public void testMergeSortedResult() { final Source<Integer, NotUsed> source1 = Source.from(List.of(1, 3, 5, 7, 9)); final Source<Integer, NotUsed> source2 = Source.from(List.of(2, 3, 4, 5, 6)); final List<Pair<Integer, Integer>> mergeResult = runWithMergeSortedAsPair(source1, source2); assertThat(mergeResult) .containsExactlyElementsOf(List.of( Pair.create(1, 2), Pair.create(3, 2), Pair.create(3, 3), Pair.create(5, 4), Pair.create(5, 5), Pair.create(7, 6), Pair.create(7, Integer.MAX_VALUE), Pair.create(9, Integer.MAX_VALUE) )); }
Example #2
Source File: MongoReadJournal.java From ditto with Eclipse Public License 2.0 | 6 votes |
private <T> Source<List<T>, NotUsed> unfoldBatchedSource( final String lowerBound, final ActorMaterializer mat, final Function<T, String> seedCreator, final Function<String, Source<T, ?>> sourceCreator) { return Source.unfoldAsync("", start -> { final String actualStart = lowerBound.compareTo(start) >= 0 ? lowerBound : start; return sourceCreator.apply(actualStart) .runWith(Sink.seq(), mat) .thenApply(list -> { if (list.isEmpty()) { return Optional.empty(); } else { return Optional.of(Pair.create(seedCreator.apply(list.get(list.size() - 1)), list)); } }); }) .withAttributes(Attributes.inputBuffer(1, 1)); }
Example #3
Source File: HttpPublisherActor.java From ditto with Eclipse Public License 2.0 | 6 votes |
@SuppressWarnings("unused") private HttpPublisherActor(final Connection connection, final HttpPushFactory factory) { super(connection); this.factory = factory; final ActorSystem system = getContext().getSystem(); final ConnectionConfig connectionConfig = DittoConnectivityConfig.of(DefaultScopedConfig.dittoScoped(system.settings().config())) .getConnectionConfig(); config = connectionConfig.getHttpPushConfig(); materializer = ActorMaterializer.create(getContext()); sourceQueue = Source.<Pair<HttpRequest, HttpPushContext>>queue(config.getMaxQueueSize(), OverflowStrategy.dropNew()) .viaMat(factory.createFlow(system, log), Keep.left()) .toMat(Sink.foreach(this::processResponse), Keep.left()) .run(materializer); }
Example #4
Source File: MongoSearchUpdaterFlow.java From ditto with Eclipse Public License 2.0 | 6 votes |
@SuppressWarnings("unchecked") // java 8 can't handle graph DSL types private static <A, B, C, D> Graph<FlowShape<A, C>, NotUsed> assembleFlows( final Flow<A, B, NotUsed> stage1Flow, final Flow<B, C, NotUsed> stage2Flow, final Flow<B, D, NotUsed> sideChannelFlow, final Flow<Pair<C, D>, C, NotUsed> resultProcessorFlow) { return GraphDSL.create(builder -> { final FlowShape<A, B> stage1 = builder.add(stage1Flow); final FlowShape<B, C> stage2 = builder.add(stage2Flow); final FlowShape<B, D> sideChannel = builder.add(sideChannelFlow); final FlowShape<Pair<C, D>, C> resultProcessor = builder.add(resultProcessorFlow); final UniformFanOutShape<B, B> broadcast = builder.add(Broadcast.create(2)); final FanInShape2<C, D, Pair<C, D>> zip = builder.add(Zip.create()); builder.from(stage1.out()).toInlet(broadcast.in()); builder.from(broadcast.out(0)).toInlet(stage2.in()); builder.from(broadcast.out(1)).toInlet(sideChannel.in()); builder.from(stage2.out()).toInlet(zip.in0()); builder.from(sideChannel.out()).toInlet(zip.in1()); builder.from(zip.out()).toInlet(resultProcessor.in()); return FlowShape.of(stage1.in(), resultProcessor.out()); }); }
Example #5
Source File: SearchSource.java From ditto with Eclipse Public License 2.0 | 6 votes |
/** * Start a robust source of search results paired with their IDs. * * @param configurer consumer to configure the resume source. * @return source of pair of ID-result pairs where the ID can be used for resumption. */ public Source<Pair<String, JsonObject>, NotUsed> startAsPair(final Consumer<ResumeSourceBuilder<?, ?>> configurer) { // start with a ResumeSourceBuilder with useful defaults. final ResumeSourceBuilder<String, Pair<String, JsonObject>> builder = ResumeSource.<String, Pair<String, JsonObject>>newBuilder() .minBackoff(Duration.ofSeconds(1L)) .maxBackoff(Duration.ofSeconds(20L)) .recovery(Duration.ofSeconds(60L)) .maxRestarts(25) .initialSeed(lastThingId) .resume(this::resume) .nextSeed(this::nextSeed) .mapError(this::mapError); configurer.accept(builder); return builder.build(); }
Example #6
Source File: SearchSource.java From ditto with Eclipse Public License 2.0 | 6 votes |
private Source<Pair<String, JsonObject>, NotUsed> retrieveThingForElement(final String thingId) { if (thingIdOnly) { final JsonObject idOnlyThingJson = JsonObject.newBuilder().set(Thing.JsonFields.ID, thingId).build(); return Source.single(Pair.create(thingId, idOnlyThingJson)); } else { return retrieveThing(thingId, fields) .map(thingJson -> Pair.create(thingId, thingJson)) .recoverWithRetries(1, new PFBuilder<Throwable, Graph<SourceShape<Pair<String, JsonObject>>, NotUsed>>() .match(ThingNotAccessibleException.class, thingNotAccessible -> { // out-of-sync thing detected final ThingsOutOfSync thingsOutOfSync = ThingsOutOfSync.of(Collections.singletonList(ThingId.of(thingId)), getDittoHeaders()); pubSubMediator.tell( DistPubSubAccess.publishViaGroup(ThingsOutOfSync.TYPE, thingsOutOfSync), ActorRef.noSender()); return Source.empty(); }) .build() ); } }
Example #7
Source File: MergeSortedAsPairTest.java From ditto with Eclipse Public License 2.0 | 6 votes |
@SuppressWarnings("unchecked") // due to GraphDSL usage private static <M1, M2> Source<Pair<Integer, Integer>, Pair<M1, M2>> mergeSortedAsPairWithMat( final Source<Integer, M1> source1, final Source<Integer, M2> source2) { final Graph<SourceShape<Pair<Integer, Integer>>, Pair<M1, M2>> combinedSource = GraphDSL.create(source1, source2, Pair::create, (builder, s1, s2) -> { final FanInShape2<Integer, Integer, Pair<Integer, Integer>> mergeSorted = builder.add(MergeSortedAsPair.getInstance(Integer.MAX_VALUE, Integer::compare)); builder.from(s1).toInlet(mergeSorted.in0()); builder.from(s2).toInlet(mergeSorted.in1()); return SourceShape.of(mergeSorted.out()); }); return Source.fromGraph(combinedSource); }
Example #8
Source File: AbstractBackgroundStreamingActorWithConfigWithStatusReport.java From ditto with Eclipse Public License 2.0 | 6 votes |
private void restartStream() { shutdownKillSwitch(); final Pair<UniqueKillSwitch, CompletionStage<Done>> materializedValues = getSource().viaMat(KillSwitches.single(), Keep.right()) .toMat(Sink.ignore(), Keep.both()) .run(materializer); killSwitch = materializedValues.first(); materializedValues.second() .<Void>handle((result, error) -> { final String description = String.format("Stream terminated. Result=<%s> Error=<%s>", result, error); log.info(description); getSelf().tell(new StreamTerminated(description), getSelf()); return null; }); }
Example #9
Source File: ResumeSource.java From ditto with Eclipse Public License 2.0 | 6 votes |
@Override public Iterable<Pair<E, Duration>> apply(final E element) { final Instant thisError = Instant.now(); final Duration thisBackoff; if (lastError.plus(recovery).isBefore(thisError)) { // recovery period passed with no error; reset backoff to minimum. thisBackoff = minBackoff; } else { final Duration nextBackoff = lastBackoff.multipliedBy(2L); thisBackoff = maxBackoff.minus(nextBackoff).isNegative() ? maxBackoff : nextBackoff; } lastError = thisError; lastBackoff = thisBackoff; return Collections.singletonList(Pair.create(element, thisBackoff)); }
Example #10
Source File: MergeSortedAsPair.java From ditto with Eclipse Public License 2.0 | 6 votes |
private void dispatch(final T element1, final T element2) { final int comparison = comparator.compare(element1, element2); final Pair<T, T> toEmit = Pair.create(element1, element2); if (comparison < 0) { // store element2 and pull the next one from in1 lastElement1 = null; lastElement2 = element2; emit(out, toEmit, this::advanceIn1AfterGrabbingIn2); } else if (comparison > 0) { // store element1 and pull the next one from in2 lastElement1 = element1; lastElement2 = null; emit(out, toEmit, this::advanceIn2AfterGrabbingIn1); } else { // neither elements will be emitted again - discard and pull the next pair from the inlets // // While technically unnecessary, the null assignments maintain the invariant that // non-null values of lastElement1 and lastElement2 will be emitted at least one more time. // This is handy in the debugger. lastElement1 = null; lastElement2 = null; emit(out, toEmit, this::advanceBothInlets); } }
Example #11
Source File: MergeSortedAsPairTest.java From ditto with Eclipse Public License 2.0 | 6 votes |
@Test public void mergeEmptySources() { // source1 is empty final List<Integer> integers = List.of(1, 2, 3, 4, 5); assertThat(runWithMergeSortedAsPair(Source.empty(), Source.from(integers))) .containsExactlyElementsOf( integers.stream().map(i -> Pair.create(Integer.MAX_VALUE, i)).collect(Collectors.toList())); // source2 is empty assertThat(runWithMergeSortedAsPair(Source.from(integers), Source.empty())) .containsExactlyElementsOf( integers.stream().map(i -> Pair.create(i, Integer.MAX_VALUE)).collect(Collectors.toList())); // both sources empty assertThat(runWithMergeSortedAsPair(Source.empty(), Source.empty())).isEmpty(); }
Example #12
Source File: HttpPublisherActor.java From ditto with Eclipse Public License 2.0 | 5 votes |
private HttpRequest createRequest(final HttpPublishTarget publishTarget, final ExternalMessage message) { final Pair<Iterable<HttpHeader>, ContentType> headersPair = getHttpHeadersPair(message); final HttpRequest requestWithoutEntity = factory.newRequest(publishTarget).addHeaders(headersPair.first()); final ContentType contentTypeHeader = headersPair.second(); if (contentTypeHeader != null) { final HttpEntity.Strict httpEntity = HttpEntities.create(contentTypeHeader.contentType(), getPayloadAsBytes(message)); return requestWithoutEntity.withEntity(httpEntity); } else if (message.isTextMessage()) { return requestWithoutEntity.withEntity(getTextPayload(message)); } else { return requestWithoutEntity.withEntity(getBytePayload(message)); } }
Example #13
Source File: MergeSortedAsPairTest.java From ditto with Eclipse Public License 2.0 | 5 votes |
@Test public void testIdenticalStartAndEnd() { materializeTestProbes(); List.of(1, 3, 4).forEach(source1Probe::sendNext); List.of(1, 2, 4).forEach(source2Probe::sendNext); source1Probe.sendComplete(); source2Probe.sendComplete(); sinkProbe.request(9); sinkProbe.expectNext(Pair.create(1, 1)); sinkProbe.expectNext(Pair.create(3, 2)); sinkProbe.expectNext(Pair.create(3, 4)); sinkProbe.expectNext(Pair.create(4, 4)); sinkProbe.expectComplete(); }
Example #14
Source File: BaseClientData.java From ditto with Eclipse Public License 2.0 | 5 votes |
private BaseClientData(final ConnectionId connectionId, final Connection connection, final ConnectivityStatus connectionStatus, final ConnectivityStatus desiredConnectionStatus, @Nullable final String connectionStatusDetails, final Instant inConnectionStatusSince, final List<Pair<ActorRef, DittoHeaders>> sessionSenders) { this.connectionId = connectionId; this.connection = connection; this.connectionStatus = connectionStatus; this.desiredConnectionStatus = desiredConnectionStatus; this.connectionStatusDetails = connectionStatusDetails; this.inConnectionStatusSince = inConnectionStatusSince; this.sessionSenders = Collections.unmodifiableList(new ArrayList<>(sessionSenders)); }
Example #15
Source File: BaseClientData.java From ditto with Eclipse Public License 2.0 | 5 votes |
/** * Adds the passed {@code origin} sender with the passed {@code dittoHeaders} to the managed {@code sessionSenders} * returning a new instance of BaseClientData. * * @param origin the sender to add * @param dittoHeaders the DittoHeaders to add for the passed sender * @return the new instance of BaseClientData */ BaseClientData addSessionSender(@Nullable final ActorRef origin, final DittoHeaders dittoHeaders) { if (origin != null) { final List<Pair<ActorRef, DittoHeaders>> newSessionSenders = new ArrayList<>(sessionSenders); newSessionSenders.add(Pair.create(origin, dittoHeaders)); return new BaseClientData(connectionId, connection, connectionStatus, desiredConnectionStatus, connectionStatusDetails, inConnectionStatusSince, newSessionSenders); } else { return this; } }
Example #16
Source File: MongoSearchUpdaterFlow.java From ditto with Eclipse Public License 2.0 | 5 votes |
private static <T> Flow<Pair<T, StartedTimer>, T, NotUsed> createStopTimerFlow() { return Flow.fromFunction(pair -> { try { pair.second().stop(); } catch (final IllegalStateException e) { // it is okay if the timer stopped already; simply return the result. } return pair.first(); }); }
Example #17
Source File: HttpPublisherActor.java From ditto with Eclipse Public License 2.0 | 5 votes |
@Override protected void publishMessage(@Nullable final Target target, final HttpPublishTarget publishTarget, final ExternalMessage message, final ConnectionMonitor publishedMonitor) { final HttpRequest request = createRequest(publishTarget, message); sourceQueue.offer(Pair.create(request, new HttpPushContext(message, request.getUri()))) .handle(handleQueueOfferResult(message)); }
Example #18
Source File: MessageMappingProcessorActor.java From ditto with Eclipse Public License 2.0 | 5 votes |
/** * Split the targets of an outbound signal into 2 parts: those without extra fields and those with. * * @param outboundSignal The outbound signal. * @return A pair of lists. The first list contains targets without matching extra fields. * The second list contains targets together with their extra fields matching the outbound signal. */ private static Pair<List<Target>, List<Pair<Target, FilteredTopic>>> splitTargetsByExtraFields( final OutboundSignal outboundSignal) { final Optional<StreamingType> streamingTypeOptional = StreamingType.fromSignal(outboundSignal.getSource()); if (streamingTypeOptional.isPresent()) { // Find targets with a matching topic with extra fields final StreamingType streamingType = streamingTypeOptional.get(); final List<Target> targetsWithoutExtraFields = new ArrayList<>(outboundSignal.getTargets().size()); final List<Pair<Target, FilteredTopic>> targetsWithExtraFields = new ArrayList<>(outboundSignal.getTargets().size()); for (final Target target : outboundSignal.getTargets()) { final Optional<FilteredTopic> matchingExtraFields = target.getTopics() .stream() .filter(filteredTopic -> filteredTopic.getExtraFields().isPresent() && streamingType == StreamingType.fromTopic(filteredTopic.getTopic().getPubSubTopic())) .findAny(); if (matchingExtraFields.isPresent()) { targetsWithExtraFields.add(Pair.create(target, matchingExtraFields.get())); } else { targetsWithoutExtraFields.add(target); } } return Pair.create(targetsWithoutExtraFields, targetsWithExtraFields); } else { // The outbound signal has no streaming type: Do not attach extra fields. return Pair.create(outboundSignal.getTargets(), Collections.emptyList()); } }
Example #19
Source File: KafkaPublisherActor.java From ditto with Eclipse Public License 2.0 | 5 votes |
private ActorRef createInternalKafkaProducer(final KafkaConnectionFactory factory, final BiFunction<Done, Throwable, Done> completionOrFailureHandler) { final Pair<ActorRef, CompletionStage<Done>> materializedFlowedValues = Source.<ProducerMessage.Envelope<String, String, PassThrough>>actorRef(100, OverflowStrategy.dropHead()) .via(factory.newFlow()) .toMat(KafkaPublisherActor.publishSuccessSink(), Keep.both()) .run(ActorMaterializer.create(getContext())); materializedFlowedValues.second().handleAsync(completionOrFailureHandler); return materializedFlowedValues.first(); }
Example #20
Source File: MergeSortedAsPairTest.java From ditto with Eclipse Public License 2.0 | 5 votes |
private void materializeTestProbes() { final Pair<Pair<TestPublisher.Probe<Integer>, TestPublisher.Probe<Integer>>, TestSubscriber.Probe<Pair<Integer, Integer>>> probes = mergeSortedAsPairWithMat(TestSource.probe(system), TestSource.probe(system)) .toMat(TestSink.probe(system), Keep.both()) .run(mat); source1Probe = probes.first().first(); source2Probe = probes.first().second(); sinkProbe = probes.second(); }
Example #21
Source File: HttpPushFactoryTest.java From ditto with Eclipse Public License 2.0 | 5 votes |
@Test public void basicAuth() throws Exception { // GIVEN: the connection has plain credentials in the URI connection = connection.toBuilder() .uri("http://username:[email protected]:" + binding.localAddress().getPort() + "/path/prefix/") .build(); final HttpPushFactory underTest = HttpPushFactory.of(connection, connectionConfig.getHttpPushConfig()); final Pair<SourceQueueWithComplete<HttpRequest>, SinkQueueWithCancel<Try<HttpResponse>>> pair = newSourceSinkQueues(underTest); final SourceQueueWithComplete<HttpRequest> sourceQueue = pair.first(); final SinkQueueWithCancel<Try<HttpResponse>> sinkQueue = pair.second(); final HttpRequest request = underTest.newRequest(HttpPublishTarget.of("PUT:/path/appendage/")); final HttpResponse response = HttpResponse.create().withStatus(StatusCodes.OK); // WHEN: request-response cycle is carried out responseQueue.offer(CompletableFuture.completedFuture(response)); sourceQueue.offer(request); final HttpRequest actualRequest = requestQueue.take(); // THEN: actual received request has a basic auth header assertThat(actualRequest.getHeader(Authorization.class)) .contains(Authorization.basic("username", "password")); }
Example #22
Source File: MergeSortedAsPairTest.java From ditto with Eclipse Public License 2.0 | 5 votes |
@Test public void testSourcesDisjointInTimeWithTheLaterSourceExhaustedFirst() { materializeTestProbes(); source1Probe.sendNext(1).sendNext(3).sendNext(5).sendNext(7).sendComplete(); sinkProbe.request(6); sinkProbe.expectNoMsg(FiniteDuration.create(250L, TimeUnit.MILLISECONDS)); source2Probe.sendNext(2).sendNext(4).sendComplete(); sinkProbe.expectNext(Pair.create(1, 2)) .expectNext(Pair.create(3, 2)) .expectNext(Pair.create(3, 4)) .expectNext(Pair.create(5, 4)) .expectNext(Pair.create(5, Integer.MAX_VALUE)) .expectNext(Pair.create(7, Integer.MAX_VALUE)) .expectComplete(); }
Example #23
Source File: MergeSortedAsPairTest.java From ditto with Eclipse Public License 2.0 | 5 votes |
@Test public void testSourcesDisjointInTimeWithTheEarlierSourceExhaustedFirst() { materializeTestProbes(); source1Probe.sendNext(1).sendNext(3).sendComplete(); sinkProbe.request(6); sinkProbe.expectNoMsg(FiniteDuration.create(250L, TimeUnit.MILLISECONDS)); source2Probe.sendNext(2).sendNext(4).sendNext(6).sendNext(8).sendComplete(); sinkProbe.expectNext(Pair.create(1, 2)) .expectNext(Pair.create(3, 2)) .expectNext(Pair.create(3, 4)) .expectNext(Pair.create(Integer.MAX_VALUE, 4)) .expectNext(Pair.create(Integer.MAX_VALUE, 6)) .expectNext(Pair.create(Integer.MAX_VALUE, 8)) .expectComplete(); }
Example #24
Source File: MergeSortedAsPairTest.java From ditto with Eclipse Public License 2.0 | 5 votes |
@Test public void testDownstreamCancellation() { materializeTestProbes(); source1Probe.sendNext(1).sendNext(3); source2Probe.sendNext(2).sendNext(4).sendNext(6); sinkProbe.request(3); sinkProbe.expectNext(Pair.create(1, 2)) .expectNext(Pair.create(3, 2)) .expectNext(Pair.create(3, 4)); sinkProbe.cancel(); source1Probe.expectCancellation(); source2Probe.expectCancellation(); }
Example #25
Source File: HttpPushFactoryTest.java From ditto with Eclipse Public License 2.0 | 5 votes |
private Pair<SourceQueueWithComplete<HttpRequest>, SinkQueueWithCancel<Try<HttpResponse>>> newSourceSinkQueues( final HttpPushFactory underTest) { return Source.<HttpRequest>queue(10, OverflowStrategy.dropNew()) .map(r -> Pair.create(r, null)) .viaMat(underTest.createFlow(actorSystem, actorSystem.log()), Keep.left()) .map(Pair::first) .toMat(Sink.queue(), Keep.both()) .run(mat); }
Example #26
Source File: MergeSortedAsPairTest.java From ditto with Eclipse Public License 2.0 | 5 votes |
@Test public void testUpstreamFailure() { materializeTestProbes(); source1Probe.sendNext(1).sendNext(3); source2Probe.sendNext(2).sendNext(4).sendNext(6); sinkProbe.request(3); sinkProbe.expectNext(Pair.create(1, 2)) .expectNext(Pair.create(3, 2)) .expectNext(Pair.create(3, 4)); final Throwable error = new IllegalStateException("source1 failure"); source1Probe.sendError(error); source2Probe.expectCancellation(); sinkProbe.expectError(error); }
Example #27
Source File: AkkaHubProxy.java From RHub with Apache License 2.0 | 5 votes |
public AkkaHubProxy(ActorMaterializer mat) { this.mat = mat; // Obtain a Sink and Source which will publish and receive from the "bus" respectively. Pair<Sink<Object, NotUsed>, Source<Object, NotUsed>> sinkAndSource = MergeHub.of(Object.class, 16) .toMat(BroadcastHub.of(Object.class, 256), Keep.both()) .run(mat); Sink<Object, NotUsed> sink = sinkAndSource.first(); source = sinkAndSource.second().takeWhile((Predicate<Object>) o -> o != Done.getInstance()); //source.runWith(Sink.ignore(), mat); busFlow = Flow.fromSinkAndSource(sink, source) .joinMat(KillSwitches.singleBidi(), Keep.right()); }
Example #28
Source File: MergeSortedAsPairTest.java From ditto with Eclipse Public License 2.0 | 5 votes |
@Test public void forIdenticalSources() { final Source<Integer, NotUsed> source1 = Source.from(List.of(1, 3, 5, 7, 9)); final List<Pair<Integer, Integer>> mergeResult = runWithMergeSortedAsPair(source1, source1); assertThat(mergeResult) .containsExactlyElementsOf(List.of( Pair.create(1, 1), Pair.create(3, 3), Pair.create(5, 5), Pair.create(7, 7), Pair.create(9, 9) )); }
Example #29
Source File: AkkaStreamsUtil.java From netty-reactive-streams with Apache License 2.0 | 5 votes |
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 #30
Source File: ResumeSourceTest.java From ditto with Eclipse Public License 2.0 | 5 votes |
@Before public void init() { system = ActorSystem.create(); mat = ActorMaterializer.create(system); rematerializeSource(); // materialize sink once - it never fails. final Sink<Integer, TestSubscriber.Probe<Integer>> sink = TestSink.probe(system); final Pair<TestSubscriber.Probe<Integer>, Sink<Integer, NotUsed>> sinkPair = sink.preMaterialize(mat); sinkProbe = sinkPair.first(); testSink = sinkPair.second(); }