/** * Akka Alpakka example * * @param args * @throws Exception */ public static void main(String[] args) throws Exception { LOGGER.info("Init"); // close Slick session onTermination of Actor system system.registerOnTermination(() -> { LOGGER.info("termination of actor system"); SESSION.close(); }); // Load first 49 users inserUsersGraph.run(materializer); //Start Akka HTTP server LOGGER.info("Start Server"); Server server = new Server(materializer, usersStream); server.startServer(CONFIG.getString("server.host"), CONFIG.getInt("server.port"), system); system.terminate(); }
@Override public Removable addUpstream(Source<Object, NotUsed> publisher) { UniqueKillSwitch killSwitch = publisher.viaMat(busFlow, Keep.right()) .to(Sink.ignore()) .run(mat); subscriptions.put(publisher, killSwitch); return () -> AkkaHubProxy.this.removeUpstream(publisher); }
private GraphActor(final Function<ActorContext, Graph<SinkShape<WithSender>, NotUsed>> graphCreator) { materializer = ActorMaterializer.create(getContext()); messageHandler = MergeHub.of(WithSender.class).to(graphCreator.apply(getContext())).run(materializer); }
private GraphActor(final BiFunction<ActorContext, DiagnosticLoggingAdapter, Graph<SinkShape<WithSender>, NotUsed>> graphCreator) { materializer = ActorMaterializer.create(getContext()); messageHandler = MergeHub.of(WithSender.class).to(graphCreator.apply(getContext(), log)).run(materializer); }
private GraphActor(final BiFunction<ActorContext, DiagnosticLoggingAdapter, Graph<SinkShape<WithSender>, NotUsed>> graphCreator) { materializer = ActorMaterializer.create(getContext()); messageHandler = MergeHub.of(WithSender.class).to(graphCreator.apply(getContext(), log)).run(materializer); }
private GraphActor(final Function<ActorContext, Graph<SinkShape<WithSender>, NotUsed>> graphCreator) { materializer = ActorMaterializer.create(getContext()); messageHandler = MergeHub.of(WithSender.class).to(graphCreator.apply(getContext())).run(materializer); }
@Override public void emit(Object event) { Source.single(event).viaMat(busFlow, Keep.right()) .to(Sink.ignore()) .run(mat); }
private Route handleDevOpsPerRequest(final RequestContext ctx, final Source<ByteString, ?> payloadSource, final Function<String, DevOpsCommand> requestJsonToCommandFunction) { final CompletableFuture<HttpResponse> httpResponseFuture = new CompletableFuture<>(); payloadSource .fold(ByteString.empty(), ByteString::concat) .map(ByteString::utf8String) .map(requestJsonToCommandFunction) .to(Sink.actorRef(createHttpPerRequestActor(ctx, httpResponseFuture), HttpRequestActor.COMPLETE_MESSAGE)) .run(materializer); return completeWithFuture(httpResponseFuture); }
private Route handleMessage(final RequestContext ctx, final Source<ByteString, Object> payloadSource, final Function<ByteBuffer, MessageCommand<?, ?>> requestPayloadToCommandFunction) { final CompletableFuture<HttpResponse> httpResponseFuture = new CompletableFuture<>(); payloadSource.fold(ByteString.empty(), ByteString::concat) .map(ByteString::toArray) .map(ByteBuffer::wrap) .map(requestPayloadToCommandFunction) .to(Sink.actorRef(createHttpPerRequestActor(ctx, httpResponseFuture), HttpRequestActor.COMPLETE_MESSAGE)) .run(materializer); return completeWithFuture(preprocessResponse(httpResponseFuture)); }
private MqttPublisherActor(final MqttConnectionFactory factory, final ActorRef mqttClientActor, final boolean dryRun) { this.mqttClientActor = mqttClientActor; this.dryRun = dryRun; final Sink<MqttMessage, CompletionStage<Done>> mqttSink = factory.newSink(); final Pair<ActorRef, CompletionStage<Done>> materializedValues = Source.<MqttMessage>actorRef(100, OverflowStrategy.dropHead()) .map(this::countPublishedMqttMessage) .toMat(mqttSink, Keep.both()) .run(ActorMaterializer.create(getContext())); materializedValues.second().handle(this::reportReadiness); sourceActor = materializedValues.first(); addressMetric = ConnectivityModelFactory.newAddressMetric(ConnectionStatus.OPEN, "Started at " + Instant.now(), 0, null); }
private MqttPublisherActor(final MqttConnectionFactory factory, final ActorRef mqttClientActor, final boolean dryRun) { this.mqttClientActor = mqttClientActor; this.dryRun = dryRun; final Sink<MqttMessage, CompletionStage<Done>> mqttSink = factory.newSink(); final Pair<ActorRef, CompletionStage<Done>> materializedValues = Source.<MqttMessage>actorRef(100, OverflowStrategy.dropHead()) .map(this::countPublishedMqttMessage) .toMat(mqttSink, Keep.both()) .run(ActorMaterializer.create(getContext())); materializedValues.second().handle(this::reportReadiness); sourceActor = materializedValues.first(); addressMetric = ConnectivityModelFactory.newAddressMetric(ConnectionStatus.OPEN, "Started at " + Instant.now(), 0, null); }
.map(this::countConsumedMessage) .toMat(consumerLoadBalancer, Keep.left()) .run(ActorMaterializer.create(getContext()));
.map(this::countConsumedMessage) .toMat(consumerLoadBalancer, Keep.left()) .run(ActorMaterializer.create(getContext()));
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()); }
private Route handleSudoCountThingsPerRequest(final RequestContext ctx, final SudoCountThings command) { final CompletableFuture<HttpResponse> httpResponseFuture = new CompletableFuture<>(); Source.single(command) .to(Sink.actorRef(createHttpPerRequestActor(ctx, httpResponseFuture), HttpRequestActor.COMPLETE_MESSAGE)) .run(materializer); final CompletionStage<HttpResponse> allThingsCountHttpResponse = Source.fromCompletionStage(httpResponseFuture) .flatMapConcat(httpResponse -> httpResponse.entity().getDataBytes()) .fold(ByteString.empty(), ByteString::concat) .map(ByteString::utf8String) .map(Integer::valueOf) .map(count -> JsonObject.newBuilder().set("allThingsCount", count).build()) .map(jsonObject -> HttpResponse.create() .withEntity(ContentTypes.APPLICATION_JSON, ByteString.fromString(jsonObject.toString())) .withStatus(HttpStatusCode.OK.toInt())) .runWith(Sink.head(), materializer); return completeWithFuture(allThingsCountHttpResponse); }
@Override protected Route routes() { return route( path("data", () -> { Source<Message, NotUsed> messages = usersStream.map(String::valueOf).map(TextMessage::create); return handleWebSocketMessages(Flow.fromSinkAndSourceCoupled(Sink.ignore(), messages)); }), path("more", () -> { CompletionStage<Done> slowFuture = DBProcessor.inserUsersGraph.run(materializer); return completeOKWithFutureString(slowFuture.thenApply(done -> "Success")); }), get(() -> pathSingleSlash(() -> getFromResource("index.html") ) ) ); } }
.to(Sink.actorRef(createHttpPerRequestActor(ctx, httpResponseFuture), HttpRequestActor.COMPLETE_MESSAGE)) .run(materializer);