title | description | ms.topic | ms.author | author | ms.date |
---|---|---|---|---|---|
Publish and consume events using namespace topics - Portal |
This article provides step-by-step instructions to publish events to Azure Event Grid in the CloudEvents JSON format and consume those events by using the pull delivery model. You use the Azure portal in this quickstart. |
quickstart |
spelluru |
spelluru |
02/20/2024 |
This quickstart provides you with step-by-step instructions to use Azure portal to create an Azure Event Grid namespace, a topic in a namespace, and a subscription to the topic using Queue as the delivery mode. Then, you use Curl to send a test event, receive the event, and then acknowledge the event.
The quickstart is for a quick test of the pull delivery functionality of Event Grid. For more information about the pull delivery model, see the concepts and pull delivery overview articles.
In this quickstart, you use the Azure portal to do the following tasks.
- Create an Event Grid namespace.
- Create a topic in the namespace.
- Create a subscription for the topic using the Queue (Pull) model.
Then, you use Curl to do the following tasks to test the setup.
- Send a test event to the topic.
- Receive the event from the subscription.
- Acknowledge the event in the subscription.
[!INCLUDE quickstarts-free-trial-note.md]
An Event Grid namespace provides a user-defined endpoint to which you post your events. The following example creates a namespace in your resource group using Bash in Azure Cloud Shell. The namespace name must be unique because it's part of a Domain Name System (DNS) entry.
-
Navigate to the Azure portal.
-
In the search bar at the topic, type
Event Grid Namespaces
, and selectEvent Grid Namespaces
from the results.:::image type="content" source="./media/publish-events-using-namespace-topics-portal/search-bar-namespace-topics.png" alt-text="Screenshot that shows the search bar in the Azure portal." lightbox="./media/publish-events-using-namespace-topics-portal/search-bar-namespace-topics.png":::
-
On the Event Grid Namespaces page, select + Create on the command bar. :::image type="content" source="./media/publish-events-using-namespace-topics-portal/namespaces-create-button.png" alt-text="Screenshot that shows the Event Grid Namespaces page with the Create button on the command bar selected." lightbox="./media/publish-events-using-namespace-topics-portal/namespaces-create-button.png":::
-
On the Create Namespace page, follow these steps:
- Select the Azure subscription in which you want to create the namespace.
- Create a new resource group by selecting Create new or select an existing resource group.
- Enter a name for the namespace.
- Select the location where you want to create the resource group.
- Then, select Review + create.
:::image type="content" source="./media/publish-events-using-namespace-topics-portal/create-namespace.png" alt-text="Screenshot that shows the Create Namespace page." lightbox="./media/publish-events-using-namespace-topics-portal/create-namespace.png"::: - On the Review + create page, select Create.
-
On the Deployment page, select Go to resource after the successful deployment.
- On the Event Grid Namespace page, select Access keys on the left menu.
- Select copy button next to the access key. :::image type="content" source="./media/publish-events-using-namespace-topics-portal/access-key.png" alt-text="Screenshot that shows the Event Grid Namespaces page with the Access keys tab selected." lightbox="./media/publish-events-using-namespace-topics-portal/access-key.png":::
- Save the access key somewhere. You use it later in this quickstart.
Create a topic that's used to hold all events published to the namespace endpoint.
- Select Topics on the left menu.
- On the Topics page, select + Topic on the command bar. :::image type="content" source="./media/publish-events-using-namespace-topics-portal/topics-page.png" alt-text="Screenshot that shows the Topics page." lightbox="./media/publish-events-using-namespace-topics-portal/topics-page.png":::
- On the Create Topic page, follow these steps:
- Enter a name for the topic.
- Select Create.
:::image type="content" source="./media/publish-events-using-namespace-topics-portal/create-topic-page.png" alt-text="Screenshot that shows the Create Topic page." lightbox="./media/publish-events-using-namespace-topics-portal/create-topic-page.png":::
Create an event subscription setting its delivery mode to queue, which supports pull delivery. For more information on all configuration options,see the latest Event Grid control plane REST API.
- On the Topics page, select the topic you created in the previous step.
- Select + Subscription on the command bar. :::image type="content" source="./media/publish-events-using-namespace-topics-portal/create-subscription-button.png" alt-text="Screenshot that shows the Topic page with Create subscription button selected." lightbox="./media/publish-events-using-namespace-topics-portal/create-subscription-button.png":::
- On the Create Event Subscription page, follow these steps:
- In the Basic tab, enter a name for the event subscription, and then select Additional features tab at the top. :::image type="content" source="./media/publish-events-using-namespace-topics-portal/create-subscription-basics-page.png" alt-text="Screenshot that shows the Create Subscription page." lightbox="./media/publish-events-using-namespace-topics-portal/create-subscription-basics-page.png":::
- In the Advanced tab, enter 5 for the Lock duration. :::image type="content" source="./media/publish-events-using-namespace-topics-portal/create-subscription-lock-duration.png" alt-text="Screenshot that shows the Additional Feature tab of the Create Subscription page." lightbox="./media/publish-events-using-namespace-topics-portal/create-subscription-lock-duration.png":::
- Select Create.
Now, send a sample event to the namespace topic by following steps in this section.
-
Launch Cloud Shell in the Azure portal. Switch to Bash.
:::image type="content" source="./media/publish-events-using-namespace-topics-portal/cloud-shell-bash.png" alt-text="Screenshot that shows the Cloud Shell." lightbox="./media/publish-events-using-namespace-topics-portal/cloud-shell-bash.png":::
-
In the Cloud Shell, run the following command to declare a variable to hold the access key for the namespace. You noted the access key earlier in this quickstart.
key=ACCESSKEY
-
Declare a variable to hold the publishing operation URI. Replace
NAMESPACENAME
with the name of your Event Grid namespace andTOPICNAME
with the name of the topic.publish_operation_uri=https://NAMESPACENAME.eastus-1.eventgrid.azure.net/topics/TOPICNAME:publish?api-version=2023-06-01-preview
-
Create a sample CloudEvents compliant event:
event=' { "specversion": "1.0", "id": "'"$RANDOM"'", "type": "com.yourcompany.order.ordercreatedV2", "source" : "/mycontext", "subject": "orders/O-234595", "time": "'`date +%Y-%m-%dT%H:%M:%SZ`'", "datacontenttype" : "application/json", "data":{ "orderId": "O-234595", "url": "https://yourcompany.com/orders/o-234595"}} '
The
data
element is the payload of your event. Any well-formed JSON can go in this field. For more information on properties (also known as context attributes) that can go in an event, see the CloudEvents specifications. -
Use CURL to send the event to the topic. CURL is a utility that sends HTTP requests.
curl -X POST -H "Content-Type: application/cloudevents+json" -H "Authorization:SharedAccessKey $key" -d "$event" $publish_operation_uri
You receive events from Event Grid using an endpoint that refers to an event subscription.
-
Declare a variable to hold the receiving operation URI. Replace
NAMESPACENAME
with the name of your Event Grid namespace,TOPICNAME
with the name of the topic, and replaceEVENTSUBSCRIPTIONNAME
with the name of the event subscription.receive_operation_uri=https://NAMESPACENAME.eastus-1.eventgrid.azure.net/topics/TOPICNAME/eventsubscriptions/EVENTSUBSCRIPTIONNAME:receive?api-version=2023-06-01-preview
-
Run the following Curl command to consume the event:
curl -X POST -H "Content-Type: application/json" -H "Authorization:SharedAccessKey $key" $receive_operation_uri
-
Note down the
lockToken
in thebrokerProperties
object of the result.
After you receive an event, you pass that event to your application for processing. Once you have successfully processed your event, you no longer need that event to be in your event subscription. To instruct Event Grid to delete the event, you acknowledge it using its lock token that you got on the receive operation's response.
-
Declare a variable to hold the lock token you noted in the previous step. Replace
LOCKTOKEN
with the lock token.lockToken="LOCKTOKEN"
-
Now, build the acknowledge operation payload, which specifies the lock token for the event you want to be acknowledged.
acknowledge_request_payload=' { "lockTokens": ["'$lockToken'"]} '
-
Proceed with building the string with the acknowledge operation URI:
acknowledge_operation_uri=https://NAMESPACENAME.eastus-1.eventgrid.azure.net/topics/TOPICNAME/eventsubscriptions/EVENTSUBSCRIPTIONNAME:acknowledge?api-version=2023-06-01-preview
-
Finally, submit a request to acknowledge the event received:
curl -X POST -H "Content-Type: application/json" -H "Authorization:SharedAccessKey $key" -d "$acknowledge_request_payload" $acknowledge_operation_uri
If the acknowledge operation is executed before the lock token expires (300 seconds as set when we created the event subscription), you should see a response like the following example:
{"succeededLockTokens":["CiYKJDQ4NjY5MDEyLTk1OTAtNDdENS1BODdCLUYyMDczNTYxNjcyMxISChDZae43pMpE8J8ovYMSQBZS"],"failedLockTokens":[]}
For more information about the pull delivery model, see the concepts and pull delivery overview articles.
For sample code using the data plane SDKs, see the .NET or the Java samples. For Java, we provide the sample code in two articles: publish events and receive events quickstarts.