本文整理汇总了Java中akka.stream.Materializer类的典型用法代码示例。如果您正苦于以下问题:Java Materializer类的具体用法?Java Materializer怎么用?Java Materializer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Materializer类属于akka.stream包,在下文中一共展示了Materializer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import akka.stream.Materializer; //导入依赖的package包/类
public static void main(String[] args) {
final ActorSystem system = ActorSystem.create("KafkaProducerSystem");
final Materializer materializer = ActorMaterializer.create(system);
final ProducerSettings<byte[], String> producerSettings =
ProducerSettings
.create(system, new ByteArraySerializer(), new StringSerializer())
.withBootstrapServers("localhost:9092");
CompletionStage<Done> done =
Source.range(1, 100)
.map(n -> n.toString())
.map(elem ->
new ProducerRecord<byte[], String>(
"topic1-ts",
0,
Instant.now().getEpochSecond(),
null,
elem))
.runWith(Producer.plainSink(producerSettings), materializer);
done.whenComplete((d, ex) -> System.out.println("sent"));
}
示例2: testReactiveStream
import akka.stream.Materializer; //导入依赖的package包/类
@Test
public void testReactiveStream() throws InterruptedException, TimeoutException {
final ActorSystem system =
ActorSystem.create("MySystem");
final Materializer mat =
ActorMaterializer.create(system);
Source<Integer, NotUsed> source = Source.range(1, 5);
Flow<Integer, Integer, NotUsed> flow = Flow
.fromFunction(x -> x + 1);
Source<Integer, NotUsed> source2 = source.via(flow);
Sink<Integer, CompletionStage<Integer>> fold =
Sink.<Integer, Integer>fold(0, (next, total) -> total + next);
CompletionStage<Integer> integerCompletionStage = source2.runWith(fold, mat);
integerCompletionStage.thenAccept(System.out::println);
Thread.sleep(3000);
Await.ready(system.terminate(), Duration.apply(10, TimeUnit.SECONDS));
}
示例3: testReactiveStreamRefined
import akka.stream.Materializer; //导入依赖的package包/类
@Test
public void testReactiveStreamRefined() throws InterruptedException, TimeoutException {
final ActorSystem system =
ActorSystem.create("MySystem");
final Materializer mat =
ActorMaterializer.create(system);
Source<Integer, NotUsed> source = Source.range(1, 5).map(x -> x + 1)
.fold(0, (next, total) -> total + next);
CompletionStage<Integer> integerCompletionStage =
source.runWith(Sink.head(), mat);
integerCompletionStage.thenAccept(System.out::println);
Thread.sleep(3000);
Await.ready(system.terminate(), Duration.apply(10, TimeUnit.SECONDS));
}
示例4: startAll
import akka.stream.Materializer; //导入依赖的package包/类
/**
* 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);
}
}
示例5: DataCenterForwarder
import akka.stream.Materializer; //导入依赖的package包/类
/**
* 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");
}
示例6: importAll
import akka.stream.Materializer; //导入依赖的package包/类
public CompletableFuture<Done> importAll() {
AtomicLong batch = new AtomicLong(0);
long start = System.currentTimeMillis();
final Materializer materializer = ActorMaterializer.create(ActorSystem.create("actor-system", ConfigFactory.load()));
Source<Page, NotUsed> channelSource = Source.fromIterator(() -> reader);
Flow<Page, List<Page>, NotUsed> processing = Flow.of(Page.class)
.filter(x -> x != null && x.getNamespace() == 0)
.buffer(cores * 100, OverflowStrategy.backpressure())
.mapAsyncUnordered(cores, x -> cleaner.clean(x))
.groupedWithin(BATCH_SIZE,
FiniteDuration.apply(1, TimeUnit.SECONDS))
.mapAsyncUnordered(cores, x -> datastore.save(x));
final CompletionStage<Done> promise = channelSource.via(processing).runForeach(x -> {
System.out.printf("Inserted: %d, Time: %d sec\n",
batch.incrementAndGet() * BATCH_SIZE,
(System.currentTimeMillis() - start) / 1000);
}, materializer);
return promise.toCompletableFuture();
}
示例7: HomeController
import akka.stream.Materializer; //导入依赖的package包/类
@Inject
public HomeController(ActorSystem actorSystem,
Materializer mat,
WebJarAssets webJarAssets) {
org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(this.getClass());
LoggingAdapter logging = Logging.getLogger(actorSystem.eventStream(), logger.getName());
//noinspection unchecked
Source<String, Sink<String, NotUsed>> source = MergeHub.of(String.class)
.log("source", logging)
.recoverWithRetries(-1, new PFBuilder().match(Throwable.class, e -> Source.empty()).build());
Sink<String, Source<String, NotUsed>> sink = BroadcastHub.of(String.class);
Pair<Sink<String, NotUsed>, Source<String, NotUsed>> sinkSourcePair = source.toMat(sink, Keep.both()).run(mat);
Sink<String, NotUsed> chatSink = sinkSourcePair.first();
Source<String, NotUsed> chatSource = sinkSourcePair.second();
this.userFlow = Flow.fromSinkAndSource(chatSink, chatSource).log("userFlow", logging);
this.webJarAssets = webJarAssets;
}
示例8: HomeController
import akka.stream.Materializer; //导入依赖的package包/类
@Inject
public HomeController(ActorSystem actorSystem,
Materializer mat) {
org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(this.getClass());
LoggingAdapter logging = Logging.getLogger(actorSystem.eventStream(), logger.getName());
//noinspection unchecked
Source<String, Sink<String, NotUsed>> source = MergeHub.of(String.class)
.log("source", logging)
.recoverWithRetries(-1, new PFBuilder().match(Throwable.class, e -> Source.empty()).build());
Sink<String, Source<String, NotUsed>> sink = BroadcastHub.of(String.class);
Pair<Sink<String, NotUsed>, Source<String, NotUsed>> sinkSourcePair = source.toMat(sink, Keep.both()).run(mat);
Sink<String, NotUsed> chatSink = sinkSourcePair.first();
Source<String, NotUsed> chatSource = sinkSourcePair.second();
this.userFlow = Flow.fromSinkAndSource(chatSink, chatSource).log("userFlow", logging);
}
示例9: HomeController
import akka.stream.Materializer; //导入依赖的package包/类
@Inject
public HomeController(ActorSystem actorSystem,
Materializer mat,
WebJarsUtil webJarsUtil) {
org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(this.getClass());
LoggingAdapter logging = Logging.getLogger(actorSystem.eventStream(), logger.getName());
//noinspection unchecked
Source<String, Sink<String, NotUsed>> source = MergeHub.of(String.class)
.log("source", logging)
.recoverWithRetries(-1, new PFBuilder().match(Throwable.class, e -> Source.empty()).build());
Sink<String, Source<String, NotUsed>> sink = BroadcastHub.of(String.class);
Pair<Sink<String, NotUsed>, Source<String, NotUsed>> sinkSourcePair = source.toMat(sink, Keep.both()).run(mat);
Sink<String, NotUsed> chatSink = sinkSourcePair.first();
Source<String, NotUsed> chatSource = sinkSourcePair.second();
this.userFlow = Flow.fromSinkAndSource(chatSink, chatSource).log("userFlow", logging);
this.webJarsUtil = webJarsUtil;
}
示例10: UserActor
import akka.stream.Materializer; //导入依赖的package包/类
@Inject
public UserActor(@Assisted String id,
@Named("stocksActor") ActorRef stocksActor,
Materializer mat) {
this.id = id;
this.stocksActor = stocksActor;
this.mat = mat;
Pair<Sink<JsonNode, NotUsed>, Source<JsonNode, NotUsed>> sinkSourcePair =
MergeHub.of(JsonNode.class, 16)
.toMat(BroadcastHub.of(JsonNode.class, 256), Keep.both())
.run(mat);
this.hubSink = sinkSourcePair.first();
Source<JsonNode, NotUsed> hubSource = sinkSourcePair.second();
Sink<JsonNode, CompletionStage<Done>> jsonSink = Sink.foreach((JsonNode json) -> {
// When the user types in a stock in the upper right corner, this is triggered,
String symbol = json.findPath("symbol").asText();
addStocks(Collections.singleton(symbol));
});
// Put the source and sink together to make a flow of hub source as output (aggregating all
// stocks as JSON to the browser) and the actor as the sink (receiving any JSON messages
// from the browse), using a coupled sink and source.
this.websocketFlow = Flow.fromSinkAndSourceCoupled(jsonSink, hubSource)
//.log("actorWebsocketFlow", logger)
.watchTermination((n, stage) -> {
// When the flow shuts down, make sure this actor also stops.
stage.thenAccept(f -> context().stop(self()));
return NotUsed.getInstance();
});
}
示例11: AuthorizationFilter
import akka.stream.Materializer; //导入依赖的package包/类
@Inject
public AuthorizationFilter(
Materializer mat,
IClientAuthConfig config,
IJwtValidation jwtValidation) {
super(mat);
this.authRequired = config.isAuthRequired();
this.jwtValidation = jwtValidation;
}
示例12: run1
import akka.stream.Materializer; //导入依赖的package包/类
private void run1() {
final ActorSystem actorSystem = ActorSystem.create();
final Materializer materializer = ActorMaterializer.create(actorSystem);
final Source<Integer, NotUsed> source = Source.range(0, 10);
final Flow<Integer, String, NotUsed> flow = Flow.fromFunction((Integer i) -> i.toString());
final Sink<String, CompletionStage<Done>> sink = Sink.foreach(s -> System.out.println("Example1 - Number: " + s));
final RunnableGraph runnable = source.via(flow).to(sink);
runnable.run(materializer);
actorSystem.terminate();
}
示例13: run2
import akka.stream.Materializer; //导入依赖的package包/类
private void run2() {
final ActorSystem actorSystem = ActorSystem.create();
final Materializer materializer = ActorMaterializer.create(actorSystem);
Source.range(0, 10)
.map(Object::toString)
.runForeach(s -> System.out.println("Example 2 - Number: " + s), materializer);
actorSystem.terminate();
}
示例14: Routes
import akka.stream.Materializer; //导入依赖的package包/类
@Inject
public Routes(Application application,
Assets assets, WebJarAssets webJars, Materializer materializer) {
this.application = application;
this.assets = assets;
this.webJars = webJars;
this.materializer = materializer;
this.router = buildRouter();
}
示例15: LoadTestServiceImpl
import akka.stream.Materializer; //导入依赖的package包/类
@Inject
public LoadTestServiceImpl(FriendService friendService, ActivityStreamService activityService,
ChirpService chirpService, Materializer materializer) {
this.friendService = friendService;
this.activityService = activityService;
this.chirpService = chirpService;
this.materializer = materializer;
}