Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send and receive messages to a local Azure Service Bus Emulator #504

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ max_line_length = 1000
[{*.md,*.asciidoc}]
max_line_length = 1000

[{*.yaml,*.yml}]
[{*.json,*.yaml,*.yml}]
indent_size = 2
58 changes: 58 additions & 0 deletions servicebus/emulator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

# Send and receive a message to a local Azure Service Bus Emulator

## Prerequisites

This example assumes you have Docker installed as it uses the Azure Service Bus Emulator Docker image.

## Start the Azure Service Bus Emulator

Make sure Docker Engine is operational in the background. Then, start the Azure Service Bus Emulator using the following command:

```shell
docker compose -f src/main/docker/docker-compose.yaml up -d
```

## Send and receive messages to an Azure Service Bus Emulator

First, create the environment variables used to connect to our Azure Service Bus Emulator
using the command line below:


```shell
export SERVICE_BUS_QUEUE_CONNECTION_STRING=Endpoint=sb://127.0.0.1;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=SAS_KEY_VALUE;UseDevelopmentEmulator=true;
export SERVICE_BUS_QUEUE=queue.1
```

Then compile the code:

```shell
mvn clean compile
```

Listen to the incoming messages with the following command:

```shell
mvn exec:java -Preceive
```

And then send messages:

```shell
mvn exec:java -Psend
```

## Cleanup

```shell
docker compose -f src/main/docker/docker-compose.yaml down
```

## References

