-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add spring messaging azure eh sample
- Loading branch information
Showing
11 changed files
with
467 additions
and
0 deletions.
There are no files selected for viewing
203 changes: 203 additions & 0 deletions
203
eventhubs/spring-messaging-azure-eventhubs/eventhubs-spring-messaging/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
--- | ||
page_type: sample | ||
languages: | ||
- java | ||
products: | ||
- azure-service-bus | ||
name: Sending and Receiving Message by Spring Messaging Azure Event Hubs in Spring Boot Application | ||
description: This sample demonstrates how to send and receive message by Spring Messaging Event Hubs in Spring Boot application. | ||
--- | ||
|
||
# Sending and Receiving Message by Spring Messaging Azure Event Hubs in Spring Boot Application | ||
|
||
This code sample demonstrates how to use Spring Messaging Azure Event Hubs sending and receiving Message. | ||
|
||
## What You Will Build | ||
You will build an application using `EventHubsTemplate` send messages and `@EventHubsListener` receive messages for Azure Event Hubs. | ||
|
||
## What You Need | ||
|
||
- [An Azure subscription](https://azure.microsoft.com/free/) | ||
- [Terraform](https://www.terraform.io/) | ||
- [Azure CLI](https://docs.microsoft.com/cli/azure/install-azure-cli) | ||
- [JDK8](https://www.oracle.com/java/technologies/downloads/) or later | ||
- Maven | ||
- You can also import the code straight into your IDE: | ||
- [IntelliJ IDEA](https://www.jetbrains.com/idea/download) | ||
|
||
## Provision Azure Resources Required to Run This Sample | ||
This sample will create Azure resources using Terraform. If you choose to run it without using Terraform to provision resources, please pay attention to: | ||
> [!IMPORTANT] | ||
> If you choose to use a security principal to authenticate and authorize with Microsoft Entra ID for accessing an Azure resource | ||
> please refer to [Authorize access with Microsoft Entra ID](https://learn.microsoft.com/azure/developer/java/spring-framework/authentication#authorize-access-with-microsoft-entra-id) to make sure the security principal has been granted the sufficient permission to access the Azure resource. | ||
### Authenticate Using the Azure CLI | ||
Terraform must authenticate to Azure to create infrastructure. | ||
|
||
In your terminal, use the Azure CLI tool to setup your account permissions locally. | ||
|
||
```shell | ||
az login | ||
``` | ||
|
||
Your browser window will open and you will be prompted to enter your Azure login credentials. After successful authentication, your terminal will display your subscription information. You do not need to save this output as it is saved in your system for Terraform to use. | ||
|
||
```shell | ||
You have logged in. Now let us find all the subscriptions to which you have access... | ||
|
||
[ | ||
{ | ||
"cloudName": "AzureCloud", | ||
"homeTenantId": "home-Tenant-Id", | ||
"id": "subscription-id", | ||
"isDefault": true, | ||
"managedByTenants": [], | ||
"name": "Subscription-Name", | ||
"state": "Enabled", | ||
"tenantId": "0envbwi39-TenantId", | ||
"user": { | ||
"name": "[email protected]", | ||
"type": "user" | ||
} | ||
} | ||
] | ||
``` | ||
|
||
If you have more than one subscription, specify the subscription-id you want to use with command below: | ||
```shell | ||
az account set --subscription <your-subscription-id> | ||
``` | ||
|
||
### Provision the Resources | ||
|
||
After login Azure CLI with your account, now you can use the terraform script to create Azure Resources. | ||
|
||
#### Run with Bash | ||
|
||
```shell | ||
# In the root directory of the sample | ||
# Initialize your Terraform configuration | ||
terraform -chdir=./terraform init | ||
|
||
# Apply your Terraform Configuration | ||
terraform -chdir=./terraform apply -auto-approve | ||
|
||
``` | ||
|
||
#### Run with Powershell | ||
|
||
```shell | ||
# In the root directory of the sample | ||
# Initialize your Terraform configuration | ||
terraform -chdir=terraform init | ||
|
||
# Apply your Terraform Configuration | ||
terraform -chdir=terraform apply -auto-approve | ||
|
||
``` | ||
|
||
It may take a few minutes to run the script. After successful running, you will see prompt information like below: | ||
|
||
```shell | ||
azurerm_resource_group.main: Creating... | ||
azurerm_resource_group.main: Creation complete after 3s ... | ||
azurerm_storage_account.storage_account: Creating... | ||
azurerm_eventhub_namespace.eventhubs_namespace: Still creating... [10s elapsed] | ||
... | ||
azurerm_storage_account.storage_account: Creation complete after 38s ... | ||
azurerm_storage_container.storage_container: Creating... | ||
azurerm_role_assignment.role_storage_account_contributor: Creating... | ||
azurerm_storage_container.storage_container: Creation complete after 1s ... | ||
azurerm_role_assignment.role_storage_blob_data_owner: Creating... | ||
... | ||
azurerm_role_assignment.role_storage_blob_data_owner: Creation complete after 25s ... | ||
azurerm_role_assignment.role_storage_account_contributor: Creation complete after 29s ... | ||
... | ||
azurerm_eventhub_namespace.eventhubs_namespace: Creation complete after 1m23s ... | ||
azurerm_eventhub.eventhubs: Creating... | ||
azurerm_eventhub.eventhubs: Creation complete after 7s ... | ||
azurerm_role_assignment.role_eventhubs_data_owner: Creating... | ||
... | ||
azurerm_role_assignment.role_eventhubs_data_owner: Creation complete after 24s ... | ||
|
||
Apply complete! Resources: 11 added, 0 changed, 0 destroyed. | ||
|
||
Outputs: | ||
... | ||
|
||
``` | ||
|
||
You can go to [Azure portal](https://ms.portal.azure.com/) in your web browser to check the resources you created. | ||
|
||
### Export Output to Your Local Environment | ||
Running the command below to export environment values: | ||
|
||
#### Run with Bash | ||
|
||
```shell | ||
source ./terraform/setup_env.sh | ||
``` | ||
|
||
#### Run with Powershell | ||
|
||
```shell | ||
terraform\setup_env.ps1 | ||
``` | ||
|
||
If you want to run the sample in debug mode, you can save the output value. | ||
|
||
```shell | ||
AZURE_EVENTHUBS_NAMESPACE=... | ||
AZURE_STORAGE_CONTAINER_NAME=... | ||
AZURE_STORAGE_ACCOUNT_NAME=... | ||
``` | ||
|
||
## Run Locally | ||
|
||
### Run the sample with Maven | ||
|
||
In your terminal, run `mvn clean spring-boot:run`. | ||
|
||
```shell | ||
mvn clean spring-boot:run | ||
``` | ||
|
||
### Run the sample in IDEs | ||
|
||
You can debug your sample by adding the saved output values to the tool's environment variables or the sample's `application.yaml` file. | ||
|
||
* If your tool is `IDEA`, please refer to [Debug your first Java application](https://www.jetbrains.com/help/idea/debugging-your-first-java-application.html) and [add environment variables](https://www.jetbrains.com/help/objc/add-environment-variables-and-program-arguments.html#add-environment-variables). | ||
|
||
* If your tool is `ECLIPSE`, please refer to [Debugging the Eclipse IDE for Java Developers](https://www.eclipse.org/community/eclipse_newsletter/2017/june/article1.php) and [Eclipse Environment Variable Setup](https://examples.javacodegeeks.com/desktop-java/ide/eclipse/eclipse-environment-variable-setup-example/). | ||
|
||
## Verify This Sample | ||
|
||
1. Verify in your app's logs that a similar message was posted: | ||
``` | ||
[ main] c.a.s.s.e.m.EventHubMessagingApplication : Sending a message to the Event Hubs. | ||
... | ||
New message received: Hello world | ||
``` | ||
## Clean Up Resources | ||
After running the sample, if you don't want to run the sample, remember to destroy the Azure resources you created to avoid unnecessary billing. | ||
The terraform destroy command terminates resources managed by your Terraform project. | ||
To destroy the resources you created. | ||
#### Run with Bash | ||
```shell | ||
terraform -chdir=./terraform destroy -auto-approve | ||
``` | ||
|
||
#### Run with Powershell | ||
|
||
```shell | ||
terraform -chdir=terraform destroy -auto-approve | ||
``` | ||
|
||
## Deploy to Azure Spring Apps | ||
|
||
Now that you have the Spring Boot application running locally, it's time to move it to production. [Azure Spring Apps](https://learn.microsoft.com/azure/spring-apps/overview) makes it easy to deploy Spring Boot applications to Azure without any code changes. The service manages the infrastructure of Spring applications so developers can focus on their code. Azure Spring Apps provides lifecycle management using comprehensive monitoring and diagnostics, configuration management, service discovery, CI/CD integration, blue-green deployments, and more. To deploy your application to Azure Spring Apps, see [Deploy your first application to Azure Spring Apps](https://learn.microsoft.com/azure/spring-apps/quickstart?tabs=Azure-CLI). |
35 changes: 35 additions & 0 deletions
35
eventhubs/spring-messaging-azure-eventhubs/eventhubs-spring-messaging/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns="http://maven.apache.org/POM/4.0.0" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>com.azure.spring</groupId> | ||
<artifactId>azure-spring-boot-samples</artifactId> | ||
<version>1.0.0</version> | ||
<relativePath>../../../pom.xml</relativePath> | ||
</parent> | ||
|
||
<artifactId>eventhubs-spring-messaging</artifactId> | ||
<version>1.0.0</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>Communicate to Azure Event Hubs via Spring messaging</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.azure.spring</groupId> | ||
<artifactId>spring-cloud-azure-starter</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.azure.spring</groupId> | ||
<artifactId>spring-messaging-azure-eventhubs</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.azure</groupId> | ||
<artifactId>azure-messaging-eventhubs-checkpointstore-blob</artifactId> | ||
</dependency> | ||
</dependencies> | ||
</project> |
20 changes: 20 additions & 0 deletions
20
...-messaging/src/main/java/com/azure/spring/sample/eventhubs/messaging/ConsumerService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.spring.sample.eventhubs.messaging; | ||
|
||
import com.azure.spring.messaging.eventhubs.implementation.core.annotation.EventHubsListener; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class ConsumerService { | ||
|
||
private static final String EVENT_HUB_NAME = "eh1"; | ||
private static final String CONSUMER_GROUP = "$Default"; | ||
|
||
@EventHubsListener(destination = EVENT_HUB_NAME, group = CONSUMER_GROUP) | ||
public void handleMessageFromEventHub(String message) { | ||
System.out.printf("New message received: %s%n", message); | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
...c/main/java/com/azure/spring/sample/eventhubs/messaging/EventHubMessagingApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.spring.sample.eventhubs.messaging; | ||
|
||
import com.azure.spring.messaging.eventhubs.core.EventHubsTemplate; | ||
import com.azure.spring.messaging.implementation.annotation.EnableAzureMessaging; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.context.ConfigurableApplicationContext; | ||
import org.springframework.messaging.support.MessageBuilder; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
@SpringBootApplication | ||
@EnableAzureMessaging | ||
public class EventHubMessagingApplication { | ||
private static final String EVENT_HUB_NAME = "eh1"; | ||
private static final Logger LOGGER = LoggerFactory.getLogger(EventHubMessagingApplication.class); | ||
|
||
|
||
public static void main(String[] args) throws InterruptedException { | ||
ConfigurableApplicationContext applicationContext = SpringApplication.run(EventHubMessagingApplication.class); | ||
EventHubsTemplate eventHubsTemplate = applicationContext.getBean(EventHubsTemplate.class); | ||
TimeUnit.SECONDS.sleep(10); | ||
LOGGER.info("Sending a message to the Event Hubs."); | ||
eventHubsTemplate.sendAsync(EVENT_HUB_NAME, MessageBuilder.withPayload("Hello world").build()).subscribe(); | ||
|
||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
...-messaging-azure-eventhubs/eventhubs-spring-messaging/src/main/resources/application.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
spring: | ||
cloud: | ||
azure: | ||
eventhubs: | ||
namespace: ${AZURE_EVENTHUBS_NAMESPACE} | ||
processor: | ||
checkpoint-store: | ||
container-name: ${AZURE_STORAGE_CONTAINER_NAME} | ||
account-name: ${AZURE_STORAGE_ACCOUNT_NAME} |
Oops, something went wrong.