Skip to content
This repository was archived by the owner on Feb 23, 2023. It is now read-only.

Commit 6ff3417

Browse files
sobychackosdeleuze
authored andcommitted
New sample for Spring Cloud Stream RabbitMQ Binder
Adding the necessary Rabbit binder hints
1 parent 5c85a7b commit 6ff3417

File tree

11 files changed

+233
-0
lines changed

11 files changed

+233
-0
lines changed
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
HELP.md
2+
target/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
!**/src/main/**/target/
5+
!**/src/test/**/target/
6+
7+
### STS ###
8+
.apt_generated
9+
.classpath
10+
.factorypath
11+
.project
12+
.settings
13+
.springBeans
14+
.sts4-cache
15+
16+
### IntelliJ IDEA ###
17+
.idea
18+
*.iws
19+
*.iml
20+
*.ipr
21+
22+
### NetBeans ###
23+
/nbproject/private/
24+
/nbbuild/
25+
/dist/
26+
/nbdist/
27+
/.nb-gradle/
28+
build/
29+
!**/src/main/**/build/
30+
!**/src/test/**/build/
31+
32+
### VS Code ###
33+
.vscode/

samples/cloud-stream-rabbit/README.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
## Spring Cloud Stream Rabbit Binder Native Sample App
2+
3+
To build and run the native application on the JVM:
4+
5+
```
6+
mvn clean spring-boot:build-image
7+
docker-compose up
8+
java -jar target/cloud-stream-rabbit-0.0.1-SNAPSHOT.jar
9+
```
10+
11+
To build the application as a native image:
12+
13+
```
14+
mvn -Pnative -DskipTests package
15+
docker-compose up
16+
./target/cloud-stream-rabbit
17+
```
18+
19+
As an alternative, you can use `build.sh` script that is provided as part of this sample application directory.
20+
This script will start RabbitMQ in a docker container, build the image, run the test that verifies the sample works as expected and then stop the docker container.

