akka.stream.Materializer Java Examples

The following examples show how to use akka.stream.Materializer. 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: FullStackHttpIdentityProcessorVerificationTest.java    From netty-reactive-streams with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public void start() throws Exception {
    executorService = Executors.newCachedThreadPool();
    actorSystem = ActorSystem.create();
    materializer = Materializer.matFromSystem(actorSystem);
    helper = new HttpHelper(materializer);
    eventLoop = new NioEventLoopGroup();
    ProcessorHttpServer server = new ProcessorHttpServer(eventLoop);

    // A flow that echos HttpRequest bodies in HttpResponse bodies
    final Flow<HttpRequest, HttpResponse, NotUsed> flow = Flow.<HttpRequest>create().map(
            new Function<HttpRequest, HttpResponse>() {
                public HttpResponse apply(HttpRequest request) throws Exception {
                    return helper.echo(request);
                }
            }
    );

    serverBindChannel = server.bind(new InetSocketAddress("127.0.0.1", 0), new Callable<Processor<HttpRequest, HttpResponse>>() {
        @Override
        public Processor<HttpRequest, HttpResponse> call() throws Exception {
            return AkkaStreamsUtil.flowToProcessor(flow, materializer);
        }
    }).await().channel();
}
 
Example #2
Source File: AkkaHttpClientTest.java    From java-specialagent with Apache License 2.0 6 votes vote down vote up
@Test
public void test(final MockTracer tracer) throws IllegalAccessException, InvocationTargetException {
  final Materializer materializer = ActorMaterializer.create(system);

  final Http http = getHttp(system);

  final CompletionStage<HttpResponse> stage = http.singleRequest(HttpRequest.GET("http://localhost:12345"));
  try {
    stage.whenComplete(new BiConsumer<HttpResponse,Throwable>() {
      @Override
      public void accept(final HttpResponse httpResponse, final Throwable throwable) {
        System.out.println(httpResponse.status());
      }
    }).toCompletableFuture().get().entity().getDataBytes().runForeach(param -> {}, materializer);
  }
  catch (final Exception ignore) {
  }

  await().atMost(15, TimeUnit.SECONDS).until(TestUtil.reportedSpansSize(tracer), equalTo(1));

  final List<MockSpan> spans = tracer.finishedSpans();
  assertEquals(1, spans.size());
  assertEquals(AkkaAgentIntercept.COMPONENT_NAME_CLIENT, spans.get(0).tags().get(Tags.COMPONENT.getKey()));
}
 
Example #3
Source File: PlayWSTest.java    From java-specialagent with Apache License 2.0 6 votes vote down vote up
@Test
public void test(final MockTracer tracer) throws Exception {
  final Materializer materializer = ActorMaterializer.create(system);
  final AsyncHttpClientConfig asyncHttpClientConfig = new DefaultAsyncHttpClientConfig.Builder()
    .setMaxRequestRetry(0)
    .setShutdownQuietPeriod(0)
    .setShutdownTimeout(0)
    .build();

  final AsyncHttpClient asyncHttpClient = new DefaultAsyncHttpClient(asyncHttpClientConfig);
  try (final StandaloneAhcWSClient wsClient = new StandaloneAhcWSClient(asyncHttpClient, materializer)) {
    wsClient.url("http://localhost:1234").get().toCompletableFuture().get(15, TimeUnit.SECONDS);
  }
  catch (final Exception ignore) {
  }

  await().atMost(15, TimeUnit.SECONDS).until(TestUtil.reportedSpansSize(tracer), equalTo(1));

  final List<MockSpan> spans = tracer.finishedSpans();
  assertEquals(1, spans.size());
  assertEquals(PlayWSAgentIntercept.COMPONENT_NAME, spans.get(0).tags().get(Tags.COMPONENT.getKey()));
}
 
Example #4
Source File: AkkaHttpClientITest.java    From java-specialagent with Apache License 2.0 6 votes vote down vote up
public static void main(final String[] args) throws Exception {
  final ActorSystem system = ActorSystem.create();
  final Materializer materializer = ActorMaterializer.create(system);

  final Http http = getHttp(system);
  final CompletionStage<HttpResponse> stage = http.singleRequest(HttpRequest.GET("http://www.google.com"));
  stage.whenComplete(new BiConsumer<HttpResponse,Throwable>() {
    @Override
    public void accept(final HttpResponse httpResponse, final Throwable throwable) {
      TestUtil.checkActiveSpan();
      System.out.println(httpResponse.status());
    }
  }).toCompletableFuture().get().entity().getDataBytes().runForeach(param -> {}, materializer);

  stage.thenRun(system::terminate).toCompletableFuture().get();
  TestUtil.checkSpan(new ComponentSpanCount("akka-http-client", 1));
}
 
