EventGridPublisherClient<T> Class
- java.
lang. Object - com.
azure. messaging. eventgrid. EventGridPublisherClient<T>
- com.
Type Parameters
- T
public final class EventGridPublisherClient<T>
A service client that publishes events to an EventGrid topic or domain. Use EventGridPublisherClientBuilder to create an instance of this client. Note that this is simply a synchronous convenience layer over the EventGridPublisherAsyncClient<T>, which has more efficient asynchronous functionality and is recommended.
Create EventGridPublisherClient for CloudEvent Samples
// Create a client to send events of CloudEvent schema (com.azure.core.models.CloudEvent)
EventGridPublisherClient<CloudEvent> cloudEventPublisherClient = new EventGridPublisherClientBuilder()
.endpoint(System.getenv("AZURE_EVENTGRID_CLOUDEVENT_ENDPOINT")) // make sure it accepts CloudEvent
.credential(new AzureKeyCredential(System.getenv("AZURE_EVENTGRID_CLOUDEVENT_KEY")))
.buildCloudEventPublisherClient();
Send CloudEvent Samples
// Create a com.azure.models.CloudEvent.
User user = new User("Stephen", "James");
CloudEvent cloudEventDataObject = new CloudEvent("/cloudevents/example/source", "Example.EventType",
BinaryData.fromObject(user), CloudEventDataFormat.JSON, "application/json");
// Send a single CloudEvent
cloudEventPublisherClient.sendEvent(cloudEventDataObject);
// Send a list of CloudEvents to the EventGrid service altogether.
// This has better performance than sending one by one.
cloudEventPublisherClient.sendEvents(Arrays.asList(
cloudEventDataObject
// add more CloudEvents objects
));
Create EventGridPublisherClient for EventGridEvent Samples
// Create a client to send events of EventGridEvent schema
EventGridPublisherClient<EventGridEvent> eventGridEventPublisherClient = new EventGridPublisherClientBuilder()
.endpoint(System.getenv("AZURE_EVENTGRID_EVENT_ENDPOINT")) // make sure it accepts EventGridEvent
.credential(new AzureKeyCredential(System.getenv("AZURE_EVENTGRID_EVENT_KEY")))
.buildEventGridEventPublisherClient();
Send EventGridEvent Samples
// Create an EventGridEvent
User user = new User("John", "James");
EventGridEvent eventGridEvent = new EventGridEvent("/EventGridEvents/example/source",
"Example.EventType", BinaryData.fromObject(user), "0.1");
// Send a single EventGridEvent
eventGridEventPublisherClient.sendEvent(eventGridEvent);
// Send a list of EventGridEvents to the EventGrid service altogether.
// This has better performance than sending one by one.
eventGridEventPublisherClient.sendEvents(Arrays.asList(
eventGridEvent
// add more EventGridEvents objects
));
Create EventGridPublisherClient for Custom Event Schema Samples
// Create a client to send events of custom event
EventGridPublisherClient<BinaryData> customEventPublisherClient = new EventGridPublisherClientBuilder()
.endpoint(System.getenv("AZURE_CUSTOM_EVENT_ENDPOINT")) // make sure it accepts custom events
.credential(new AzureKeyCredential(System.getenv("AZURE_CUSTOM_EVENT_KEY")))
.buildCustomEventPublisherClient();
Send Custom Event Schema Samples
// Create an custom event object
Map<String, Object> customEvent = new HashMap<String, Object>() {
{
put("id", UUID.randomUUID().toString());
put("subject", "Test");
put("foo", "bar");
put("type", "Microsoft.MockPublisher.TestEvent");
put("data", 100.0);
put("dataVersion", "0.1");
}
};
// Send a single custom event
customEventPublisherClient.sendEvent(BinaryData.fromObject(customEvent));
// Send a list of custom events to the EventGrid service altogether.
// This has better performance than sending one by one.
customEventPublisherClient.sendEvents(Arrays.asList(
BinaryData.fromObject(customEvent)
// add more custom events in BinaryData
));
Method Summary
| Modifier and Type | Method and Description |
|---|---|
| static String |
generateSas(String endpoint, AzureKeyCredential keyCredential, OffsetDateTime expirationTime)
Generate a shared access signature to provide time-limited authentication for requests to the Event Grid service with the latest Event Grid service API defined in getLatest(). |
| static String |
generateSas(String endpoint, AzureKeyCredential keyCredential, OffsetDateTime expirationTime, EventGridServiceVersion apiVersion)
Generate a shared access signature to provide time-limited authentication for requests to the Event Grid service. |
| void |
sendEvent(T event)
Publishes the given event to the set topic or domain and gives the response issued by Event |
| void |
sendEvents(Iterable<T> events)
Publishes the given events to the given topic or domain. |
| Response<Void> |
sendEventsWithResponse(Iterable<T> events, Context context)
Publishes the given events to the set topic or domain and gives the response issued by Event |
| Response<Void> |
sendEventsWithResponse(Iterable<T> events, String channelName, Context context)
Publishes the given events to the set topic or domain and gives the response issued by Event |
Methods inherited from java.lang.Object
Method Details
generateSas
public static String generateSas(String endpoint, AzureKeyCredential keyCredential, OffsetDateTime expirationTime)
Generate a shared access signature to provide time-limited authentication for requests to the Event Grid service with the latest Event Grid service API defined in getLatest().
Parameters:
Returns:
generateSas
public static String generateSas(String endpoint, AzureKeyCredential keyCredential, OffsetDateTime expirationTime, EventGridServiceVersion apiVersion)
Generate a shared access signature to provide time-limited authentication for requests to the Event Grid service.
Parameters:
Returns:
sendEvent
public void sendEvent(T event)
Publishes the given event to the set topic or domain and gives the response issued by EventGrid.
Parameters:
sendEvents
public void sendEvents(Iterable<T> events)
Publishes the given events to the given topic or domain.
Parameters:
sendEventsWithResponse
public Response<Void> sendEventsWithResponse(Iterable<T> events, Context context)
Publishes the given events to the set topic or domain and gives the response issued by EventGrid.
Parameters:
Returns:
sendEventsWithResponse
public Response<Void> sendEventsWithResponse(Iterable<T> events, String channelName, Context context)
Publishes the given events to the set topic or domain and gives the response issued by EventGrid.
Parameters:
Returns: