-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add bidi subscription to validate workflow. Signed-off-by: Artur Souza <[email protected]>
- Loading branch information
1 parent
cedaebc
commit 763fd6e
Showing
19 changed files
with
851 additions
and
605 deletions.
There are no files selected for viewing
This file contains 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 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 was deleted.
Oops, something went wrong.
122 changes: 122 additions & 0 deletions
122
examples/src/main/java/io/dapr/examples/pubsub/stream/README.md
This file contains 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,122 @@ | ||
# Dapr Streaming Subscription Sample | ||
|
||
In this sample, we'll create a publisher and a subscriber java applications using Dapr, based on the publish-subscribe pattern. The publisher will generate messages of a specific topic, while a subscriber will listen for messages of a specific topic via a bi-directional stream. All is abstracted by the SDK. See the [Dapr Pub-Sub docs](https://docs.dapr.io/developing-applications/building-blocks/pubsub/) to understand when this pattern might be a good choice for your software architecture. | ||
|
||
Visit [this](https://docs.dapr.io/developing-applications/building-blocks/pubsub/pubsub-overview/) link for more information about Dapr and Pub-Sub. | ||
|
||
## Pub-Sub Sample using the Java-SDK | ||
|
||
This sample shows how the subscription to events no longer requires the application to listen to an HTTP or gRPC port. This example uses Redis Streams (enabled in Redis versions => 5). | ||
## Pre-requisites | ||
|
||
* [Dapr CLI](https://docs.dapr.io/getting-started/install-dapr-cli/). | ||
* Java JDK 11 (or greater): | ||
* [Microsoft JDK 11](https://docs.microsoft.com/en-us/java/openjdk/download#openjdk-11) | ||
* [Oracle JDK 11](https://www.oracle.com/technetwork/java/javase/downloads/index.html#JDK11) | ||
* [OpenJDK 11](https://jdk.java.net/11/) | ||
* [Apache Maven](https://maven.apache.org/install.html) version 3.x. | ||
|
||
### Checking out the code | ||
|
||
Clone this repository: | ||
|
||
```sh | ||
git clone https://github.com/dapr/java-sdk.git | ||
cd java-sdk | ||
``` | ||
|
||
Then build the Maven project: | ||
|
||
```sh | ||
# make sure you are in the `java-sdk` directory. | ||
mvn install | ||
``` | ||
|
||
Then get into the examples directory: | ||
|
||
```sh | ||
cd examples | ||
``` | ||
### Initialize Dapr | ||
|
||
Run `dapr init` to initialize Dapr in Self-Hosted Mode if it's not already initialized. | ||
|
||
### Running the subscriber | ||
|
||
The subscriber uses the `DaprPreviewClient` interface to use a new feature where events are subscribed via a streaming and processed via a callback interface. | ||
|
||
|
||
|
||
The publisher is a simple Java application with a main method that uses the Dapr gRPC Client to publish 10 messages to a specific topic. | ||
|
||
In the `Subscriber.java` file, you will find the `Subscriber` class, containing the main method. The main method declares a `DaprPreviewClient` using the `DaprClientBuilder` class. When invoking `subscribeToEvents`, the subscriber provides an implementation of the `SubscriptionListener` interface, receiving a `Subscription` object. The `Subscription` object implements the `Closeable` interface and the `close()` method must be used to stop the subscription. | ||
|
||
```java | ||
public class Subscriber { | ||
|
||
// ... | ||
|
||
public static void main(String[] args) throws Exception { | ||
String topicName = getTopicName(args); | ||
try (var client = new DaprClientBuilder().buildPreviewClient()) { | ||
var subscription = client.subscribeToEvents( | ||
PUBSUB_NAME, | ||
topicName, | ||
new SubscriptionListener<>() { | ||
|
||
@Override | ||
public Status onEvent(CloudEvent<String> event) { | ||
System.out.println("Subscriber got: " + event.getData()); | ||
return Status.SUCCESS; | ||
} | ||
|
||
@Override | ||
public void onError(RuntimeException exception) { | ||
System.out.println("Subscriber got exception: " + exception.getMessage()); | ||
} | ||
}, | ||
TypeRef.STRING); | ||
|
||
subscription.awaitTermination(); | ||
} | ||
} | ||
|
||
// ... | ||
} | ||
``` | ||
|
||
Execute the following command to run the Subscriber example: | ||
|
||
<!-- STEP | ||
name: Run Subscriber | ||
expected_stdout_lines: | ||
- '== APP == Subscriber got: This is message #0' | ||
- '== APP == Subscriber got: This is message #1' | ||
background: true | ||
sleep: 30 | ||
--> | ||
|
||
```bash | ||
dapr run --resources-path ./components/pubsub --app-id subscriber -- java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.pubsub.stream.Subscriber | ||
``` | ||
|
||
<!-- END_STEP --> | ||
|
||
Once the subscriber is running, run the publisher in a new terminal to see the events in the subscriber's side: | ||
|
||
<!-- STEP | ||
name: Run Publisher | ||
expected_stdout_lines: | ||
- '== APP == Published message: This is message #0' | ||
- '== APP == Published message: This is message #1' | ||
background: true | ||
sleep: 15 | ||
--> | ||
|
||
```bash | ||
dapr run --resources-path ./components/pubsub --app-id publisher -- java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.pubsub.Publisher | ||
``` | ||
|
||
<!-- END_STEP --> | ||
|
||
|
88 changes: 88 additions & 0 deletions
88
examples/src/main/java/io/dapr/examples/pubsub/stream/Subscriber.java
This file contains 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,88 @@ | ||
/* | ||
* Copyright 2021 The Dapr Authors | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package io.dapr.examples.pubsub.stream; | ||
|
||
import io.dapr.client.DaprClientBuilder; | ||
import io.dapr.client.DaprPreviewClient; | ||
import io.dapr.client.SubscriptionListener; | ||
import io.dapr.client.domain.CloudEvent; | ||
import io.dapr.examples.DaprApplication; | ||
import io.dapr.utils.TypeRef; | ||
import io.grpc.Server; | ||
import io.grpc.ServerBuilder; | ||
import org.apache.commons.cli.CommandLine; | ||
import org.apache.commons.cli.CommandLineParser; | ||
import org.apache.commons.cli.DefaultParser; | ||
import org.apache.commons.cli.Options; | ||
|
||
/** | ||
* Subscriber using bi-directional gRPC streaming, which does not require an app port. | ||
* 1. Build and install jars: | ||
* mvn clean install | ||
* 2. cd [repo root]/examples | ||
* 3. Run the subscriber: | ||
* dapr run --resources-path ./components/pubsub --app-id subscriber -- \ | ||
* java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.pubsub.stream.Subscriber | ||
*/ | ||
public class Subscriber { | ||
|
||
//The title of the topic to be used for publishing | ||
private static final String DEFAULT_TOPIC_NAME = "testingtopic"; | ||
|
||
//The name of the pubsub | ||
private static final String PUBSUB_NAME = "messagebus"; | ||
|
||
/** | ||
* This is the entry point for this example app, which subscribes to a topic. | ||
* @param args Used to optionally pass a topic name. | ||
* @throws Exception An Exception on startup. | ||
*/ | ||
public static void main(String[] args) throws Exception { | ||
String topicName = getTopicName(args); | ||
try (var client = new DaprClientBuilder().buildPreviewClient()) { | ||
var subscription = client.subscribeToEvents( | ||
PUBSUB_NAME, | ||
topicName, | ||
new SubscriptionListener<>() { | ||
|
||
@Override | ||
public Status onEvent(CloudEvent<String> event) { | ||
System.out.println("Subscriber got: " + event.getData()); | ||
return Status.SUCCESS; | ||
} | ||
|
||
@Override | ||
public void onError(RuntimeException exception) { | ||
System.out.println("Subscriber got exception: " + exception.getMessage()); | ||
} | ||
}, | ||
TypeRef.STRING); | ||
|
||
subscription.awaitTermination(); | ||
} | ||
} | ||
|
||
/** | ||
* If a topic is specified in args, use that. | ||
* Else, fallback to the default topic. | ||
* @param args program arguments | ||
* @return name of the topic to publish messages to. | ||
*/ | ||
private static String getTopicName(String[] args) { | ||
if (args.length >= 1) { | ||
return args[0]; | ||
} | ||
return DEFAULT_TOPIC_NAME; | ||
} | ||
} |
This file contains 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 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 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.