You can try Spring Cloud Stream in less then 5 min even before you jump into any details and the following three-step guide will help.
We’ll create a simple Spring Cloud Stream application which receives messages coming from the messaging middleware of your choice (more on this later) and logs received messages to the console. We’ll call it LoggingConsumer. While not very practical it will certainly provide a good introduction to some of the main concepts and abstractions, making it easier to digest the rest of this user guide.
So let’s get started. . .
Visit the Spring Initializr. This is where we’ll generate our LoggingConsumer application.
In the Dependencies start typing 'stream' and Cloud Stream option should pop up. Select it. Now start typing either 'kafka' or 'rabbit'. Basically this is where you are choosing what messaging midleware this application will be bound to. Choose the one you have already installed and/or feel more comfortable with installing/running. Also, as you can see from the Initilaizer screen there are few other options you can choose. For example, you can choose Gradle as your build tool instead of the default Maven. With the Dependencies selected the only other thing you have to identify is the application name - logging-consumer. Your configuration screeen should now contain the following:
Dependencies: Cloud Stream, RabbitMQ (or Kafka) Group: com.example - default Artifact: logging-consumer Spring Boot Version: 2.0.0 (or above) - default
Click on Generate Project button. This will donwload the zipped version of the generated project to your hard drive. Unzip it and you’re ready for Step Two.
Here you simply import the project into your IDE of choice.
Please keep in mind that dependening on the IDE you may need to follow a specific import procedures. For example depending on how the project was generated (Maven or Gradle)
you may need to follow specific import procedure (e.g., in Eclipse/STS: File → Import → Maven → Existing Maven Project
).
Ones imported the project must have no errors of any kind and src/main/java
should also contain com.example.loggingconsumer.LoggingConsumerApplication
.
Technically at this point you can just run the application’s main class since it’s already a valid Spring Boot application, but it does not do anything, so let’s add some code.
Modify the com.example.loggingconsumer.LoggingConsumerApplication
to look as follows:
@SpringBootApplication @EnableBinding(Sink.class) public class LoggingConsumerApplication { public static void main(String[] args) { SpringApplication.run(LoggingConsumerApplication.class, args); } @StreamListener(Sink.INPUT) public void handle(Person person) { System.out.println("Received: " + person); } public static class Person { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public String toString() { return this.name; } } }
As you can see from the above:
Sink
binding (input-no-output) via @EnableBinding(Sink.class)
. This will signal to the framework to initiate binding to the messaging middleware where
it will auto-create the destination (i.e., queue, topic) which will be bound to Sink.INPUT
channel.Person
. What this means is that here youcan already observe one of the core features of the framework where
it will attempt to automatically convert incoming message’s payload to type Person
.This is it, we now have a fully functional Spring Cloud Stream application that does something. From here for simplicity we’ll assume RabbitMQ was selected in step one.
Assuming you have RabbitMQ installed and running, start the application by simply running its main
method.
You should see following output:
--- [ main] c.s.b.r.p.RabbitExchangeQueueProvisioner : declaring queue for inbound: input.anonymous.CbMIwdkJSBO1ZoPDOtHtCg, bound to: input --- [ main] o.s.a.r.c.CachingConnectionFactory : Attempting to connect to: [localhost:5672] --- [ main] o.s.a.r.c.CachingConnectionFactory : Created new connection: rabbitConnectionFactory#2a3a299:0/SimpleConnection@66c83fc8. . . . . . --- [ main] o.s.i.a.i.AmqpInboundChannelAdapter : started inbound.input.anonymous.CbMIwdkJSBO1ZoPDOtHtCg . . . --- [ main] c.e.l.LoggingConsumerApplication : Started LoggingConsumerApplication in 2.531 seconds (JVM running for 2.897)
Go to RabbitMQ management console or any other RabbitMQ client and simply send message to input.anonymous.CbMIwdkJSBO1ZoPDOtHtCg
(NOTE: the anonymous.CbMIwdkJSBO1ZoPDOtHtCg
part represents the group name and is generated and will be different in your environment. For something more
predictable you can use explicit group name via spring.cloud.stream.bindings.input.group=hello
).
The contents of the message should be JSON representation of Person
class, so let’s send this:
{"name":"Turd Ferguson"}
And in your console you should see:
Received: Turd Ferguson
You can also build/package your application into a boot jar (i.e., ./mvnw clean install
) and run the built JAR using java -jar
command.
That is all!