forked from Azure/azure-sdk-for-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPublishEvent.java
68 lines (57 loc) · 2.97 KB
/
PublishEvent.java
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
59
60
61
62
63
64
65
66
67
68
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import com.azure.core.amqp.exception.AmqpException;
import com.azure.messaging.eventhubs.EventData;
import com.azure.messaging.eventhubs.EventHubClient;
import com.azure.messaging.eventhubs.EventHubClientBuilder;
import com.azure.messaging.eventhubs.EventHubProducer;
import java.io.IOException;
import static java.nio.charset.StandardCharsets.UTF_8;
/**
* Sample demonstrates how to send a message to an Azure Event Hub.
*/
public class PublishEvent {
/**
* Main method to invoke this demo on how to send a message to an Azure Event Hub.
*
* @param args Unused arguments to the program.
*/
public static void main(String[] args) {
// The connection string value can be obtained by:
// 1. Going to your Event Hubs namespace in Azure Portal.
// 2. Creating an Event Hub instance.
// 3. Creating a "Shared access policy" for your Event Hub instance.
// 4. Copying the connection string from the policy's properties.
String connectionString = "Endpoint={endpoint};SharedAccessKeyName={sharedAccessKeyName};SharedAccessKey={sharedAccessKey};EntityPath={eventHubPath}";
// Instantiate a client that will be used to call the service.
EventHubClient client = new EventHubClientBuilder()
.connectionString(connectionString)
.buildAsyncClient();
// Create a producer. This overload of `createProducer` does not accept any arguments. Consequently, events
// sent from this producer are load balanced between all available partitions in the Event Hub instance.
EventHubProducer producer = client.createProducer();
// Create an event to send.
EventData data = new EventData("Hello world!".getBytes(UTF_8));
// Send that event. This call returns a Mono<Void>, which we subscribe to. It completes successfully when the
// event has been delivered to the Event Hub. It completes with an error if an exception occurred while sending
// the event.
producer.send(data).subscribe(
(ignored) -> System.out.println("Event sent."),
error -> {
System.err.println("There was an error sending the event: " + error.toString());
if (error instanceof AmqpException) {
AmqpException amqpException = (AmqpException) error;
System.err.println(String.format("Is send operation retriable? %s. Error condition: %s",
amqpException.isTransient(), amqpException.getErrorCondition()));
}
}, () -> {
// Disposing of our producer and client.
try {
producer.close();
} catch (IOException e) {
System.err.println("Error encountered while closing producer: " + e.toString());
}
client.close();
});
}
}