-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add spring cloud sample to demo how to use messaging annotation with …
…Service Bus (#20277)
- Loading branch information
Showing
14 changed files
with
227 additions
and
9 deletions.
There are no files selected for viewing
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...e-spring-cloud-sample-messaging/README.md → ...loud-sample-messaging-eventhubs/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
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
2 changes: 1 addition & 1 deletion
2
...ample/messaging/MessagingApplication.java → ...aging/eventhubs/MessagingApplication.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
2 changes: 1 addition & 1 deletion
2
...m/azure/spring/sample/messaging/User.java → ...ring/sample/messaging/eventhubs/User.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
2 changes: 1 addition & 1 deletion
2
...pring/sample/messaging/WebController.java → ...le/messaging/eventhubs/WebController.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
File renamed without changes.
77 changes: 77 additions & 0 deletions
77
...re-spring-boot-samples/azure-spring-cloud-sample-messaging-servicebus/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,77 @@ | ||
# Spring Cloud Azure Messaging Service Bus Sample shared library for Java | ||
|
||
## Key concepts | ||
|
||
This code sample demonstrates how to use [AzureMessageListener.java][annotation-azure-message-listener] to listen to messages from Service Bus Topic. | ||
|
||
## Getting started | ||
|
||
Running this sample will be charged by Azure. You can check the usage and bill at | ||
[this link][azure-account]. | ||
|
||
### Prerequisites | ||
- [Environment checklist][environment_checklist] | ||
|
||
### Create Azure resources | ||
|
||
1. Create [Azure Service Bus Namespace][create-service-bus-namespace]. | ||
Please note `Basic` tier is unsupported. | ||
|
||
1. Create [Azure Service Bus Topic][create-service-bus-topic] and named `topic`. After creating the Azure Service Bus Topic, | ||
you can create the subscription [Azure Service Bus Topic subscription][create-subscription] to the topic and named `sub` . | ||
|
||
|
||
### Include the package | ||
Because dependency `azure-spring-cloud-starter-servicebus` does not introduce the dependency about messaging, we need to add | ||
dependency `azure-spring-cloud-messaging`. | ||
|
||
[//]: # ({x-version-update-start;com.azure.spring:azure-spring-cloud-messaging;current}) | ||
```xml | ||
<dependency> | ||
<groupId>com.azure.spring</groupId> | ||
<artifactId>azure-spring-cloud-messaging</artifactId> | ||
<version>3.0.0</version> | ||
</dependency> | ||
``` | ||
[//]: # ({x-version-update-end}) | ||
|
||
## Examples | ||
|
||
1. Update [application.yaml][application.yaml]. | ||
```yaml | ||
spring: | ||
cloud: | ||
azure: | ||
servicebus: | ||
connection-string: [servicebus-namespace-connection-string] | ||
``` | ||
1. Run the `mvn spring-boot:run` in the root of the code sample to get the app running. | ||
|
||
1. Send a POST request | ||
|
||
$ curl -X POST http://localhost:8080/messages?message=hello | ||
|
||
1. Verify in your app’s logs that a similar message was posted: | ||
|
||
New service bus topic message received: 'hello' | ||
|
||
1. Delete the resources on [Azure Portal][azure-portal] to avoid unexpected charges. | ||
|
||
## Troubleshooting | ||
|
||
## Next steps | ||
|
||
## Contributing | ||
|
||
|
||
<!-- LINKS --> | ||
|
||
[azure-account]: https://azure.microsoft.com/account/ | ||
[azure-portal]: https://ms.portal.azure.com/ | ||
[create-service-bus-namespace]: https://docs.microsoft.com/azure/service-bus-messaging/service-bus-quickstart-topics-subscriptions-portal#create-a-namespace-in-the-azure-portal | ||
[create-service-bus-topic]: https://docs.microsoft.com/azure/service-bus-messaging/service-bus-quickstart-topics-subscriptions-portal#create-a-topic-using-the-azure-portal | ||
[create-subscription]: https://docs.microsoft.com/azure/service-bus-messaging/service-bus-quickstart-topics-subscriptions-portal#create-subscriptions-to-the-topic | ||
[annotation-azure-message-listener]: https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/spring/azure-spring-cloud-messaging/src/main/java/com/azure/spring/messaging/annotation/AzureMessageListener.java | ||
[environment_checklist]: https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/spring/ENVIRONMENT_CHECKLIST.md#ready-to-run-checklist | ||
[application.yaml]: https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/spring/azure-spring-boot-samples/azure-spring-cloud-sample-servicebus-operation/src/main/resources/application.yaml |
81 changes: 81 additions & 0 deletions
81
sdk/spring/azure-spring-boot-samples/azure-spring-cloud-sample-messaging-servicebus/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,81 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>2.4.3</version> <!-- {x-version-update;org.springframework.boot:spring-boot-starter-parent;external_dependency} --> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
<groupId>com.azure.spring</groupId> | ||
<artifactId>azure-spring-cloud-sample-messaging-servicebus</artifactId> | ||
<version>1.0.0</version> <!-- {x-version-update;com.azure.spring:azure-spring-cloud-sample-messaging-servicebus;current} --> | ||
<name>ServiceBusMessaging</name> | ||
<description>Azure Spring Cloud Sample Messaging Service Bus</description> | ||
<properties> | ||
<java.version>1.8</java.version> | ||
</properties> | ||
<dependencies> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.azure.spring</groupId> | ||
<artifactId>azure-spring-cloud-starter-servicebus</artifactId> | ||
<version>2.4.0-beta.1</version> <!-- {x-version-update;com.azure.spring:azure-spring-cloud-starter-servicebus;current} --> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.azure.spring</groupId> | ||
<artifactId>azure-spring-cloud-messaging</artifactId> | ||
<version>2.4.0-beta.1</version> <!-- {x-version-update;com.azure.spring:azure-spring-cloud-messaging;current} --> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-logging</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-core</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.powermock</groupId> | ||
<artifactId>powermock-api-mockito2</artifactId> | ||
<version>2.0.2</version> <!-- {x-version-update;org.powermock:powermock-api-mockito2;external_dependency} --> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.powermock</groupId> | ||
<artifactId>powermock-module-junit4</artifactId> | ||
<version>2.0.2</version> <!-- {x-version-update;org.powermock:powermock-module-junit4;external_dependency} --> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.hibernate.validator</groupId> | ||
<artifactId>hibernate-validator</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
18 changes: 18 additions & 0 deletions
18
...ain/java/com/azure/spring/sample/messaging/servicebus/ServiceBusMessagingApplication.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,18 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.spring.sample.messaging.servicebus; | ||
|
||
import com.azure.spring.messaging.annotation.EnableAzureMessaging; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
@EnableAzureMessaging | ||
public class ServiceBusMessagingApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(ServiceBusMessagingApplication.class, args); | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
...ervicebus/src/main/java/com/azure/spring/sample/messaging/servicebus/TopicController.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.messaging.servicebus; | ||
|
||
import com.azure.spring.integration.servicebus.topic.ServiceBusTopicOperation; | ||
import com.azure.spring.messaging.annotation.AzureMessageListener; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.integration.support.MessageBuilder; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
public class TopicController { | ||
|
||
private static final String TOPIC_NAME = "topic"; | ||
private static final String SUBSCRIPTION_NAME = "sub"; | ||
|
||
@Autowired | ||
ServiceBusTopicOperation topicOperation; | ||
|
||
@PostMapping("/messages") | ||
public String send(@RequestParam("message") String message) { | ||
this.topicOperation.sendAsync(TOPIC_NAME, MessageBuilder.withPayload(message).build()); | ||
return message; | ||
} | ||
|
||
@AzureMessageListener(destination = TOPIC_NAME,group = SUBSCRIPTION_NAME) | ||
public void handleMessage(String message) { | ||
System.out.println(String.format("New service bus topic message received: '%s'", message)); | ||
} | ||
|
||
} |
5 changes: 5 additions & 0 deletions
5
...amples/azure-spring-cloud-sample-messaging-servicebus/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,5 @@ | ||
spring: | ||
cloud: | ||
azure: | ||
servicebus: | ||
connection-string: [servicebus-namespace-connection-string] |
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
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