Skip to content

Latest commit

 

History

History
185 lines (140 loc) · 7.25 KB

configure-spring-boot-initializer-java-app-with-event-grid.md

File metadata and controls

185 lines (140 loc) · 7.25 KB
titledescriptionauthorms.authorms.reviewerms.datems.topicms.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

Use Azure Event Grid in Spring

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.

Prerequisites

Subscribe to custom topic

Use the following steps to create an event subscription to tell the Event Grid to send events to the Service Bus Queue:

  1. In the Azure portal, navigate to your Event Grid Topic instance.
  2. Select Event Subscriptions on the toolbar.
  3. On the Create Event Subscription page, enter a name value for the event subscription.
  4. For Endpoint Type, select Service Bus Queue.
  5. Choose Select an endpoint and then select the Service Bus Queue instance you created earlier.

Send an event by Azure Event Grid and receive by Azure Service Bus Queue

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 to 4.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>

Code the application

Use the following steps to configure your application to send an event by using Event Grid and receive by using Service Bus Queue.

  1. 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]

  2. 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()); } }
  3. 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]

Next steps

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

close