Java Code Examples for akka.japi.Pair#first()

The following examples show how to use akka.japi.Pair#first() . 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: AbstractBackgroundStreamingActorWithConfigWithStatusReport.java    From ditto with Eclipse Public License 2.0 6 votes vote down vote up
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 2
Source File: ResumeSourceTest.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
@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();
}
 
Example 3
Source File: BackgroundSyncStream.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
private Source<Metadata, NotUsed> filterForInconsistency(final Pair<Metadata, Metadata> pair) {
    final Metadata persisted = pair.first();
    final Metadata indexed = pair.second();
    final int comparison = compareMetadata(persisted, indexed);
    final Instant toleranceCutOff = Instant.now().minus(toleranceWindow);
    if (comparison < 0) {
        // persisted thing is not in search index; trigger update if the snapshot is not too recent
        return isInsideToleranceWindow(persisted, toleranceCutOff)
                ? Source.empty()
                : Source.single(persisted).log("PersistedAndNotIndexed");
    } else if (comparison > 0) {
        // indexed thing is not persisted; trigger update if the index entry is not too recent
        return isInsideToleranceWindow(indexed, toleranceCutOff)
                ? Source.empty()
                : Source.single(indexed).log("NotPersistedAndIndexed");
    } else {
        // IDs match
        if (indexed.getThingId().isDummy()) {
            // sanity check: entry should not be dummy
            return Source.failed(new IllegalStateException("Unexpected double-dummy entry: " + pair));
        } else if (isInsideToleranceWindow(indexed, toleranceCutOff)) {
            // ignore entries within tolerance window
            return Source.empty();
        } else {
            return emitUnlessConsistent(persisted, indexed);
        }
    }
}
 
Example 4
Source File: MessageMappingProcessorActor.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
private CompletionStage<Collection<OutboundSignalWithId>> enrichAndFilterSignal(
        final Pair<OutboundSignalWithId, FilteredTopic> outboundSignalWithExtraFields) {

    final OutboundSignalWithId outboundSignal = outboundSignalWithExtraFields.first();
    final FilteredTopic filteredTopic = outboundSignalWithExtraFields.second();
    final Optional<JsonFieldSelector> extraFieldsOptional =
            Optional.ofNullable(filteredTopic).flatMap(FilteredTopic::getExtraFields);
    if (extraFieldsOptional.isEmpty()) {
        return CompletableFuture.completedFuture(Collections.singletonList(outboundSignal));
    }
    final JsonFieldSelector extraFields = extraFieldsOptional.get();
    final Target target = outboundSignal.getTargets().get(0);

    final ThingId thingId = ThingId.of(outboundSignal.getEntityId());
    final DittoHeaders headers = DittoHeaders.newBuilder()
            .authorizationContext(target.getAuthorizationContext())
            // schema version is always the latest for connectivity signal enrichment.
            .schemaVersion(JsonSchemaVersion.LATEST)
            .build();
    final CompletionStage<JsonObject> extraFuture =
            signalEnrichmentFacade.retrievePartialThing(thingId, extraFields, headers, outboundSignal.getSource());

    return extraFuture.thenApply(outboundSignal::setExtra)
            .thenApply(outboundSignalWithExtra -> applyFilter(outboundSignalWithExtra, filteredTopic))
            .exceptionally(error -> {
                logger.withCorrelationId(outboundSignal.getSource())
                        .warning("Could not retrieve extra data due to: {} {}",
                                error.getClass().getSimpleName(), error.getMessage());
                // recover from all errors to keep message-mapping-stream running despite enrichment failures
                return Collections.singletonList(recoverFromEnrichmentError(outboundSignal, target, error));
            });
}
 
Example 5
Source File: KafkaPublisherActor.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
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 6
Source File: HttpPushFactoryTest.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
@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 7
Source File: AkkaHubProxy.java    From RHub with Apache License 2.0 5 votes vote down vote up
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 8
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 9
Source File: SearchSourceTest.java    From ditto with Eclipse Public License 2.0 4 votes vote down vote up
private SourceRef<Object> materializeSourceProbe() {
    final Pair<TestPublisher.Probe<Object>, CompletionStage<SourceRef<Object>>> materializedValues =
            TestSource.probe(actorSystem).toMat(StreamRefs.sourceRef(), Keep.both()).run(materializer);
    sourceProbe = materializedValues.first();
    return materializedValues.second().toCompletableFuture().join();
}
 
Example 10
Source File: ResumeSourceTest.java    From ditto with Eclipse Public License 2.0 4 votes vote down vote up
private void rematerializeSource() {
    final Source<Integer, TestPublisher.Probe<Integer>> source = TestSource.probe(system);
    final Pair<TestPublisher.Probe<Integer>, Source<Integer, NotUsed>> sourcePair = source.preMaterialize(mat);
    sourceProbe = sourcePair.first();
    testSource = sourcePair.second();
}
 
