|
| 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 | + |
0 commit comments