* [Overview of the Azure Service Bus emulator](https://learn.microsoft.com/en-us/azure/service-bus-messaging/overview-emulator)
* [Test locally by using the Azure Service Bus emulator](https://learn.microsoft.com/en-us/azure/service-bus-messaging/test-locally-with-service-bus-emulator?tabs=docker-linux-container)
* [Azure Service Bus emulator config file](https://github.com/Azure/azure-service-bus-emulator-installer/blob/main/ServiceBus-Emulator/Config/Config.json)
* [Simple Azure Service Bus Emulator – Finally Here!](https://devopsifyme.com/simple-azure-service-bus-emulator-finally-here)
* [Azure SQL Edge Docker image](https://hub.docker.com/r/microsoft/azure-sql-edge)
* [Emulator connection string support](https://github.com/Azure/azure-sdk-for-java/issues/38735)
74 changes: 74 additions & 0 deletions servicebus/emulator/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>examples.azure.servicebus</groupId>
<artifactId>project</artifactId>
<version>1-SNAPSHOT</version>
</parent>
<artifactId>servicebus-emulator</artifactId>
<packaging>jar</packaging>
<name>Azure Service Bus :: Send and receive message using the Azure Service Bus Emulator</name>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-sdk-bom</artifactId>
<version>${azure.bom.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-messaging-servicebus</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.30</version>
</dependency>
</dependencies>

<build>
<finalName>emulator</finalName>
</build>
<profiles>
<profile>
<id>receive</id>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.5.0</version>
<configuration>
<mainClass>examples.azure.servicebus.emulator.ReceiveMessage</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>send</id>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.5.0</version>
<configuration>
<mainClass>examples.azure.servicebus.emulator.SendMessage</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>

</project>
2 changes: 2 additions & 0 deletions servicebus/emulator/src/main/docker/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ACCEPT_EULA=Y
MSSQL_SA_PASSWORD=myadmin!psswd246
28 changes: 28 additions & 0 deletions servicebus/emulator/src/main/docker/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"UserConfig": {
"Namespaces": [
{
"Name": "sbemulatorns",
"Queues": [
{
"Name": "queue.1",
"Properties": {
"DeadLetteringOnMessageExpiration": false,
"DefaultMessageTimeToLive": "PT1H",
"DuplicateDetectionHistoryTimeWindow": "PT20S",
"ForwardDeadLetteredMessagesTo": "",
"ForwardTo": "",
"LockDuration": "PT1M",
"MaxDeliveryCount": 10,
"RequiresDuplicateDetection": false,
"RequiresSession": false
}
}
]
}
],
"Logging": {
"Type": "File"
}
}
}
32 changes: 32 additions & 0 deletions servicebus/emulator/src/main/docker/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: microsoft-azure-servicebus-emulator
services:
emulator:
container_name: "servicebus-emulator"
image: mcr.microsoft.com/azure-messaging/servicebus-emulator:latest
pull_policy: always
volumes:
- "./config.json:/ServiceBus_Emulator/ConfigFiles/Config.json"
ports:
- "5672:5672"
environment:
SQL_SERVER: sqledge
MSSQL_SA_PASSWORD: ${MSSQL_SA_PASSWORD}
ACCEPT_EULA: ${ACCEPT_EULA}
depends_on:
- sqledge
networks:
sb-emulator:
aliases:
- "sb-emulator"
sqledge:
container_name: "sqledge"
image: "mcr.microsoft.com/azure-sql-edge:latest"
networks:
sb-emulator:
aliases:
- "sqledge"
environment:
ACCEPT_EULA: ${ACCEPT_EULA}
MSSQL_SA_PASSWORD: ${MSSQL_SA_PASSWORD}
networks:
sb-emulator:
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package examples.azure.servicebus.emulator;

import com.azure.messaging.servicebus.ServiceBusClientBuilder;
import com.azure.messaging.servicebus.ServiceBusProcessorClient;
import com.azure.messaging.servicebus.ServiceBusReceivedMessage;

import java.util.concurrent.TimeUnit;

public class ReceiveMessage {

private static final String SERVICE_BUS_QUEUE_CONNECTION_STRING = System.getenv("SERVICE_BUS_QUEUE_CONNECTION_STRING");
private static final String SERVICE_BUS_QUEUE = System.getenv("SERVICE_BUS_QUEUE");

private ServiceBusReceivedMessage receivedMessage;

private boolean processed;

public void execute() throws Exception {
ServiceBusProcessorClient client = new ServiceBusClientBuilder()
.connectionString(SERVICE_BUS_QUEUE_CONNECTION_STRING)
.processor()
.queueName(SERVICE_BUS_QUEUE)
.processMessage((context) -> {
receivedMessage = context.getMessage();
processed = true;
})
.processError((context) -> {
System.err.println(context.getException());
processed = true;
})
.buildProcessorClient();

client.start();
while (!processed) {
TimeUnit.SECONDS.sleep(1);
}
System.out.println("Received a " + receivedMessage.getBody().toString());
client.close();
}

public static void main(String[] arguments) throws Exception {
ReceiveMessage receiveMessage = new ReceiveMessage();
receiveMessage.execute();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package examples.azure.servicebus.emulator;

import com.azure.messaging.servicebus.ServiceBusClientBuilder;
import com.azure.messaging.servicebus.ServiceBusMessage;
import com.azure.messaging.servicebus.ServiceBusSenderClient;

import java.time.LocalDateTime;

public class SendMessage {

private static final String SERVICE_BUS_QUEUE_CONNECTION_STRING = System.getenv("SERVICE_BUS_QUEUE_CONNECTION_STRING");
private static final String SERVICE_BUS_QUEUE = System.getenv("SERVICE_BUS_QUEUE");

public static void main(String[] arguments) {
ServiceBusSenderClient senderClient = new ServiceBusClientBuilder()
.connectionString(SERVICE_BUS_QUEUE_CONNECTION_STRING)
.sender()
.queueName(SERVICE_BUS_QUEUE)
.buildClient();

System.out.println("Sending....");
senderClient.sendMessage(new ServiceBusMessage("Hello, World! at " + LocalDateTime.now()));
System.out.println("Sent a 'Hello, World!' message to " + SERVICE_BUS_QUEUE);

System.exit(0);
}
}
1 change: 1 addition & 0 deletions servicebus/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<modules>
<module>create</module>
<module>create-queue</module>
<module>emulator</module>
<module>jms-to-cosmosdb-springboot-1</module>
<module>jms-to-cosmosdb-springboot-2</module>
<module>jms-to-eventhubs-springboot</module>
Expand Down
Loading