title | description | author | ms.author | ms.reviewer | ms.date | ms.topic | ms.custom |
---|---|---|---|---|---|---|---|
Use Azure Event Grid in Spring | Configure a Spring Boot application created with the Spring Initializr to use the Azure Event Grid. | KarlErickson | karler | xiada | 04/18/2025 | article | devx-track-java, spring-cloud-azure, devx-track-extended-java |
This article shows you how to use Azure Event Grid to send an event to a topic and use Service Bus Queue as an Event Handler to receive in a Spring Boot application.
The Azure Event Grid service is a highly scalable, fully managed Pub Sub message distribution service that offers flexible message consumption patterns using the MQTT and HTTP protocols.
An Azure subscription - create one for free.
Java Development Kit (JDK) version 8 or higher.
Apache Maven, version 3.0 or higher.
An Event Grid Topic instance. If you don't have one, see Create a custom topic or a domain in Azure Event Grid.
A Service Bus Queue instance. If you don't have one, see Create a queue in the Azure portal.
A Spring Boot application. If you don't have one, create a Maven project with the Spring Initializr. Be sure to select Maven Project and select Java version 8 or higher.
Use the following steps to create an event subscription to tell the Event Grid to send events to the Service Bus Queue:
- In the Azure portal, navigate to your Event Grid Topic instance.
- Select Event Subscriptions on the toolbar.
- On the Create Event Subscription page, enter a name value for the event subscription.
- For Endpoint Type, select Service Bus Queue.
- Choose Select an endpoint and then select the Service Bus Queue instance you created earlier.
With an Azure Event Grid resource, you can send an event using Spring Cloud Azure Event Grid. With an Azure Service Bus Queue resource as an event handler, you can receive the event using Spring Cloud Azure Stream Binder for Service Bus.
To install the Spring Cloud Azure Event Grid Starter module and the Spring Cloud Azure Stream Binder Service Bus module, add the following dependencies to your pom.xml file:
The Spring Cloud Azure Bill of Materials (BOM):
<dependencyManagement> <dependencies> <dependency> <groupId>com.azure.spring</groupId> <artifactId>spring-cloud-azure-dependencies</artifactId> <version>5.22.0</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
[!NOTE] If you're using Spring Boot 2.x, be sure to set the
spring-cloud-azure-dependencies
version to4.20.0
. This Bill of Material (BOM) should be configured in the<dependencyManagement>
section of your pom.xml file. This ensures that all Spring Cloud Azure dependencies are using the same version. For more information about the version used for this BOM, see Which Version of Spring Cloud Azure Should I Use.The Spring Cloud Azure Event Grid Starter artifact:
<dependency> <groupId>com.azure.spring</groupId> <artifactId>spring-cloud-azure-starter-eventgrid</artifactId> </dependency>
The Spring Cloud Azure Stream Binder Service Bus artifact:
<dependency> <groupId>com.azure.spring</groupId> <artifactId>spring-cloud-azure-stream-binder-servicebus</artifactId> </dependency>
Use the following steps to configure your application to send an event by using Event Grid and receive by using Service Bus Queue.
Configure Azure Event Grid and Service Bus credentials in the application.yaml configuration file, as shown in the following example:
spring: cloud: azure: eventgrid: endpoint: ${AZURE_EVENTGRID_ENDPOINT} key: ${AZURE_EVENTGRID_KEY} servicebus: connection-string: ${AZURE_SERVICEBUS_CONNECTION_STRING} function: definition: consume stream: bindings: consume-in-0: destination: ${AZURE_SERVICEBUS_QUEUE_NAME} servicebus: bindings: consume-in-0: consumer: auto-complete: false
[!INCLUDE security-note]
Edit the startup class file to show the following content. This code generates completions.
importcom.azure.core.util.BinaryData; importcom.azure.messaging.eventgrid.EventGridEvent; importcom.azure.messaging.eventgrid.EventGridPublisherClient; importorg.slf4j.Logger; importorg.slf4j.LoggerFactory; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.boot.CommandLineRunner; importorg.springframework.boot.SpringApplication; importorg.springframework.boot.autoconfigure.SpringBootApplication; importorg.springframework.context.annotation.Bean; importorg.springframework.messaging.Message; importjava.util.List; importjava.util.function.Consumer; @SpringBootApplicationpublicclassEventGridSampleApplicationimplementsCommandLineRunner { privatestaticfinalLoggerLOGGER = LoggerFactory.getLogger(EventGridSampleApplication.class); @AutowiredEventGridPublisherClient<EventGridEvent> client; publicstaticvoidmain(String[] args) { SpringApplication.run(EventGridSampleApplication.class, args); } @BeanpublicConsumer<Message<String>> consume() { returnmessage -> { List<EventGridEvent> eventData = EventGridEvent.fromString(message.getPayload()); eventData.forEach(event -> { LOGGER.info("New event received: '{}'", event.getData()); }); }; } @Overridepublicvoidrun(String... args) throwsException { Stringstr = "FirstName: John, LastName: James"; EventGridEventevent = newEventGridEvent("A user is created", "User.Created.Text", BinaryData.fromObject(str), "0.1"); client.sendEvent(event); LOGGER.info("New event published: '{}'", event.getData()); } }
Start the application. After launch, the application produces logs similar to the following example:
New event published: '"FirstName: John, LastName: James"' ... New event received: '"FirstName: John, LastName: James"'
[!INCLUDE deploy-to-azure-spring-apps]
To learn more about Spring and Azure, continue to the Spring on Azure documentation center.
[!div class="nextstepaction"] Azure for Spring developersSpring Cloud Azure Event Grid samples