Skip to content

Commit 81591b9

Browse files
initial bulk publish impl for java (#789)
* initial bulk publish impl for java Signed-off-by: Mukundan Sundararajan <[email protected]> * add UTs and clean up java doc for new interface methods. Signed-off-by: Mukundan Sundararajan <[email protected]> * add more interface methods for bulk publish Signed-off-by: Mukundan Sundararajan <[email protected]> * adding examples and ITs for bulk publish Signed-off-by: Mukundan Sundararajan <[email protected]> * addressing review comments Signed-off-by: Mukundan Sundararajan <[email protected]> * use latest ref from dapr branch Signed-off-by: Mukundan Sundararajan <[email protected]> * add example validation Signed-off-by: Mukundan Sundararajan <[email protected]> * fix bindings example validation Signed-off-by: Mukundan Sundararajan <[email protected]> * make changes for latest bulk publish dapr changes Signed-off-by: Mukundan Sundararajan <[email protected]> * fix examples Signed-off-by: Mukundan Sundararajan <[email protected]> * fix examples Signed-off-by: Mukundan Sundararajan <[email protected]> * fix typo Signed-off-by: Mukundan Sundararajan <[email protected]> * test against java 11 only Signed-off-by: Mukundan Sundararajan <[email protected]> * change to latest dapr commit Signed-off-by: Mukundan Sundararajan <[email protected]> * run only pubsub IT, upload failsafe reports as run artifact Signed-off-by: Mukundan Sundararajan <[email protected]> * fix checkstyle Signed-off-by: Mukundan Sundararajan <[email protected]> * fix IT report upload condition Signed-off-by: Mukundan Sundararajan <[email protected]> * fix compile issues Signed-off-by: Mukundan Sundararajan <[email protected]> * fix spotbugs issue Signed-off-by: Mukundan Sundararajan <[email protected]> * run pubsubIT only Signed-off-by: Mukundan Sundararajan <[email protected]> * change upload artifact name for IT Signed-off-by: Mukundan Sundararajan <[email protected]> * fix tests Signed-off-by: Mukundan Sundararajan <[email protected]> * fix Signed-off-by: Mukundan Sundararajan <[email protected]> * introduce sleep Signed-off-by: Mukundan Sundararajan <[email protected]> * test bulk publish with redis Signed-off-by: Mukundan Sundararajan <[email protected]> * change longvalues test to kafka Signed-off-by: Mukundan Sundararajan <[email protected]> * change bulk pub to kafka and revert long values changes Signed-off-by: Mukundan Sundararajan <[email protected]> * remove kafka pubsub from pubsub IT Signed-off-by: Mukundan Sundararajan <[email protected]> * change match order in examples Signed-off-by: Mukundan Sundararajan <[email protected]> * set fail fast as false Signed-off-by: Mukundan Sundararajan <[email protected]> * fix Internal Invoke exception in ITs Signed-off-by: Mukundan Sundararajan <[email protected]> * address review comments Signed-off-by: Mukundan Sundararajan <[email protected]> * fix IT Signed-off-by: Mukundan Sundararajan <[email protected]> * fix app scopes in examples Signed-off-by: Mukundan Sundararajan <[email protected]> * add content to daprdocs Signed-off-by: Mukundan Sundararajan <[email protected]> * address review comments Signed-off-by: Mukundan Sundararajan <[email protected]> * fix mm.py step comment Signed-off-by: Mukundan Sundararajan <[email protected]> * reset bindings examples readme Signed-off-by: Mukundan Sundararajan <[email protected]> * fix example, IT and make classes immutable Signed-off-by: Mukundan Sundararajan <[email protected]> Signed-off-by: Mukundan Sundararajan <[email protected]> Co-authored-by: Artur Souza <[email protected]>
1 parent eb8565c commit 81591b9

32 files changed

+1942
-50
lines changed

.github/workflows/build.yml

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,10 @@ jobs:
100100
- name: Codecov
101101
uses: codecov/[email protected]
102102
- name: Install jars
103-
run: mvn install -q
103+
run: mvn install -q
104104
- name: Integration tests
105-
run: mvn -f sdk-tests/pom.xml verify -q
105+
id: integration_tests
106+
run: mvn -f sdk-tests/pom.xml verify
106107
- name: Upload test report for sdk
107108
uses: actions/upload-artifact@master
108109
with:
@@ -113,6 +114,19 @@ jobs:
113114
with:
114115
name: report-dapr-java-sdk-actors
115116
path: sdk-actors/target/jacoco-report/
117+
- name: Upload failsafe test report for sdk-tests on failure
118+
if: ${{ failure() && steps.integration_tests.conclusion == 'failure' }}
119+
uses: actions/upload-artifact@master
120+
with:
121+
name: failsafe-report-sdk-tests
122+
path: sdk-tests/target/failsafe-reports
123+
- name: Upload surefire test report for sdk-tests on failure
124+
if: ${{ failure() && steps.integration_tests.conclusion == 'failure' }}
125+
uses: actions/upload-artifact@master
126+
with:
127+
name: surefire-report-sdk-tests
128+
path: sdk-tests/target/surefire-reports
129+
116130
publish:
117131
runs-on: ubuntu-latest
118132
needs: build

.github/workflows/validate.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ jobs:
2828
validate:
2929
runs-on: ubuntu-latest
3030
strategy:
31+
fail-fast: false # Keep running if one leg fails.
3132
matrix:
3233
java: [ 11, 13, 15, 16 ]
3334
env:
@@ -129,10 +130,10 @@ jobs:
129130
working-directory: ./examples
130131
run: |
131132
mm.py ./src/main/java/io/dapr/examples/state/README.md
132-
- name: Validate pubsub HTTP example
133+
- name: Validate pubsub example
133134
working-directory: ./examples
134135
run: |
135-
mm.py ./src/main/java/io/dapr/examples/pubsub/http/README.md
136+
mm.py ./src/main/java/io/dapr/examples/pubsub/README.md
136137
- name: Validate bindings HTTP example
137138
working-directory: ./examples
138139
run: |

daprdocs/content/en/java-sdk-docs/_index.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,35 @@ public class SubscriberController {
222222
}
223223
```
224224

225+
##### Bulk Publish Messages
226+
> Note: API is in Alpha stage
227+
228+
229+
```java
230+
import io.dapr.client.DaprClientBuilder;
231+
import io.dapr.client.DaprPreviewClient;
232+
import io.dapr.client.domain.BulkPublishResponse;
233+
import io.dapr.client.domain.BulkPublishResponseFailedEntry;
234+
import java.util.ArrayList;
235+
import java.util.List;
236+
class Solution {
237+
public void publishMessages() {
238+
try (DaprPreviewClient client = (new DaprClientBuilder()).buildPreviewClient()) {
239+
// Create a list of messages to publish
240+
List<String> messages = new ArrayList<>();
241+
for (int i = 0; i < NUM_MESSAGES; i++) {
242+
String message = String.format("This is message #%d", i);
243+
messages.add(message);
244+
System.out.println("Going to publish message : " + message);
245+
}
246+
247+
// Publish list of messages using the bulk publish API
248+
BulkPublishResponse<String> res = client.publishEvents(PUBSUB_NAME, TOPIC_NAME, "text/plain", messages).block()
249+
}
250+
}
251+
}
252+
```
253+
225254
- For a full guide on publishing messages and subscribing to a topic [How-To: Publish & subscribe]({{< ref howto-publish-subscribe.md >}}).
226255
- Visit [Java SDK examples](https://github.com/dapr/java-sdk/tree/master/examples/src/main/java/io/dapr/examples/pubsub/http) for code samples and instructions to try out pub/sub
227256

examples/components/pubsub/redis_messagebus.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,8 @@ spec:
1010
value: localhost:6379
1111
- name: redisPassword
1212
value: ""
13+
scopes:
14+
- publisher
15+
- bulk-publisher
16+
- subscriber
17+
- publisher-tracing

examples/src/main/java/io/dapr/examples/bindings/http/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ cd examples
5050

5151
Before getting into the application code, follow these steps in order to set up a local instance of Kafka. This is needed for the local instances. Steps are:
5252

53-
1. To run container locally run:
53+
1. To run container locally run:
5454

5555
<!-- STEP
5656
name: Setup kafka container
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* Copyright 2023 The Dapr Authors
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package io.dapr.examples.pubsub;
15+
16+
import io.dapr.client.DaprClient;
17+
import io.dapr.client.DaprClientBuilder;
18+
import io.dapr.client.DaprPreviewClient;
19+
import io.dapr.client.domain.BulkPublishResponse;
20+
import io.dapr.client.domain.BulkPublishResponseFailedEntry;
21+
import io.dapr.examples.OpenTelemetryConfig;
22+
import io.opentelemetry.api.OpenTelemetry;
23+
import io.opentelemetry.api.trace.Span;
24+
import io.opentelemetry.api.trace.Tracer;
25+
import io.opentelemetry.context.Scope;
26+
import io.opentelemetry.sdk.OpenTelemetrySdk;
27+
28+
import java.util.ArrayList;
29+
import java.util.List;
30+
31+
import static io.dapr.examples.OpenTelemetryConfig.getReactorContext;
32+
33+
/**
34+
* Message publisher.
35+
* 1. Build and install jars:
36+
* mvn clean install
37+
* 2. cd [repo root]/examples
38+
* 3. Run the program:
39+
* dapr run --components-path ./components/pubsub --app-id bulk-publisher -- \
40+
* java -Ddapr.grpc.port="50010" -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.pubsub.BulkPublisher
41+
*/
42+
public class BulkPublisher {
43+
44+
private static final int NUM_MESSAGES = 10;
45+
46+
private static final String TOPIC_NAME = "bulkpublishtesting";
47+
48+
//The name of the pubsub
49+
private static final String PUBSUB_NAME = "messagebus";
50+
51+
/**
52+
* main method.
53+
*
54+
* @param args incoming args
55+
* @throws Exception any exception
56+
*/
57+
public static void main(String[] args) throws Exception {
58+
OpenTelemetry openTelemetry = OpenTelemetryConfig.createOpenTelemetry();
59+
Tracer tracer = openTelemetry.getTracer(BulkPublisher.class.getCanonicalName());
60+
Span span = tracer.spanBuilder("Bulk Publisher's Main").setSpanKind(Span.Kind.CLIENT).startSpan();
61+
try (DaprPreviewClient client = (new DaprClientBuilder()).buildPreviewClient()) {
62+
DaprClient c = (DaprClient) client;
63+
c.waitForSidecar(10000);
64+
try (Scope scope = span.makeCurrent()) {
65+
System.out.println("Using preview client...");
66+
List<String> messages = new ArrayList<>();
67+
System.out.println("Constructing the list of messages to publish");
68+
for (int i = 0; i < NUM_MESSAGES; i++) {
69+
String message = String.format("This is message #%d", i);
70+
messages.add(message);
71+
System.out.println("Going to publish message : " + message);
72+
}
73+
BulkPublishResponse<?> res = client.publishEvents(PUBSUB_NAME, TOPIC_NAME, "text/plain", messages)
74+
.subscriberContext(getReactorContext()).block();
75+
System.out.println("Published the set of messages in a single call to Dapr");
76+
if (res != null) {
77+
if (res.getFailedEntries().size() > 0) {
78+
// Ideally this condition will not happen in examples
79+
System.out.println("Some events failed to be published");
80+
for (BulkPublishResponseFailedEntry<?> entry : res.getFailedEntries()) {
81+
System.out.println("EntryId : " + entry.getEntry().getEntryId()
82+
+ " Error message : " + entry.getErrorMessage());
83+
}
84+
}
85+
} else {
86+
throw new Exception("null response from dapr");
87+
}
88+
}
89+
// Close the span.
90+
91+
span.end();
92+
// Allow plenty of time for Dapr to export all relevant spans to the tracing infra.
93+
Thread.sleep(10000);
94+
// Shutdown the OpenTelemetry tracer.
95+
OpenTelemetrySdk.getGlobalTracerManagement().shutdown();
96+
97+
System.out.println("Done");
98+
}
99+
}
100+
}
101+
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright 2023 The Dapr Authors
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package io.dapr.examples.pubsub;
15+
16+
import io.dapr.client.DaprClientBuilder;
17+
import io.dapr.client.DaprPreviewClient;
18+
import io.dapr.client.domain.BulkPublishEntry;
19+
import io.dapr.client.domain.BulkPublishRequest;
20+
import io.dapr.client.domain.BulkPublishResponse;
21+
import io.dapr.client.domain.BulkPublishResponseFailedEntry;
22+
import io.dapr.client.domain.CloudEvent;
23+
24+
import java.util.ArrayList;
25+
import java.util.HashMap;
26+
import java.util.List;
27+
import java.util.Map;
28+
import java.util.UUID;
29+
30+
/**
31+
* Message publisher.
32+
* 1. Build and install jars:
33+
* mvn clean install
34+
* 2. cd [repo root]/examples
35+
* 3. Run the program:
36+
* dapr run --components-path ./components/pubsub --app-id publisher -- \
37+
* java -Ddapr.grpc.port="50010" \
38+
* -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.pubsub.CloudEventBulkPublisher
39+
*/
40+
public class CloudEventBulkPublisher {
41+
42+
private static final int NUM_MESSAGES = 10;
43+
44+
private static final String TOPIC_NAME = "bulkpublishtesting";
45+
46+
//The name of the pubsub
47+
private static final String PUBSUB_NAME = "messagebus";
48+
49+
/**
50+
* main method.
51+
*
52+
* @param args incoming args
53+
* @throws Exception any exception
54+
*/
55+
public static void main(String[] args) throws Exception {
56+
try (DaprPreviewClient client = (new DaprClientBuilder()).buildPreviewClient()) {
57+
System.out.println("Using preview client...");
58+
List<BulkPublishEntry<CloudEvent<Map<String, String>>>> entries = new ArrayList<>();
59+
for (int i = 0; i < NUM_MESSAGES; i++) {
60+
CloudEvent<Map<String, String>> cloudEvent = new CloudEvent<>();
61+
cloudEvent.setId(UUID.randomUUID().toString());
62+
cloudEvent.setType("example");
63+
cloudEvent.setSpecversion("1");
64+
cloudEvent.setDatacontenttype("application/json");
65+
String val = String.format("This is message #%d", i);
66+
cloudEvent.setData(new HashMap<>() {
67+
{
68+
put("dataKey", val);
69+
}
70+
});
71+
BulkPublishEntry<CloudEvent<Map<String, String>>> entry = new BulkPublishEntry<>(
72+
"" + (i + 1), cloudEvent, CloudEvent.CONTENT_TYPE, null);
73+
entries.add(entry);
74+
}
75+
BulkPublishRequest<CloudEvent<Map<String, String>>> request = new BulkPublishRequest<>(PUBSUB_NAME, TOPIC_NAME,
76+
entries);
77+
BulkPublishResponse<?> res = client.publishEvents(request).block();
78+
if (res != null) {
79+
if (res.getFailedEntries().size() > 0) {
80+
// Ideally this condition will not happen in examples
81+
System.out.println("Some events failed to be published");
82+
for (BulkPublishResponseFailedEntry<?> entry : res.getFailedEntries()) {
83+
System.out.println("EntryId : " + entry.getEntry().getEntryId()
84+
+ " Error message : " + entry.getErrorMessage());
85+
}
86+
}
87+
} else {
88+
throw new Exception("null response");
89+
}
90+
System.out.println("Done");
91+
}
92+
}
93+
}
94+

examples/src/main/java/io/dapr/examples/pubsub/http/CloudEventPublisher.java renamed to examples/src/main/java/io/dapr/examples/pubsub/CloudEventPublisher.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
limitations under the License.
1212
*/
1313

14-
package io.dapr.examples.pubsub.http;
14+
package io.dapr.examples.pubsub;
1515

1616
import io.dapr.client.DaprClient;
1717
import io.dapr.client.DaprClientBuilder;
@@ -30,7 +30,7 @@
3030
* 2. cd [repo root]/examples
3131
* 3. Run the program:
3232
* dapr run --components-path ./components/pubsub --app-id publisher -- \
33-
* java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.pubsub.http.CloudEventPublisher
33+
* java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.pubsub.CloudEventPublisher
3434
*/
3535
public class CloudEventPublisher {
3636

examples/src/main/java/io/dapr/examples/pubsub/http/Publisher.java renamed to examples/src/main/java/io/dapr/examples/pubsub/Publisher.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
limitations under the License.
1212
*/
1313

14-
package io.dapr.examples.pubsub.http;
14+
package io.dapr.examples.pubsub;
1515

1616
import io.dapr.client.DaprClient;
1717
import io.dapr.client.DaprClientBuilder;
@@ -26,7 +26,7 @@
2626
* 2. cd [repo root]/examples
2727
* 3. Run the program:
2828
* dapr run --components-path ./components/pubsub --app-id publisher -- \
29-
* java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.pubsub.http.Publisher
29+
* java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.pubsub.Publisher
3030
*/
3131
public class Publisher {
3232

examples/src/main/java/io/dapr/examples/pubsub/http/PublisherWithTracing.java renamed to examples/src/main/java/io/dapr/examples/pubsub/PublisherWithTracing.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
limitations under the License.
1212
*/
1313

14-
package io.dapr.examples.pubsub.http;
14+
package io.dapr.examples.pubsub;
1515

1616
import io.dapr.client.DaprClient;
1717
import io.dapr.client.DaprClientBuilder;
@@ -31,7 +31,7 @@
3131
* 2. cd [repo root]/examples
3232
* 3. Run the program:
3333
* dapr run --components-path ./components/pubsub --app-id publisher-tracing -- \
34-
* java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.pubsub.http.PublisherWithTracing
34+
* java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.pubsub.PublisherWithTracing
3535
*/
3636
public class PublisherWithTracing {
3737

0 commit comments

Comments
 (0)