Skip to content

Commit

Permalink
Add spring cloud sample to demo how to use messaging annotation with …
Browse files Browse the repository at this point in the history
…Service Bus (#20277)
  • Loading branch information
wujack778 committed Apr 2, 2021
1 parent 601ba46 commit fb04ed2
Show file tree
Hide file tree
Showing 14 changed files with 227 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Spring Cloud Azure Messaging Sample shared library for Java
# Spring Cloud Azure Messaging Event Hubs Sample shared library for Java

## Key concepts

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>azure-spring-cloud-sample-messaging</artifactId>
<artifactId>azure-spring-cloud-sample-messaging-eventhubs</artifactId>
<groupId>com.azure.spring</groupId>
<version>1.0.0</version> <!-- {x-version-update;com.azure.spring:azure-spring-cloud-sample-messaging;current} -->
<name>Azure Spring Cloud Sample Messaging</name>
<version>1.0.0</version> <!-- {x-version-update;com.azure.spring:azure-spring-cloud-sample-messaging-eventhubs;current} -->
<name>Azure Spring Cloud Sample Messaging Event Hubs</name>

<dependencyManagement>
<dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.spring.sample.messaging;
package com.azure.spring.sample.messaging.eventhubs;

import com.azure.spring.messaging.annotation.EnableAzureMessaging;
import org.springframework.boot.SpringApplication;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.spring.sample.messaging;
package com.azure.spring.sample.messaging.eventhubs;

public class User {
private String name;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.spring.sample.messaging;
package com.azure.spring.sample.messaging.eventhubs;

import com.azure.spring.integration.eventhub.api.EventHubOperation;
import com.azure.spring.messaging.annotation.AzureMessageListener;
Expand Down
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
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>
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);
}

}
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));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
spring:
cloud:
azure:
servicebus:
connection-string: [servicebus-namespace-connection-string]
4 changes: 3 additions & 1 deletion sdk/spring/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,9 @@ extends:
groupId: com.azure.spring
- name: feature-management-web-sample
groupId: com.azure.spring
- name: azure-spring-cloud-sample-messaging
- name: azure-spring-cloud-sample-messaging-eventhubs
groupId: com.azure.spring
- name: azure-spring-cloud-sample-messaging-servicebus
groupId: com.azure.spring
- name: azure-spring-cloud-sample-eventhubs-multibinders
groupId: com.azure.spring
Expand Down
3 changes: 2 additions & 1 deletion sdk/spring/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@
<module>azure-spring-boot-samples/feature-management-web-sample</module>
<module>azure-spring-boot-samples/feature-management-sample</module>
<module>azure-spring-boot-samples/azure-spring-cloud-sample-eventhubs-operation</module>
<module>azure-spring-boot-samples/azure-spring-cloud-sample-messaging</module>
<module>azure-spring-boot-samples/azure-spring-cloud-sample-messaging-eventhubs</module>
<module>azure-spring-boot-samples/azure-spring-cloud-sample-messaging-servicebus</module>
<module>azure-spring-boot-samples/azure-spring-cloud-sample-servicebus-operation</module>
<module>azure-spring-boot-samples/azure-spring-cloud-sample-servicebus-queue-binder</module>
<module>azure-spring-boot-samples/azure-spring-cloud-sample-servicebus-queue-multibinders</module>
Expand Down

0 comments on commit fb04ed2

Please sign in to comment.