-
Notifications
You must be signed in to change notification settings - Fork 193
move Java 9 Flow support into main Source and Sink classes #2460
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
pjfanning
wants to merge
9
commits into
apache:main
Choose a base branch
from
pjfanning:java-flow
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f4357ef
move some JavaFlowSupport methods to Source and Sink
pjfanning a20db26
more changes
pjfanning e67567e
update docs
pjfanning ea20bd6
javafmt
pjfanning 77cc75a
asjavapublisher
pjfanning 07d8213
some doc changes
pjfanning 0efea85
build issues
pjfanning 02072f6
Update JavaFlowSupportCompileTest.java
pjfanning 63d1cf1
Update JavaFlowSupport.scala
pjfanning File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
docs/src/main/paradox/stream/operators/Sink/asJavaPublisher.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| # Sink.asJavaPublisher | ||
|
|
||
| Integration with Java Flow API, materializes into a `java.util.concurrent.Flow.Publisher`. | ||
|
|
||
| @ref[Sink operators](../index.md#sink-operators) | ||
|
|
||
| ## Signature | ||
|
|
||
| @apidoc[Sink.asJavaPublisher](Sink$) { scala="#asJavaPublisher[T](fanout:Boolean):org.apache.pekko.stream.scaladsl.Sink[T,java.util.concurrent.Flow.Publisher[T]]" java="#asJavaPublisher(org.apache.pekko.stream.javadsl.asJavaPublisher)" } | ||
|
|
||
|
|
||
|
|
||
| ## Description | ||
|
|
||
| This method gives you the capability to publish the data from the `Sink` through a Java Flow [Publisher](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/concurrent/Flow.Publisher.html). | ||
| Generally, in Pekko Streams a `Sink` is considered a subscriber, which consumes the data from source. To integrate with other Reactive Stream implementations `Sink.asJavaPublisher` provides a `Publisher` materialized value when run. | ||
| Now, the data from this publisher can be consumed by subscribing to it. We can control if we allow more than one downstream subscriber from the single running Pekko stream through the `fanout` parameter. | ||
| If you want to support a ReactiveStreams Publisher, there is @ref[Sink.asPublisher](asPublisher.md). | ||
|
|
||
| ## Example | ||
|
|
||
| In the example we are using a source and then creating a Publisher. After that, we see that when `fanout` is true multiple subscribers can subscribe to it, | ||
| but when it is false only the first subscriber will be able to subscribe and others will be rejected. | ||
|
|
||
| Scala | ||
| : @@snip [AsPublisher.scala](/docs/src/test/scala/docs/stream/operators/sink/AsPublisher.scala) { #asPublisher } | ||
|
|
||
| Java | ||
| : @@snip [SinkDocExamples.java](/docs/src/test/java/jdocs/stream/operators/SinkDocExamples.java) { #asJavaPublisher } | ||
|
|
||
| ## Reactive Streams semantics | ||
|
|
||
| @@@div { .callout } | ||
|
|
||
| **emits** the materialized publisher | ||
|
|
||
| **completes** after the source is consumed and materialized publisher is created | ||
|
|
||
| @@@ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
docs/src/main/paradox/stream/operators/Source/asJavaSubscriber.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| # Source.asJavaSubscriber | ||
|
|
||
| Integration with Java Flow API, materializes into a @javadoc[Subscriber](java.util.concurrent.Flow.Subscriber). | ||
|
|
||
| @ref[Source operators](../index.md#source-operators) | ||
|
|
||
| ## Signature | ||
|
|
||
| Scala | ||
| : @@snip[JavaFlowSupport.scala](/stream/src/main/scala/org/apache/pekko/stream/scaladsl/JavaFlowSupport.scala) { #asSubscriber } | ||
|
|
||
| Java | ||
| : @@snip[AsSubscriber.java](/docs/src/test/java/jdocs/stream/operators/source/AsSubscriber.java) { #api } | ||
|
|
||
| ## Description | ||
|
|
||
| If you want to create a @apidoc[Source] that gets its elements from another library that supports | ||
| the Java Flow API, you can use `Source.asJavaSubscriber`. | ||
| Each time this @apidoc[Source] is materialized, it produces a materialized value of type | ||
| @javadoc[java.util.concurrent.Flow.Subscriber](java.util.concurrent.Flow.Subscriber). | ||
| This @javadoc[Subscriber](java.util.concurrent.Flow.Subscriber) can be attached to a | ||
| Java Flow @javadoc[Publisher](java.util.concurrent.Flow.Publisher) | ||
| to populate it. | ||
|
|
||
| If the API you want to consume elements from provides a @javadoc[Publisher](java.util.concurrent.Flow.Publisher) instead of accepting a @javadoc[Subscriber](java.util.concurrent.Flow.Subscriber), see @ref[fromPublisher](fromPublisher.md). | ||
|
|
||
| @@@ note | ||
|
|
||
| Reactive Streams users: we prefer @javadoc[java.util.concurrent.Flow](java.util.concurrent.Flow) but you may still use the [org.reactivestreams](https://github.com/reactive-streams/reactive-streams-jvm#reactive-streams) library with @apidoc[Source.asSubscriber](Source$) { scala="#asSubscriber[T]:org.apache.pekko.stream.scaladsl.Source[T,org.reactivestreams.Subscriber[T]]" java="#asSubscriber()" }. | ||
|
|
||
| @@@ | ||
|
|
||
| ## Example | ||
|
|
||
| Suppose we use a database client that supports the Java Flow API, | ||
| we could create a @apidoc[Source] that queries the database for its rows. That @apidoc[Source] can then | ||
| be used for further processing, for example creating a @apidoc[Source] that contains the names of the | ||
| rows. | ||
|
|
||
| Note that since the database is queried for each materialization, the `rowSource` can be safely re-used. | ||
| Because both the database driver and Pekko Streams support Java Flow API, | ||
| backpressure is applied throughout the stream, preventing us from running out of memory when the database | ||
| rows are consumed slower than they are produced by the database. | ||
|
|
||
| Scala | ||
| : @@snip [AsSubscriber.scala](/docs/src/test/scala/docs/stream/operators/source/AsSubscriber.scala) { #imports #example } | ||
|
|
||
| Java | ||
| : @@snip [AsSubscriber.java](/docs/src/test/java/jdocs/stream/operators/source/AsSubscriber.java) { #imports #example } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice, much clear