samples/cloud-stream-rabbit/build.sh

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bash
2+
3+
RC=0
4+
5+
docker-compose up -d rabbitmq
6+
${PWD%/*samples/*}/scripts/compileWithMaven.sh && ${PWD%/*samples/*}/scripts/test.sh || RC=$?
7+
docker-compose down
8+
9+
exit $RC
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
version: '3'
2+
services:
3+
rabbitmq:
4+
image: rabbitmq
5+
ports:
6+
- 5672:5672

samples/cloud-stream-rabbit/pom.xml

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>org.springframework.experimental</groupId>
8+
<artifactId>spring-native-sample-parent</artifactId>
9+
<version>0.10.6-SNAPSHOT</version>
10+
<relativePath>../maven-parent/pom.xml</relativePath>
11+
</parent>
12+
<groupId>com.example</groupId>
13+
<artifactId>cloud-stream-rabbit</artifactId>
14+
<version>0.0.1-SNAPSHOT</version>
15+
<name>rabbit</name>
16+
<description>GraalVM Spring Cloud Stream Rabbit Binder Demo</description>
17+
18+
<properties>
19+
<java.version>1.8</java.version>
20+
<main.class>com.example.demo.SpringCloudStreamRabbitApplication</main.class>
21+
</properties>
22+
23+
<dependencies>
24+
<dependency>
25+
<groupId>org.springframework.experimental</groupId>
26+
<artifactId>spring-native</artifactId>
27+
</dependency>
28+
<dependency>
29+
<groupId>com.fasterxml.jackson.core</groupId>
30+
<artifactId>jackson-databind</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter</artifactId>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.springframework.cloud</groupId>
38+
<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
39+
<version>3.1.6-SNAPSHOT</version>
40+
</dependency>
41+
</dependencies>
42+
43+
<build>
44+
<plugins>
45+
<plugin>
46+
<groupId>org.springframework.experimental</groupId>
47+
<artifactId>spring-aot-maven-plugin</artifactId>
48+
<configuration>
49+
<removeYamlSupport>true</removeYamlSupport>
50+
</configuration>
51+
</plugin>
52+
<plugin>
53+
<groupId>org.springframework.boot</groupId>
54+
<artifactId>spring-boot-maven-plugin</artifactId>
55+
</plugin>
56+
<plugin>
57+
<groupId>org.apache.maven.plugins</groupId>
58+
<artifactId>maven-compiler-plugin</artifactId>
59+
<configuration>
60+
<source>8</source>
61+
<target>8</target>
62+
</configuration>
63+
</plugin>
64+
</plugins>
65+
</build>
66+
67+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.example.demo;
2+
3+
import java.util.Random;
4+
import java.util.function.Consumer;
5+
import java.util.function.Function;
6+
import java.util.function.Supplier;
7+
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.boot.SpringApplication;
10+
import org.springframework.boot.autoconfigure.SpringBootApplication;
11+
import org.springframework.cloud.stream.function.StreamBridge;
12+
import org.springframework.context.annotation.Bean;
13+
14+
@SpringBootApplication
15+
public class SpringCloudStreamRabbitApplication {
16+
17+
@Autowired
18+
StreamBridge streamBridge;
19+
20+
public static void main(String[] args) {
21+
SpringApplication.run(SpringCloudStreamRabbitApplication.class, args);
22+
}
23+
24+
@Bean
25+
public Function<String, String> graalUppercaseFunction() {
26+
return String::toUpperCase;
27+
}
28+
29+
@Bean
30+
public Consumer<String> graalLoggingConsumer() {
31+
return s -> {
32+
System.out.println("++++++Received:" + s);
33+
// Verifying that StreamBridge API works in native applications.
34+
streamBridge.send("sb-out", s);
35+
};
36+
}
37+
38+
@Bean
39+
public Supplier<String> graalSupplier() {
40+
return () -> {
41+
String woodchuck = "How much wood a woodchuck chuck if a woodchuck could chuck wood?";
42+
final String[] splitWoodchuck = woodchuck.split(" ");
43+
Random random = new Random();
44+
return splitWoodchuck[random.nextInt(splitWoodchuck.length)];
45+
};
46+
}
47+
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
spring.cloud.function.definition=graalSupplier;graalUppercaseFunction;graalLoggingConsumer
2+
3+
spring.cloud.stream.bindings.graalLoggingConsumer-in-0.destination=graalUppercaseFunction-out-0
4+
5+
spring.cloud.stream.bindings.graalSupplier-out-0.destination=graalUppercaseFunction-in-0

samples/cloud-stream-rabbit/verify.sh

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env bash
2+
sleep 2
3+
if [[ `cat target/native/test-output.txt | grep -E "Received:(HOW|MUCH|WOOD|A|WOODCHUCK|CHUCK|IF|COULD|WOULD?)"` ]]; then
4+
exit 0
5+
else
6+
exit 1
7+
fi

spring-native-configuration/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@
6767
<artifactId>spring-cloud-stream-binder-kafka</artifactId>
6868
<scope>provided</scope>
6969
</dependency>
70+
<dependency>
71+
<groupId>org.springframework.cloud</groupId>
72+
<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
73+
<scope>provided</scope>
74+
</dependency>
7075
<dependency>
7176
<groupId>org.springframework.batch</groupId>
7277
<artifactId>spring-batch-core</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2019-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.stream.rabbit;
18+
19+
import org.springframework.cloud.stream.binder.rabbit.config.RabbitServiceAutoConfiguration;
20+
import org.springframework.cloud.stream.function.FunctionConfiguration;
21+
import org.springframework.nativex.hint.NativeHint;
22+
import org.springframework.nativex.hint.TypeHint;
23+
import org.springframework.nativex.type.NativeConfiguration;
24+
25+
@NativeHint(trigger = FunctionConfiguration.class,
26+
types = {
27+
@TypeHint(types = {
28+
RabbitServiceAutoConfiguration.class})
29+
})
30+
31+
public class RabbitBinderHints implements NativeConfiguration {
32+
}

spring-native-configuration/src/main/resources/META-INF/services/org.springframework.nativex.type.NativeConfiguration

+1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ org.springframework.cloud.loadbalancer.LoadBalancerHints
9999
org.springframework.cloud.netflix.eureka.EurekaClientHints
100100
org.springframework.cloud.sleuth.TraceHints
101101
org.springframework.cloud.stream.kafka.KafkaBinderHints
102+
org.springframework.cloud.stream.rabbit.RabbitBinderHints
102103
org.springframework.cloud.task.CloudTaskHints
103104
org.springframework.context.annotation.ContextAnnotationHints
104105
org.springframework.core.annotation.CoreAnnotationHints

0 commit comments

Comments
 (0)