Skip to content

Commit

Permalink
Update Messaging Libraries to New Codesnippet Tooling (Azure#25426)
Browse files Browse the repository at this point in the history
  • Loading branch information
alzimmermsft authored and XiaofeiCao committed Nov 18, 2021
1 parent 9e117a2 commit c87cc53
Show file tree
Hide file tree
Showing 44 changed files with 1,947 additions and 252 deletions.
116 changes: 58 additions & 58 deletions sdk/eventgrid/azure-messaging-eventgrid/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,52 +119,52 @@ To send events to a topic or domain with a `TokenCredential`, the authenticated
Once you have your access key and topic endpoint, you can create the publisher client as follows:

Sync client that works for every Java developer:
<!-- embedme ./src/samples/java/com/azure/messaging/eventgrid/samples/ReadmeSamples.java#L54-L58 -->
```java

```java readme-sample-createCloudEventPublisherClient
// For CloudEvent
EventGridPublisherClient<CloudEvent> cloudEventClient = new EventGridPublisherClientBuilder()
.endpoint("<endpont of your event grid topic/domain that accepts CloudEvent schema>")
.endpoint("<endpoint of your event grid topic/domain that accepts CloudEvent schema>")
.credential(new AzureKeyCredential("<key for the endpoint>"))
.buildCloudEventPublisherClient();
```
<!-- embedme ./src/samples/java/com/azure/messaging/eventgrid/samples/ReadmeSamples.java#L62-L66 -->
```java

```java readme-sample-createEventGridEventPublisherClient
// For EventGridEvent
EventGridPublisherClient<EventGridEvent> eventGridEventClient = new EventGridPublisherClientBuilder()
.endpoint("<endpont of your event grid topic/domain that accepts EventGridEvent schema>")
.endpoint("<endpoint of your event grid topic/domain that accepts EventGridEvent schema>")
.credential(new AzureKeyCredential("<key for the endpoint>"))
.buildEventGridEventPublisherClient();
```
<!-- embedme ./src/samples/java/com/azure/messaging/eventgrid/samples/ReadmeSamples.java#L70-L74 -->
```java

```java readme-sample-createCustomEventPublisherClient
// For custom event
EventGridPublisherClient<BinaryData> customEventClient = new EventGridPublisherClientBuilder()
.endpoint("<endpont of your event grid topic/domain that accepts custom event schema>")
.endpoint("<endpoint of your event grid topic/domain that accepts custom event schema>")
.credential(new AzureKeyCredential("<key for the endpoint>"))
.buildCustomEventPublisherClient();
```
or async client if your technology stack has reactive programming such as project reactor:
<!-- embedme ./src/samples/java/com/azure/messaging/eventgrid/samples/ReadmeSamples.java#L78-L82 -->
```java

```java readme-sample-createCloudEventPublisherAsyncClient
// For CloudEvent
EventGridPublisherAsyncClient<CloudEvent> cloudEventAsyncClient = new EventGridPublisherClientBuilder()
.endpoint("<endpont of your event grid topic/domain that accepts CloudEvent schema>")
.endpoint("<endpoint of your event grid topic/domain that accepts CloudEvent schema>")
.credential(new AzureKeyCredential("<key for the endpoint>"))
.buildCloudEventPublisherAsyncClient();
```
<!-- embedme ./src/samples/java/com/azure/messaging/eventgrid/samples/ReadmeSamples.java#L86-L90 -->
```java

```java readme-sample-createEventGridEventPublisherAsyncClient
// For EventGridEvent
EventGridPublisherAsyncClient<EventGridEvent> eventGridEventAsyncClient = new EventGridPublisherClientBuilder()
.endpoint("<endpont of your event grid topic/domain that accepts EventGridEvent schema>")
.endpoint("<endpoint of your event grid topic/domain that accepts EventGridEvent schema>")
.credential(new AzureKeyCredential("<key for the endpoint>"))
.buildEventGridEventPublisherAsyncClient();
```
<!-- embedme ./src/samples/java/com/azure/messaging/eventgrid/samples/ReadmeSamples.java#L94-L98 -->
```java

```java readme-sample-createCustomEventPublisherAsyncClient
// For custom event
EventGridPublisherAsyncClient<BinaryData> customEventAsyncClient = new EventGridPublisherClientBuilder()
.endpoint("<endpont of your event grid topic/domain that accepts custom event schema>")
.endpoint("<endpoint of your event grid topic/domain that accepts custom event schema>")
.credential(new AzureKeyCredential("<key for the endpoint>"))
.buildCustomEventPublisherAsyncClient();
```
Expand All @@ -174,18 +174,18 @@ If you have a SAS (**Shared Access Signature**) that can be used to send events
limited time, you can use it to create the publisher client:

Sync client:
<!-- embedme ./src/samples/java/com/azure/messaging/eventgrid/samples/ReadmeSamples.java#L102-L105 -->
```java

```java readme-sample-createPublisherClientWithSas
EventGridPublisherClient<CloudEvent> cloudEventClient = new EventGridPublisherClientBuilder()
.endpoint("<endpont of your event grid topic/domain that accepts CloudEvent schema>")
.endpoint("<endpoint of your event grid topic/domain that accepts CloudEvent schema>")
.credential(new AzureSasCredential("<sas token that can access the endpoint>"))
.buildCloudEventPublisherClient();
```
Async client:
<!-- embedme ./src/samples/java/com/azure/messaging/eventgrid/samples/ReadmeSamples.java#L109-L112 -->
```java

```java readme-sample-createPublisherClientWithSasAsync
EventGridPublisherAsyncClient<CloudEvent> cloudEventAsyncClient = new EventGridPublisherClientBuilder()
.endpoint("<endpont of your event grid topic/domain that accepts CloudEvent schema>")
.endpoint("<endpoint of your event grid topic/domain that accepts CloudEvent schema>")
.credential(new AzureSasCredential("<sas token that can access the endpoint>"))
.buildCloudEventPublisherAsyncClient();
```
Expand All @@ -195,16 +195,16 @@ To use the AAD token credential, include `azure-identity` artifact as a dependen
[azure-identity README](https://docs.microsoft.com/java/api/overview/azure/identity-readme) for details.

Sync client:
<!-- embedme ./src/samples/java/com/azure/messaging/eventgrid/samples/ReadmeSamples.java#L231-L234 -->
```java

```java readme-sample-createPublisherClientWithTokenCredential
EventGridPublisherClient<CloudEvent> cloudEventClient = new EventGridPublisherClientBuilder()
.endpoint("<endpoint of your event grid topic/domain that accepts CloudEvent schema>")
.credential(new DefaultAzureCredentialBuilder().build())
.buildCloudEventPublisherClient();
```
Async client:
<!-- embedme ./src/samples/java/com/azure/messaging/eventgrid/samples/ReadmeSamples.java#L238-L241 -->
```java

```java readme-sample-createPublisherAsyncClientWithTokenCredential
EventGridPublisherAsyncClient<CloudEvent> cloudEventClient = new EventGridPublisherClientBuilder()
.endpoint("<endpoint of your event grid topic/domain that accepts CloudEvent schema>")
.credential(new DefaultAzureCredentialBuilder().build())
Expand All @@ -217,8 +217,8 @@ a SAS (**Shared Access Signature**) for them so they can create an `EventGridPub
to create the publisher client.

Here is sample code to create a shared access signature that expires after 20 minutes:
<!-- embedme ./src/samples/java/com/azure/messaging/eventgrid/samples/ReadmeSamples.java#L48-L50 -->
```java

```java readme-sample-createSharedAccessSignature
OffsetDateTime expiration = OffsetDateTime.now().plusMinutes(20);
String sasToken = EventGridPublisherClient
.generateSas("<your event grid endpoint>", new AzureKeyCredential("<key for the endpoint>"), expiration);
Expand Down Expand Up @@ -282,30 +282,30 @@ the synchronous client is used for samples, however the asynchronous client has

Note: figure out what schema (cloud event, event grid event, or custom event) the event grid topic accepts before you start sending.
#### Sending `EventGridEvent` to a topic that accepts EventGridEvent schema
<!-- embedme ./src/samples/java/com/azure/messaging/eventgrid/samples/ReadmeSamples.java#L125-L129 -->
```java
// Make sure that the event grid topic or domain you're sending to accepts EventGridEvent schema.
List<EventGridEvent> events = new ArrayList<>();
User user = new User("John", "James");
events.add(new EventGridEvent("exampleSubject", "Com.Example.ExampleEventType", BinaryData.fromObject(user), "0.1"));
eventGridEventClient.sendEvents(events);
```

#### Sending `CloudEvent` to a topic that accepts CloudEvent schema
<!-- embedme ./src/samples/java/com/azure/messaging/eventgrid/samples/ReadmeSamples.java#L116-L121 -->
```java
// Make sure that the event grid topic or domain you're sending to accepts CloudEvent schema.
```java readme-sample-sendCloudEventsToTopic
// Make sure that the event grid topic or domain you're sending to is able to accept the CloudEvent schema.
List<CloudEvent> events = new ArrayList<>();
User user = new User("John", "James");
events.add(new CloudEvent("https://source.example.com", "Com.Example.ExampleEventType",
BinaryData.fromObject(user), CloudEventDataFormat.JSON, "application/json"));
cloudEventClient.sendEvents(events);
```

#### Sending `CloudEvent` to a topic that accepts CloudEvent schema

```java readme-sample-sendEventGridEventsToTopic
// Make sure that the event grid topic or domain you're sending to is able to accept the EventGridEvent schema.
List<EventGridEvent> events = new ArrayList<>();
User user = new User("John", "James");
events.add(new EventGridEvent("exampleSubject", "Com.Example.ExampleEventType", BinaryData.fromObject(user), "0.1"));
eventGridEventClient.sendEvents(events);
```

#### Sending Custom Events to a topic that accepts custom event schema
<!-- embedme ./src/samples/java/com/azure/messaging/eventgrid/samples/ReadmeSamples.java#L141-L154 -->
```java
// Make sure that the event grid topic or domain you're sending to accepts the custom event schema.

```java readme-sample-sendCustomEventsToTopic
// Make sure that the event grid topic or domain you're sending to is able to accept the custom event schema.
List<BinaryData> events = new ArrayList<>();
events.add(BinaryData.fromObject(new HashMap<String, String>() {
{
Expand All @@ -326,8 +326,8 @@ An [Event Grid Domain](https://docs.microsoft.com/azure/event-grid/event-domains
but has a single endpoint. You can use a domain to manage a set of related topics. Sending events to the topics of
an Event Grid Domain is the same as sending events to a regular Event Grid Topic except that you need to
specify the `topic` of an `EventGridEvent` if the domain accepts `EventGridEvent` schema.
<!-- embedme ./src/samples/java/com/azure/messaging/eventgrid/samples/ReadmeSamples.java#L133-L137 -->
```java

```java readme-sample-sendEventGridEventsToDomain
List<EventGridEvent> events = new ArrayList<>();
User user = new User("John", "James");
events.add(new EventGridEvent("com/example", "Com.Example.ExampleEventType", BinaryData.fromObject(user), "1")
Expand All @@ -338,7 +338,7 @@ eventGridEventClient.sendEvents(events);
If the domain accepts `CloudEvent` schema, the CloudEvent's attribute that is configured to map the `topic` when the
domain is created must be set. The default mapping attribute is `source`.

### Recieving and Consuming Events
### Receiving and Consuming Events
The Event Grid service doesn't store events. So this Event Grid SDK doesn't have an event receiver.
Instead, events are stored in the [Event Handlers](#event-handlers-and-event-deserialization), including ServiceBus, EventHubs, Storage Queue, WebHook endpoint, or many other supported Azure Services.
However, currently all events will be sent and stored as encoded JSON data. Here is some basic code that details the deserialization
Expand All @@ -347,8 +347,8 @@ from the topic/subscription.

#### Deserialize `EventGridEvent` or `CloudEvent` from a Json String
The Json String can have a single event or an array of events. The returned result is a list of events.
<!-- embedme ./src/samples/java/com/azure/messaging/eventgrid/samples/ReadmeSamples.java#L158-L164 -->
```java

```java readme-sample-deserializeEvents
// Deserialize an EventGridEvent
String eventGridEventJsonData = "<your EventGridEvent json String>";
List<EventGridEvent> eventGridEvents = EventGridEvent.fromString(eventGridEventJsonData);
Expand All @@ -366,8 +366,8 @@ object, which has methods to further deserialize the data into usable types:
- `BinaryData.toString()` gets the data as a String
- `BinaryData.toObject()` gets the data as an object of a specific type. It uses Json deserializer by default. It has
an overload to accept your deserializer if you want to use your own.
<!-- embedme ./src/samples/java/com/azure/messaging/eventgrid/samples/ReadmeSamples.java#L168-L184 -->
```java

```java readme-sample-deserializeEventData
BinaryData eventData = eventGridEvent.getData();

//Deserialize data to a model class
Expand Down Expand Up @@ -407,14 +407,14 @@ Receiving and consuming system events is the same as other events. Additionally,
for the various system event data are defined in package `com.azure.messaging.eventgrid.systemevents`. You can do the
following after you deserialize an event by using `EventGridEvent.fromString()` or `CloudEvent.fromString()`:
- look up the system event data model class that the System Event data can be deserialized to;
<!-- embedme ./src/samples/java/com/azure/messaging/eventgrid/samples/ReadmeSamples.java#L193-L194 -->
```java

```java readme-sample-lookupSystemEventClass
// Look up the System Event data class
Class<?> eventDataClazz = SystemEventNames.getSystemEventMappings().get(event.getEventType());
```
- deserialize a system event's data to a model class instance like deserializing any other event data;
<!-- embedme ./src/samples/java/com/azure/messaging/eventgrid/samples/ReadmeSamples.java#L196-L201 -->
```java

```java readme-sample-deserializeToSystemEventType
// Deserialize the event data to an instance of a specific System Event data class type
BinaryData data = event.getData();
if (data != null) {
Expand All @@ -423,8 +423,8 @@ if (data != null) {
}
```
- deal with multiple event types.
<!-- embedme ./src/samples/java/com/azure/messaging/eventgrid/samples/ReadmeSamples.java#L205-L225 -->
```java

```java readme-sample-systemEventDifferentEventData
List<EventGridEvent> eventGridEvents = EventGridEvent.fromString("<Your EventGridEvent Json String>");
for (EventGridEvent eventGridEvent : eventGridEvents) {
BinaryData binaryData = eventGridEvent.getData();
Expand Down
3 changes: 3 additions & 0 deletions sdk/eventgrid/azure-messaging-eventgrid/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
--add-opens com.azure.messaging.eventgrid/com.azure.messaging.eventgrid=ALL-UNNAMED
--add-opens com.azure.messaging.eventgrid/com.azure.messaging.eventgrid.implementation=ALL-UNNAMED
</javaModulesSurefireArgLine>
<codesnippet.skip>false</codesnippet.skip>
<javadocDoclet></javadocDoclet>
<javadocDocletOptions></javadocDocletOptions>
</properties>

<developers>
Expand Down
Loading

0 comments on commit c87cc53

Please sign in to comment.