This sample application demonstrates a Kafka binder based Spring Cloud Stream application running as a native image.
The application consists of a supplier that is generating a random string every second, a function that receives the generated string and uppercase it, and then finally a consumer that simply logs the uppercased function.
The application comes with a standard configuration properties yaml file that activates all the functions in the application.
It then specifies the necessary destinations for all the bindings.
Some bindings rely on the default binding destinations (see the application.yml
for details).
In addition, the application expects Kafka to be available on localhost:9092
.
If that is not the case, please ensure to update that in application.yml
.
Before we build and run the sample app as a native image, let us do a few preliminary verifications.
Ensure that, you are using a graalVM compatible JVM. For example, when running this app, we used the following JVM.
java -version
openjdk version "20.0.2" 2023-07-18
OpenJDK Runtime Environment GraalVM CE 20.0.2+9.1 (build 20.0.2+9-jvmci-23.0-b15)
OpenJDK 64-Bit Server VM GraalVM CE 20.0.2+9.1 (build 20.0.2+9-jvmci-23.0-b15, mixed mode, sharing)
First, let us run this application in regular JVM mode without generating any AOT or native specific information.
Make sure that you are in the directory of this sample app: samples/kafka-binder-native-app
./mvnw clean package
and then:
java -jar target/kafka-binder-native-app-0.0.1-SNAPSHOT.jar
You should see output similar to the following:
++++++Received:MUCH
++++++Received:WOOD
++++++Received:COULD
++++++Received:A
++++++Received:WOODCHUCK
Next, let us run the same app in AOT mode.
./mvnw clean package -Pnative
and then:
java -Dspring.aot.enabled=true -jar target/kafka-binder-native-app-0.0.1-SNAPSHOT.jar
You should see the similar output as above.
Now that we verified that our app is working in regular and AOT mode, let us now take it to run it as a native executable.
First, build the native image:
./mvnw -Pnative native:compile
Then, run the generated executable:
./target/kafka-binder-native-app
You should see output similar to what we got above.