Example 11
Source File: HttpPublisherActor.java    From ditto with Eclipse Public License 2.0 4 votes vote down vote up
private void processResponse(final Pair<Try<HttpResponse>, HttpPushContext> responseWithMessage) {
    final Try<HttpResponse> tryResponse = responseWithMessage.first();
    final HttpPushContext context = responseWithMessage.second();
    final ExternalMessage message = context.getExternalMessage();
    final Uri requestUri = context.getRequestUri();
    if (tryResponse.isFailure()) {
        final Throwable error = tryResponse.toEither().left().get();
        final String errorDescription = MessageFormat.format("Failed to send HTTP request to <{0}>.",
                stripUserInfo(requestUri));
        log.debug("Failed to send message <{}> due to <{}>", message, error);
        responsePublishedMonitor.failure(message, errorDescription);
        escalate(error, errorDescription);
    } else {
        final HttpResponse response = tryResponse.toEither().right().get();
        log.debug("Sent message <{}>. Got response <{} {}>", message, response.status(), response.getHeaders());
        if (response.status().isSuccess()) {
            responsePublishedMonitor.success(message,
                    "HTTP call to <{0}> successfully responded with status <{1}>.",
                    stripUserInfo(requestUri), response.status());
            response.discardEntityBytes(materializer);
        } else {
            getResponseBody(response, materializer)
                    .thenAccept(body -> responsePublishedMonitor.failure(message,
                            "HTTP call to <{0}> responded with status <{1}> and body: {2}.",
                            stripUserInfo(requestUri),
                            response.status(), body)
                    )
                    .exceptionally(bodyReadError -> {
                        responsePublishedMonitor.failure(message,
                                "HTTP call to <{0}> responded with status <{1}>. Failed to read body within {2} ms",
                                stripUserInfo(requestUri), response.status(), READ_BODY_TIMEOUT_MS);
                        LogUtil.enhanceLogWithCorrelationId(log, message.getInternalHeaders());
                        log.info("Got <{}> when reading body of publish response to <{}>", bodyReadError,
                                message);
                        return null;
                    });
        }
    }
}
 
Example 12
Source File: HttpPushFactoryTest.java    From ditto with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void withHttpProxyConfig() throws Exception {
    // GIVEN: the connection's URI points to an unreachable host
    connection = connection.toBuilder()
            .uri("http://no.such.host.example:42/path/prefix/")
            .build();

    // GIVEN: the HTTP-push factory has the proxy configured to the test server binding
    final HttpPushFactory underTest = HttpPushFactory.of(connection, new HttpPushConfig() {
        @Override
        public int getMaxQueueSize() {
            return 0;
        }

        @Override
        public HttpProxyConfig getHttpProxyConfig() {
            return getEnabledProxyConfig(binding);
        }
    });
    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/"));

    // WHEN: request is made
    sourceQueue.offer(request);

    // THEN: CONNECT request is made to the Akka HTTP test server.
    // THEN: Akka HTTP server rejects CONNECT request, creating a failed response
    final Optional<Try<HttpResponse>> optionalTryResponse = sinkQueue.pull().toCompletableFuture().join();
    assertThat(optionalTryResponse).isNotEmpty();
    final Try<HttpResponse> tryResponse = optionalTryResponse.get();
    assertThat(tryResponse).isInstanceOf(Failure.class);
    assertThat(tryResponse.failed().get()).isInstanceOf(ProxyConnectionFailedException.class);
    assertThat(tryResponse.failed().get().getMessage())
            .contains("proxy rejected to open a connection to no.such.host.example:42 with status code: 400");
}
 
Example 13
Source File: HttpPushFactoryTest.java    From ditto with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void handleFailure() throws Exception {
    new TestKit(actorSystem) {{
        // GIVEN: An HTTP-push connection is established against localhost.
        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 request1 = underTest.newRequest(HttpPublishTarget.of("1"));
        final HttpRequest request2 = underTest.newRequest(HttpPublishTarget.of("2"));
        final HttpRequest request3 = underTest.newRequest(HttpPublishTarget.of("3"));
        final HttpResponse response1 = HttpResponse.create().withStatus(StatusCodes.IM_A_TEAPOT);
        final HttpResponse response3 = HttpResponse.create().withStatus(StatusCodes.BLOCKED_BY_PARENTAL_CONTROLS);

        // GIVEN: The connection is working.
        responseQueue.offer(CompletableFuture.completedFuture(response1));
        sourceQueue.offer(request1);
        assertThat(requestQueue.take().getUri()).isEqualTo(request1.getUri());
        final Try<HttpResponse> responseOrError1 = pullResponse(sinkQueue);
        assertThat(responseOrError1.isSuccess()).isTrue();
        assertThat(responseOrError1.get().status()).isEqualTo(response1.status());

        // WHEN: In-flight request is killed
        // THEN: Akka HTTP responds with status 500
        responseQueue.offer(new CompletableFuture<>());
        sourceQueue.offer(request2);
        assertThat(requestQueue.take().getUri()).isEqualTo(request2.getUri());
        shutdownAllServerStreams();
        final Try<HttpResponse> responseOrError2 = pullResponse(sinkQueue);
        assertThat(responseOrError2.isSuccess()).isTrue();
        assertThat(responseOrError2.get().status()).isEqualTo(StatusCodes.INTERNAL_SERVER_ERROR);

        // WHEN: HTTP server becomes available again.
        // THEN: A new request resumes and the previously failed request is discarded.
        responseQueue.offer(CompletableFuture.completedFuture(response3));
        sourceQueue.offer(request3);
        assertThat(requestQueue.take().getUri()).isEqualTo(request3.getUri());
        final Try<HttpResponse> responseOrError3 = pullResponse(sinkQueue);
        assertThat(responseOrError3.isSuccess()).isTrue();
        assertThat(responseOrError3.get().status()).isEqualTo(response3.status());
    }};
}