Example #5
Source File: DataCenterForwarder.java    From ts-reaktive with MIT License 6 votes vote down vote up
/**
   * Creates a new DataCenterForwarder and starts to forward events to a data center.
   * @param materializer Akka streams materializer to use
   * @param dataCenter Target data center to forward events to.
   * @param visibilityRepo Repository that stores the current visiblity of aggregates
   * @param eventRepo Classifier that determines which additional datacenters an event should trigger replication for
   * @param eventsByTagQuery Query to use to find a continuous stream of all events
   * @param tag Tag to pass to {@link EventsByTagQuery} (all events must be tagged by this)
   * @param currentEventsByPersistenceIdQuery Query to find all current events for a specific persistenceId
   */
  public DataCenterForwarder(Materializer materializer, DataCenter dataCenter, VisibilityRepository visibilityRepo, Class<E> eventType,
      EventsByTagQuery eventsByTagQuery, CurrentEventsByPersistenceIdQuery currentEventsByPersistenceIdQuery) {
      
final Replication replication = Replication.get(context().system());
      
this.eventsByTagQuery = eventsByTagQuery;
      this.materializer = materializer;
      this.visibilityRepo = visibilityRepo;
      this.classifier = replication.getEventClassifier(eventType);
      this.dataCenter = dataCenter;
      this.tag = replication.getEventTag(eventType);
      this.localDataCenterName = replication.getLocalDataCenterName();
      this.currentEventsByPersistenceIdQuery = currentEventsByPersistenceIdQuery;
      this.parallelism = context().system().settings().config().getInt("ts-reaktive.replication.parallellism");

      pipe(visibilityRepo.getLastEventOffset(dataCenter, tag).thenApply(LastEventOffsetKnown::new), context().dispatcher()).to(self());
      log.debug("Started");
  }
 
Example #6
Source File: PlayWSITest.java    From java-specialagent with Apache License 2.0 6 votes vote down vote up
public static void main(final String[] args) throws Exception {
  final ActorSystem system = ActorSystem.create();
  final Materializer materializer = ActorMaterializer.create(system);

  final AsyncHttpClientConfig asyncHttpClientConfig = new DefaultAsyncHttpClientConfig.Builder()
    .setMaxRequestRetry(0)
    .setShutdownQuietPeriod(0)
    .setShutdownTimeout(0)
    .build();

  final AsyncHttpClient asyncHttpClient = new DefaultAsyncHttpClient(asyncHttpClientConfig);
  try (final StandaloneAhcWSClient wsClient = new StandaloneAhcWSClient(asyncHttpClient, materializer)) {
    final int status = wsClient.url("http://www.google.com").get()
      .whenComplete((response, throwable) -> TestUtil.checkActiveSpan())
      .toCompletableFuture().get(15, TimeUnit.SECONDS)
      .getStatus();

    if (200 != status)
      throw new AssertionError("Response: " + status);
  }
  finally {
    system.terminate();
    TestUtil.checkSpan(new ComponentSpanCount("play-ws", 1));
  }
}
 
Example #7
Source File: AriCommandResponseKafkaProcessor.java    From ari-proxy with GNU Affero General Public License v3.0 6 votes vote down vote up
private static CompletionStage<Tuple2<AriResponse, CallContextAndCommandId>> toAriResponse(
		Tuple2<HttpResponse, CallContextAndCommandId> responseWithContext,
		Materializer materializer) {

	final HttpResponse response = responseWithContext._1;

	final long contentLength = response
			.entity()
			.getContentLengthOption()
			.orElseThrow(() -> new RuntimeException("failed to get content length"));

	return response
			.entity()
			.toStrict(contentLength, materializer)
			.thenCompose(strictText -> Option.of(StringUtils.trimToNull(strictText.getData().decodeString(Charset.defaultCharset())))
					.map(rawBody -> Try
							.of(() -> genericReader.readTree(rawBody))
							.map(jsonBody -> new AriResponse( response.status().intValue(), jsonBody))
							.map(res -> responseWithContext.map1(httpResponse -> res))
							.map(tuple -> CompletableFuture.completedFuture(tuple))
							.getOrElseGet(t -> Future.<Tuple2<AriResponse, CallContextAndCommandId>>failed(t).toCompletableFuture()))
					.getOrElse(CompletableFuture.completedFuture(responseWithContext.map1(httpResponse -> new AriResponse(response.status().intValue(), null))))
			);
}
 
