azure-docs-sdk-java/docs-ref-autogen/com.azure.messaging.eventgr...

18 строки
6.9 KiB
YAML

### YamlMime:JavaPackage
uid: "com.azure.messaging.eventgrid"
fullName: "com.azure.messaging.eventgrid"
name: "com.azure.messaging.eventgrid"
summary: "[Azure Event Grid][] is a highly scalable, fully managed event routing service.\n\n\n[Azure Event Grid]: https://learn.microsoft.com/en-us/azure/event-grid/"
classes:
- "com.azure.messaging.eventgrid.EventGridEvent"
- "com.azure.messaging.eventgrid.EventGridPublisherAsyncClient"
- "com.azure.messaging.eventgrid.EventGridPublisherClient"
- "com.azure.messaging.eventgrid.EventGridPublisherClientBuilder"
- "com.azure.messaging.eventgrid.SystemEventNames"
enums:
- "com.azure.messaging.eventgrid.EventGridServiceVersion"
desc: "[Azure Event Grid][] is a highly scalable, fully managed event routing service. With Event Grid can connect applications and services to react to relevant events. This library is for publishing Event Grid events and deserializing event payloads in subscriptions.\n\n**Key Concepts:**\n\n * **Event** \\- Information about what happened.\n * **Event Source** \\- where the event took place.\n * **Topic** \\- the endpoint where events are published to.\n * **Event Handler** \\- the endpoint that handles the events.\n * **Event Subscription** \\- the endpoint or built-in mechanism for routing events.\n\nFor more information see the [concepts overview.][]\n\n## Getting Started ##\n\nThe Azure EventGrid SDK provides <xref uid=\"com.azure.messaging.eventgrid.EventGridPublisherClient\" data-throw-if-not-resolved=\"false\" data-raw-source=\"com.azure.messaging.eventgrid.EventGridPublisherClient\"></xref> and <xref uid=\"com.azure.messaging.eventgrid.EventGridPublisherAsyncClient\" data-throw-if-not-resolved=\"false\" data-raw-source=\"com.azure.messaging.eventgrid.EventGridPublisherAsyncClient\"></xref> for synchronous and asynchronous publishing of events to Azure Event Grid. These can be instantiated using the <xref uid=\"com.azure.messaging.eventgrid.EventGridPublisherClientBuilder\" data-throw-if-not-resolved=\"false\" data-raw-source=\"com.azure.messaging.eventgrid.EventGridPublisherClientBuilder\"></xref>.\n\n### Authentication ###\n\nThere are three ways to authenticate a publisher client for Azure Event Grid.\n\n**Microsoft Entra ID**: Using managed identity is the recommended way to authenticate. The recommended way to do so is using DefaultAzureCredential:\n\n```java\nDefaultAzureCredential credential = new DefaultAzureCredentialBuilder().build();\n EventGridPublisherClient<EventGridEvent> eventGridEventPublisherClient = new EventGridPublisherClientBuilder()\n .endpoint(System.getenv(\"AZURE_EVENTGRID_EVENT_ENDPOINT\"))\n .credential(credential)\n .buildEventGridEventPublisherClient();\n```\n\n**Topic Access Key**: When a topic is created, an access key is created for that topic. It is used with the AzureKeyCredential:\n\n```java\nAzureKeyCredential credential = new AzureKeyCredential(System.getenv(\"AZURE_EVENTGRID_EVENT_TOPIC_KEY\"));\n EventGridPublisherClient<EventGridEvent> eventGridEventPublisherClient = new EventGridPublisherClientBuilder()\n .endpoint(System.getenv(\"AZURE_EVENTGRID_EVENT_ENDPOINT\"))\n .credential(credential)\n .buildEventGridEventPublisherClient();\n```\n\n**Shared Access Signature**: A Shared Access Signature (SAS) key can be used to authenticate. First, you must create one:\n\n```java\n// You can get a SAS token using static methods of EventGridPublisherClient.\n String sasKey = EventGridPublisherClient.generateSas(System.getenv(\"AZURE_EVENTGRID_EVENT_ENDPOINT\"),\n new AzureKeyCredential(System.getenv(\"AZURE_EVENTGRID_EVENT_TOPIC_KEY\")),\n OffsetDateTime.now().plusHours(1));\n```\n\nOnce it is created, it is used with a SAS token credential:\n\n```java\n// Once you have this key, you can share it with anyone who needs to send events to your topic. They use it like this:\n AzureSasCredential credential = new AzureSasCredential(sasKey);\n EventGridPublisherClient<EventGridEvent> eventGridEventPublisherClient = new EventGridPublisherClientBuilder()\n .endpoint(System.getenv(\"AZURE_EVENTGRID_EVENT_ENDPOINT\"))\n .credential(credential)\n .buildEventGridEventPublisherClient();\n```\n\n--------------------\n\n## Send an EventGridEvent ##\n\nIn order to interact with the Azure Event Grid service, you will need to create an instance of the <xref uid=\"com.azure.messaging.eventgrid.EventGridPublisherClient\" data-throw-if-not-resolved=\"false\" data-raw-source=\"com.azure.messaging.eventgrid.EventGridPublisherClient\"></xref> class:\n\n```java\n// Create a client to send events of EventGridEvent schema\n EventGridPublisherClient<EventGridEvent> eventGridEventPublisherClient = new EventGridPublisherClientBuilder()\n .endpoint(System.getenv(\"AZURE_EVENTGRID_EVENT_ENDPOINT\")) // make sure it accepts EventGridEvent\n .credential(new AzureKeyCredential(System.getenv(\"AZURE_EVENTGRID_EVENT_KEY\")))\n .buildEventGridEventPublisherClient();\n```\n\n```java\n// Create an EventGridEvent\n User user = new User(\"John\", \"James\");\n EventGridEvent eventGridEvent = new EventGridEvent(\"/EventGridEvents/example/source\",\n \"Example.EventType\", BinaryData.fromObject(user), \"0.1\");\n\n // Send a single EventGridEvent\n eventGridEventPublisherClient.sendEvent(eventGridEvent).block();\n\n // Send a list of EventGridEvents to the EventGrid service altogether.\n // This has better performance than sending one by one.\n eventGridEventPublisherClient.sendEvents(Arrays.asList(\n eventGridEvent\n // add more EventGridEvents objects\n )).block();\n```\n\n--------------------\n\n## Send a Cloud Event ##\n\n```java\n// Create a client to send events of CloudEvent schema (com.azure.core.models.CloudEvent)\n EventGridPublisherAsyncClient<CloudEvent> cloudEventPublisherClient = new EventGridPublisherClientBuilder()\n .endpoint(System.getenv(\"AZURE_EVENTGRID_CLOUDEVENT_ENDPOINT\")) // make sure it accepts CloudEvent\n .credential(new AzureKeyCredential(System.getenv(\"AZURE_EVENTGRID_CLOUDEVENT_KEY\")))\n .buildCloudEventPublisherAsyncClient();\n```\n\n```java\n// Create a com.azure.models.CloudEvent.\n User user = new User(\"Stephen\", \"James\");\n CloudEvent cloudEventDataObject = new CloudEvent(\"/cloudevents/example/source\", \"Example.EventType\",\n BinaryData.fromObject(user), CloudEventDataFormat.JSON, \"application/json\");\n\n // Send a single CloudEvent\n cloudEventPublisherClient.sendEvent(cloudEventDataObject).block();\n\n // Send a list of CloudEvents to the EventGrid service altogether.\n // This has better performance than sending one by one.\n cloudEventPublisherClient.sendEvents(Arrays.asList(\n cloudEventDataObject\n // add more CloudEvents objects\n )).block();\n```\n\n\n[Azure Event Grid]: https://learn.microsoft.com/en-us/azure/event-grid/\n[concepts overview.]: https://learn.microsoft.com/en-us/azure/event-grid/concepts"
metadata: {}
package: "com.azure.messaging.eventgrid"
artifact: com.azure:azure-messaging-eventgrid:4.24.0