-
Notifications
You must be signed in to change notification settings - Fork 195
Expand file tree
/
Copy pathAsSubscriber.java
More file actions
58 lines (49 loc) · 1.63 KB
/
AsSubscriber.java
File metadata and controls
58 lines (49 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* license agreements; and to You under the Apache License, version 2.0:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* This file is part of the Apache Pekko project, which was derived from Akka.
*/
/*
* Copyright (C) 2019-2022 Lightbend Inc. <https://www.lightbend.com>
*/
package jdocs.stream.operators.source;
// #imports
import java.util.concurrent.Flow.Publisher;
import org.apache.pekko.NotUsed;
// #imports
import org.apache.pekko.stream.javadsl.Source;
public interface AsSubscriber {
static class Row {
public String getField(String fieldName) {
throw new UnsupportedOperationException("Not implemented in sample");
}
}
static class DatabaseClient {
Publisher<Row> fetchRows() {
throw new UnsupportedOperationException("Not implemented in sample");
}
}
DatabaseClient databaseClient = null;
// #example
class Example {
Source<Row, NotUsed> rowSource =
Source.<Row>asJavaSubscriber()
.mapMaterializedValue(
subscriber -> {
// For each materialization, fetch the rows from the database:
Publisher<Row> rows = databaseClient.fetchRows();
rows.subscribe(subscriber);
return NotUsed.getInstance();
});
public Source<String, NotUsed> names() {
// rowSource can be re-used, since it will start a new
// query for each materialization, fully supporting backpressure
// for each materialized stream:
return rowSource.map(row -> row.getField("name"));
}
}
// #example
}