Example #8
Source File: DataCenterForwarder.java    From ts-reaktive with MIT License 6 votes vote down vote up
/**
 * Starts a DataCenterForwarder for each of the known data centers in the {@link DataCenterRepository}.
 * @param system Actor system to create the DataCenterForwarder actors in
 * @param dataRepo Repository that knows about all data centers
 * @param materializer Akka streams materializer to use
 * @param visibilityRepo Repository that stores the current visiblity of aggregates
 * @param eventRepo Classifier that determines which additional datacenters an event should trigger replication for
 * @param eventsByTagQuery Query to use to find a continuous stream of all events
 * @param tag Tag to pass to {@link EventsByTagQuery} (all events must be tagged by this)
 * @param currentEventsByPersistenceIdQuery Query to find all current events for a specific persistenceId
 */
public static <E> void startAll(ActorSystem system, Materializer materializer, DataCenterRepository dataRepo, VisibilityRepository visibilityRepo, Class<E> eventType,
    EventsByTagQuery eventsByTagQuery, CurrentEventsByPersistenceIdQuery currentEventsByPersistenceIdQuery) {
    
    String tag = Replication.get(system).getEventTag(eventType);
    for (DataCenter dataCenter: dataRepo.getRemotes().values()) {
        system.actorOf(ClusterSingletonManager.props(
            BackoffSupervisor.props(
                Backoff.onFailure(
                    Props.create(DataCenterForwarder.class, () -> new DataCenterForwarder<>(materializer, dataCenter, visibilityRepo, eventType,
                        eventsByTagQuery, currentEventsByPersistenceIdQuery)),
                    "f",
                    Duration.create(1, TimeUnit.SECONDS),
                    Duration.create(1, TimeUnit.SECONDS), // TODO make these 3 configurable
                    0.2)
            ),
            Done.getInstance(),
            ClusterSingletonManagerSettings.create(system).withSingletonName("s")), "forwarder_" + dataCenter.getName() + "_" + tag);
    }
    
}
 
