Using Java & Spring Cloud Streams (SCS) to create Event-Driven Applications with PubSub+

story_section1

story_section2

Developer IDE & Code Access

IDE Setup

The recommended IDE for the workshop is Spring Tools Suite (STS) Download Here. STS comes with some niceties, such as autodeploy, when used with spring-boot-devtools. Participants can of course use another IDE if preferred. It is also recommended that you begin the workshop with an empty STS workspace to avoid any unforeseen issues with existing projects/configurations/code.

Required libraries:

Code Access

$ git clone -b master https://github.com/SolaceTraining/solace-workshop-scs.git
$ git clone -b master git@github.com:SolaceTraining/solace-workshop-scs.git

After importing everything you should see the following projects in STS:

$ cd ~/git/solace-workshop-scs/01-scs-workshop-common/
$ mvn clean install

Create and/or Verify access to a Solace PubSub+ Service

PubSub+ Service in Solace Cloud

If you want to stand up your Solace PubSub+ Service in Solace Cloud go ahead and login or signup at the Cloud Signup Page. Note that a free tier is available and will work for this workshop.

solace_credentails

Local Solace PubSub+ Instance

When developing your application, you may want to test using a local instance of the Solace PubSub+ Event Broker. Refer to the Solace Docker Getting Started Guide to get you up and running quickly with a broker instance running in Docker. You may skip this step if you decide to use a broker running elsewhere, such as in Solace Cloud.

Other Solace PubSub+ Service Instance

If using another PubSub+ Service Instance either get the connection information from your administrator or if you have management access it can be retrieved from the PubSub+ manager.

Multiple Codelabers

If more than one person is doing this codelab using a shared PubSub+ instance you'll need to replace ATTENDEE with a unique identifier to not receive duplicate events. As we walk through the codelab there are reminders to do so, but if you prefer to knock that out in the beginning here are a few quick commands to help you do that if running on Mac/Linux.

BE SURE TO REPLACE UNIQUE_NAME WITH A UNIQUE IDENTIFIER FOR YOURSELF!

cd /path/to/solace-workshop-scs
find . -name application.yml -exec sed -i '' 's/ATTENDEE/UNIQUE_NAME/g' {} \;
find . -name ScSProcessorFeaturesDynamic.java -exec sed -i '' 's/ATTENDEE/UNIQUE_NAME/g' {} \;

story_section3

Application Architecture

At the end of this section we will have created the apps below! The Source will send out tweets that will be received by the marketing Sink.

1 Application Architecture

Creating a Source

Before our company can do anything with the tweets we have to start to receive an incoming stream of them. Let's get started! Please navigate to the "02-scs-source-tweets" project in your IDE.

Learn the Project Structure

Before we take a look at the code, let's take a quick look at the structure of a Spring Cloud Streams project.

SCS Project Structure

SCS Maven Dependencies

SCS Sample Implementation

SCS Application Config File

Run our 02-scs-source-tweets app

$ mvn clean install
$ mvn spring-boot:run

story_section3_g2

To do this we will create a sink app. Recall that a sink app binds to an INPUT channel.

Creating a Sink

story_section5_g1

Application Architecture

At the end of this section we will have added the Factory Tweet Board Sink.

2 Application Architecture

Creating the Tweet Board Sink

We obviously don't have a giant LED board that we can use so we're going to settle for logging the tweets as they come in.

Run the Tweet Board

At this point we have created our "04-scs-sink-tweetboard" application and it's ready for action!

$ mvn clean install
$ mvn spring-boot:run

story_section5_g2

So far in this workshop we have created source or sink applications. In this section we will create our first processor.

story_section6_g1

Application Architecture

In order to meet our new goal we will add the Features processor and a new Sink as seen below.

3 Application Architecture

Create the Feature Processor

story_section6_g2

Processor with a Custom Binding Interface

Processor using Dynamic Destinations

Create the Feature Sink for the Boss

Update the Tweet Board Subscription

Note that our processor that we created earlier in this lab publishes to multiple topics essentially splitting our feed into two. Due to our new requirements to not show new features on the twitter board we need to update that sink appropriately.

story_section7

Application Architecture

We're going to add a "No Yelling" processor in our event driven architecture in order to meet this new need.

4 Application Architecture

Creating a SCS Processor using Spring Cloud Functions

* After creating the function we now need to tell SCS add the `spring.cloud.stream.function.definition` argument to the `SpringApplication.run` command in the `main` method as seen below. This specifies which functional bean to bind to the external destination(s) exposed by the bindings.
- ``` java
SpringApplication.run(ScsprocessoryellingApplication.class, "--spring.cloud.stream.function.definition=changeCase");

At this point we've created our reactive function, registered it as a bean, and told spring cloud stream what function to bind to the available bindings! So let's run it and see if it works...

Running the Processor

story_section8

Application Architecture

A processor will be added to our architecture in order to convert negative words to positive ones.

5 Application Architecture

Create the Processor

Let's get started and hopefully have a bit of fun!

Update the Tweet Board Subscription

story_section9

Application Architecture

To meet this new requirement we are going to add the MQTT Web App shown in the diagram below:

6 Application Architecture

Obtain PubSub+ Connection Info for MQTT over Websockets

IF PARTICIPATING IN AN INSTRUCTOR LED WORKSHOP THE INSTRUCTOR WILL PERFORM THIS SECTION. YOU ARE WELCOME TO RUN LOCALLY IF YOU WOULD LIKE

Create the Web App

IF PARTICIPATING IN AN INSTRUCTOR LED WORKSHOP THE INSTRUCTOR WILL PERFORM THIS SECTION. YOU ARE WELCOME TO RUN LOCALLY IF YOU WOULD LIKE

Review

story_section10

Continued learning topics:

This course was just an introduction to Spring Cloud Streams, but we've included some resources below if you're interested in learning more about it or some of the features that complement it! Happy Learning :)