Skip to content

Commit

Permalink
Add spring-cloud-azure-eventhubs example
Browse files Browse the repository at this point in the history
  • Loading branch information
eddumelendez committed Aug 27, 2024
1 parent b73eff4 commit 5088fac
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 0 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<module>spring-cloud-vault</module>
<module>spring-cloud-zookeeper</module>
<module>spring-cloud-azure-storage</module>
<module>spring-cloud-azure-eventhubs</module>
<module>spring-boot-r2dbc-postgresql-chaos</module>
<module>spring-boot-rabbitmq</module>
</modules>
Expand Down
92 changes: 92 additions & 0 deletions spring-cloud-azure-eventhubs/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?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>3.1.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>spring-cloud-azure-eventhubs</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-cloud-azure-eventhubs</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>21</java.version>
<spring-cloud-azure.version>5.15.0</spring-cloud-azure.version>
<testcontainers.version>1.20.1</testcontainers.version>
</properties>
<dependencies>
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>spring-cloud-azure-starter-integration-eventhubs</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers-bom</artifactId>
<version>${testcontainers.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>spring-cloud-azure-dependencies</artifactId>
<version>${spring-cloud-azure.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.springcloudazureeventhubs;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringCloudAzureEventHubsApplication {

public static void main(String[] args) {
SpringApplication.run(SpringCloudAzureEventHubsApplication.class, args);
}

}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.example.springcloudazureeventhubs;

import com.azure.core.util.IterableStream;
import com.azure.messaging.eventhubs.EventData;
import com.azure.messaging.eventhubs.EventHubConsumerClient;
import com.azure.messaging.eventhubs.EventHubProducerClient;
import com.azure.messaging.eventhubs.models.EventPosition;
import com.azure.messaging.eventhubs.models.PartitionEvent;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.Network;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.MountableFile;

import java.time.Duration;
import java.util.Iterator;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.waitAtMost;

@SpringBootTest(properties = { "spring.cloud.azure.eventhubs.event-hub-name=eh1",
"spring.cloud.azure.eventhubs.consumer.consumer-group=$default" })
@Testcontainers
class SpringCloudAzureEventHubsApplicationTests {

private static final Network network = Network.newNetwork();

private static final int AZURE_STORAGE_BLOB_PORT = 10000;

private static final int AZURE_STORAGE_QUEUE_PORT = 10001;

private static final int AZURE_STORAGE_TABLE_PORT = 10002;

private static final int AZURE_EVENTHUBS_BLOB_PORT = 5672;

@Container
private static final GenericContainer<?> azurite = new GenericContainer<>(
"mcr.microsoft.com/azure-storage/azurite:latest")
.withExposedPorts(AZURE_STORAGE_BLOB_PORT, AZURE_STORAGE_QUEUE_PORT, AZURE_STORAGE_TABLE_PORT)
.withNetwork(network)
.withNetworkAliases("azurite");

@Container
private static final GenericContainer<?> eventHubs = new GenericContainer<>(
"mcr.microsoft.com/azure-messaging/eventhubs-emulator:latest")
.withExposedPorts(AZURE_EVENTHUBS_BLOB_PORT)
.withCopyFileToContainer(MountableFile.forClasspathResource("Config.json"),
"/Eventhubs_Emulator/ConfigFiles/Config.json")
.waitingFor(Wait.forLogMessage(".*Emulator Service is Successfully Up!.*", 1))
.withNetwork(network)
.withEnv("BLOB_SERVER", "azurite")
.withEnv("METADATA_SERVER", "azurite")
.withEnv("ACCEPT_EULA", "Y");

@Autowired
private EventHubProducerClient producerClient;

@Autowired
private EventHubConsumerClient consumerClient;

@DynamicPropertySource
static void properties(DynamicPropertyRegistry registry) {
var eventHubsHost = eventHubs.getHost();
var eventHubsMappedPort = eventHubs.getMappedPort(AZURE_EVENTHUBS_BLOB_PORT);
var connectionString = "Endpoint=sb://%s:%d;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=SAS_KEY_VALUE;UseDevelopmentEmulator=true;"
.formatted(eventHubsHost, eventHubsMappedPort);
registry.add("spring.cloud.azure.eventhubs.connection-string", () -> connectionString);
}

@Test
void contextLoads() {
this.producerClient.send(List.of(new EventData("test message")));

waitAtMost(Duration.ofSeconds(30)).pollDelay(Duration.ofSeconds(5)).untilAsserted(() -> {
IterableStream<PartitionEvent> events = this.consumerClient.receiveFromPartition("0", 1,
EventPosition.earliest(), Duration.ofSeconds(2));
Iterator<PartitionEvent> iterator = events.stream().iterator();
assertThat(iterator.hasNext()).isTrue();
assertThat(iterator.next().getData().getBodyAsString()).isEqualTo("test message");
});
}

}
24 changes: 24 additions & 0 deletions spring-cloud-azure-eventhubs/src/test/resources/Config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"UserConfig": {
"NamespaceConfig": [
{
"Type": "EventHub",
"Name": "emulatorNs1",
"Entities": [
{
"Name": "eh1",
"PartitionCount": "2",
"ConsumerGroups": [
{
"Name": "cg1"
}
]
}
]
}
],
"LoggingConfig": {
"Type": "File"
}
}
}
Empty file.
18 changes: 18 additions & 0 deletions spring-cloud-azure-eventhubs/src/test/resources/logback-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<configuration>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} %-5level %logger - %msg%n</pattern>
</encoder>
</appender>

<root level="INFO">
<appender-ref ref="STDOUT"/>
</root>

<logger name="org.testcontainers" level="INFO"/>
<logger name="com.github.dockerjava" level="WARN"/>

</configuration>

0 comments on commit 5088fac

Please sign in to comment.