Example #9
Source File: KafkaSagaEventConsumer.java    From servicecomb-pack with Apache License 2.0 5 votes vote down vote up
public KafkaSagaEventConsumer(ActorSystem actorSystem, ActorRef sagaShardRegionActor,
    MetricsService metricsService, String bootstrap_servers, String topic) {
  super(actorSystem, sagaShardRegionActor, metricsService);


  // init consumer
  final Materializer materializer = ActorMaterializer.create(actorSystem);
  final Config consumerConfig = actorSystem.settings().config().getConfig("akka.kafka.consumer");
  final ConsumerSettings<String, String> consumerSettings =
      ConsumerSettings
          .create(consumerConfig, new StringDeserializer(), new StringDeserializer())
          .withBootstrapServers(bootstrap_servers)
          .withGroupId(groupId)
          .withProperty(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, "false")
          .withProperty(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest")
          .withProperty(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, "StringDeserializer.class")
          .withProperty(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, "StringDeserializer.class");
  Consumer.committableSource(consumerSettings, Subscriptions.topics(topic))
      .mapAsync(20, event -> {
        BaseEvent bean = jsonMapper.readValue(event.record().value(), BaseEvent.class);
        if (LOG.isDebugEnabled()) {
          LOG.debug("receive [{}] {} {}", bean.getGlobalTxId(), bean.getType(), bean.getLocalTxId());
        }
        return sendSagaActor(bean).thenApply(done -> event.committableOffset());
      })
      .batch(
          100,
          ConsumerMessage::createCommittableOffsetBatch,
          ConsumerMessage.CommittableOffsetBatch::updated
      )
      .mapAsync(20, offset -> offset.commitJavadsl())
      .to(Sink.ignore())
      .run(materializer);
}
 
Example #10
Source File: HttpStreamsTest.java    From netty-reactive-streams with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public void startEventLoop() {
    eventLoop = new NioEventLoopGroup();
    actorSystem = ActorSystem.create();
    materializer = Materializer.matFromSystem(actorSystem);
    helper = new HttpHelper(materializer);
}
 
Example #11
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 #12
Source File: AkkaStreams.java    From ts-reaktive with MIT License 5 votes vote down vote up
/**
 * Materializes the given source and waits for it to successfully emit one element. It then completes the returned
 * CompletionStage with the full stream. It will wait indefinitely for that first element, so timeouts will have to be handled
 * separately on the stream, returned future, or both.
 * 
 * This is useful in cases where you want to "fail early" when handling a stream result. For example, you might want
 * to build an http response based on a stream, but want to set a different status code if the stream fails
 * to emit any element.
 */
public static <T> CompletionStage<Source<T,NotUsed>> awaitOne(Source<T,?> source, Materializer materializer) {
    return source.prefixAndTail(1).map(pair -> {
        if (pair.first().isEmpty()) {
            return pair.second();
        } else {
            T head = pair.first().get(0);
            Source<T, NotUsed> tail = pair.second();
            return Source.single(head).concat(tail);                
        }
    }).runWith(Sink.head(), materializer);
}
 
Example #13
Source File: EventRoute.java    From ts-reaktive with MIT License 5 votes vote down vote up
/**
 * Creates a new EventRoute
 * @param journal The cassandra journal to read from
 * @param tagName The tag name of the events that this route should query
 */
public EventRoute(Materializer materializer, EventsByTagQuery journal, Marshaller<Source<EventEnvelope, ?>, HttpResponse> marshaller, String tagName) {
    this.materializer = materializer;
    this.journal = journal;
    this.tagName = tagName;
    this.marshaller = marshaller;
}
 
Example #14
Source File: Boot.java    From docker-nginx-consul with MIT License 5 votes vote down vote up
public static void main(String[] args) {
  // config
  final String appId = UUID.randomUUID().toString();
  final AppConfiguration appConfig = AppConfiguration.loadConfig(appId);

  // actor system init
  final ActorSystem system = ActorSystem.create();
  final Materializer materializer = ActorMaterializer.create(system);

  // service discovery actor
  final ActorRef serviceDiscoveryActor = system.actorOf(DiscoveryAgentActor.props(appConfig), "example-app-consul-service");

  // http init
  final Flow<HttpRequest, HttpResponse, NotUsed> routeFlow = new AppResource(appConfig).routes().flow(system, materializer);
  final CompletionStage<ServerBinding> binding = Http
      .get(system)
      .bindAndHandle(
          routeFlow,
          ConnectHttp.toHost(appConfig.host, appConfig.port),
          materializer
      );

  // exception handling
  binding.exceptionally(failure -> {
    System.err.println("Something very bad happened! " + failure.getMessage());
    system.terminate();
    return null;
  });

}
 
Example #15
Source File: CustomGzipFilter.java    From sunbird-lms-service with MIT License 5 votes vote down vote up
@Inject
public CustomGzipFilter(Materializer materializer) {
    GzipFilterConfig gzipFilterConfig = new GzipFilterConfig();
    gzipFilter = new GzipFilter(
            gzipFilterConfig.withBufferSize(BUFFER_SIZE)
                    .withChunkedThreshold(CHUNKED_THRESHOLD)
                    .withShouldGzip((BiFunction<Http.RequestHeader, Result, Object>)
                            (req, res) -> shouldGzipFunction(req, res)),
            materializer
    );
}
 
Example #16
Source File: AkkaApplicationTest.java    From reactive-code-workshop with Apache License 2.0 5 votes vote down vote up
private String dumpSourceToString(Source<?,NotUsed> f) throws InterruptedException, ExecutionException {
    final ActorSystem system = ActorSystem.create("QuickStart");
    final Materializer materializer = ActorMaterializer.create(system);

    StringBuilder s = new StringBuilder();
    final CompletionStage<Done> done = f.runWith(Sink.foreach(a -> s.append(a)),materializer);

    done.thenRun(()->system.terminate());
    done.toCompletableFuture().get();

    return s.toString();
}
 
Example #17
Source File: AkkaApplication.java    From reactive-code-workshop with Apache License 2.0 5 votes vote down vote up
private void dumpSourceToStdOut(Source<?,NotUsed> src) throws InterruptedException, ExecutionException {
    final ActorSystem system = ActorSystem.create("QuickStart");
    final Materializer materializer = ActorMaterializer.create(system);

    final CompletionStage<Done> done = src.runWith(Sink.foreach(a -> System.out.println(a)),materializer);
    done.thenRun(()->system.terminate());

    // Make it happen
    done.toCompletableFuture().get();
}
 
Example #18
Source File: AuthorizationFilter.java    From remote-monitoring-services-java with MIT License 5 votes vote down vote up
@Inject
public AuthorizationFilter(
    Materializer mat,
    IClientAuthConfig config,
    IJwtValidation jwtValidation,
    IUserManagementClient userManagementClient) {
    super(mat);
    this.authRequired = config.isAuthRequired();
    this.jwtValidation = jwtValidation;
    this.userManagementClient = userManagementClient;
}
 
Example #19
Source File: AuthorizationFilter.java    From remote-monitoring-services-java with MIT License 5 votes vote down vote up
@Inject
public AuthorizationFilter(
    Materializer mat,
    IClientAuthConfig config,
    IJwtValidation jwtValidation,
    IUserManagementClient userManagementClient) {
    super(mat);
    this.authRequired = config.isAuthRequired();
    this.jwtValidation = jwtValidation;
    this.userManagementClient = userManagementClient;
}
 
Example #20
Source File: AuthorizationFilter.java    From remote-monitoring-services-java with MIT License 5 votes vote down vote up
@Inject
public AuthorizationFilter(
    Materializer mat,
    IClientAuthConfig config,
    IJwtValidation jwtValidation,
    IUserManagementClient userManagementClient) {
    super(mat);
    this.authRequired = config.isAuthRequired();
    this.jwtValidation = jwtValidation;
    this.userManagementClient = userManagementClient;
}
 
Example #21
Source File: Messenger.java    From tutorials with MIT License 5 votes vote down vote up
private CompletionStage<MessageDTO> consumeHttpResponse(HttpResponse httpResponse) {
    Materializer materializer = Materializer.matFromSystem(getContext().getSystem());
    return Jackson.unmarshaller(MessageDTO.class)
      .unmarshal(httpResponse.entity(), materializer)
      .thenApply(messageDTO -> {
          log.info("Received message: {}", messageDTO);
          discardEntity(httpResponse, materializer);
          return messageDTO;
      });
}
 
Example #22
Source File: AkkaHttpServerITest.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
public static void main(final String[] args) throws Exception {
  final ActorSystem system = ActorSystem.create();
  final Materializer materializer = ActorMaterializer.create(system);
  final Http http = getHttp(system);

  testSync(http, materializer);
  testAsync(http, materializer);

  Await.result(system.terminate(), Duration.create(15, TimeUnit.SECONDS));
}
 
Example #23
Source File: ResultConsumer.java    From kafka-tutorials with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {

        final Config config = ConfigFactory.load();
        final String outputTopic = config.getString("output.topic.name");

        final ActorSystem system = ActorSystem.create();
        final Materializer materializer = ActorMaterializer.create(system);

        final ConsumerSettings<Windowed<String>, Long> consumerSettings =
                ConsumerSettings
                        .create(
                                system,
                                timeWindowedSerdeFrom(
                                        String.class,
                                        config.getDuration("window.size").toMillis()
                                ).deserializer(),
                                Serdes.Long().deserializer()
                        )
                        .withGroupId(UUID.randomUUID().toString())
                        .withBootstrapServers(config.getString("bootstrap.servers"))
                        .withProperty(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");

        Consumer.plainSource(
                consumerSettings,
                Subscriptions.topics(outputTopic))
                .to(Sink.foreach((record) -> {
                            logger.info(printWindowedKey(config, record));
                            return BoxedUnit.UNIT;
                        })
                ).run(materializer);

    }
 
Example #24
Source File: DBProcessor.java    From alpakka-jdbc with Apache License 2.0 4 votes vote down vote up
Server(Materializer materializer, Source<User, NotUsed> usersStream) {
    this.materializer = materializer;
    this.usersStream = usersStream;
}
 
Example #25
Source File: Messenger.java    From tutorials with MIT License 4 votes vote down vote up
private void discardEntity(HttpResponse httpResponse, Materializer materializer) {
    httpResponse.discardEntityBytes(materializer)
      .completionStage()
      .whenComplete((done, ex) -> log.info("Entity discarded completely!"));
}
 
Example #26
Source File: ExampleFilter.java    From glowroot with Apache License 2.0 4 votes vote down vote up
@Inject
public ExampleFilter(Materializer mat, Executor exec) {
    super(mat);
    this.exec = exec;
}
 
Example #27
Source File: HttpAuthenticationFilter.java    From commercetools-sunrise-java with Apache License 2.0 4 votes vote down vote up
@Inject
public HttpAuthenticationFilter(final Materializer mat) {
    super(mat);
}
 
Example #28
Source File: TracingFilter.java    From skywalking with Apache License 2.0 4 votes vote down vote up
@Inject
public TracingFilter(Materializer mat) {
    super(mat);
}
 
Example #29
Source File: HttpHelper.java    From netty-reactive-streams with Apache License 2.0 4 votes vote down vote up
public HttpHelper(Materializer materializer) {
    this.materializer = materializer;
}
 
Example #30
Source File: HomeController.java    From tutorials with MIT License 4 votes vote down vote up
@Inject
public HomeController(ActorSystem actorSystem, Materializer materializer) {
    this.actorSystem = actorSystem;
    this.materializer = materializer;
}