Reactive Messaging with Quarkus on OpenShift
  • Introduction
  • Setup the IBM Cloud Environment
    • Introduction
    • Access the Cluster
    • Access IBM Cloud Shell
  • Setup the sample application
    • Exercise 1 - Setup via Script
    • YouTube - How to setup the sample application (optional)
  • Build new version of the Microservice
    • Exercise 2 - Reactive Messaging with MicroProfile
    • Exercise 3 - Server Sent Events
    • Exercise 4 - Vert.x Event Bus
    • Exercise 5 (optional) - Use distributed Logging
  • Resources
    • How to setup the reactive sample application on OpenShift
    • Blog posts related to reactive
    • Workshop: Reactive Endpoint with Quarkus on OpenShift
    • Cloud-Native-Starter project
    • Cloud-Native-Starter project reactive
Powered by GitBook
On this page
  • Step 1: Understand the Web Application Consumer
  • Step 2: Develop the Streaming Endpoint
  • Step 3: Deploy new Version
  • Step 4: Verify new Version

Was this helpful?

  1. Build new version of the Microservice

Exercise 3 - Server Sent Events

PreviousExercise 2 - Reactive Messaging with MicroProfileNextExercise 4 - Vert.x Event Bus

Last updated 4 years ago

Was this helpful?

In this lab you'll learn how to expose streaming endpoints so that web applications are notified via .

The web application 'Web-App' receives notifications from the 'Web-API' service.

Step 1: Understand the Web Application Consumer

Let's take a look at the JavaScript code which consumes the server side events.

A new EventSource is created by passing in the the URL of the streaming endpoint. The function source.onmessage is invoked when the events arrive. In our case this triggers the reload of the last articles.

cd ~/cloud-native-starter/reactive/web-app-reactive/src/components
cat Home.vue

Step 2: Develop the Streaming Endpoint

The sample already comes with a working endpoint. Let's delete the file and recreate it from scratch.

cd ~/cloud-native-starter/reactive/web-api-reactive/src/main/java/com/ibm/webapi/apis/
rm NewArticlesStreamResource.java
touch NewArticlesStreamResource.java
nano NewArticlesStreamResource.java

Add the package name, the import statements and the empty class.

package com.ibm.webapi.apis;

import javax.inject.Inject;
import javax.ws.rs.core.MediaType;
import org.reactivestreams.Publisher;
import io.smallrye.reactive.messaging.annotations.Channel;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import org.jboss.resteasy.annotations.SseElementType;

@Path("/v2")
public class NewArticlesStreamResource {
}
    @Inject
    @Channel("stream-new-article") Publisher<String> newArticles;

Last, but not least, add the implementation of the streaming endpoint. The media type is MediaType.SERVER_SENT_EVENTS and the annotation @SseElementType defines the type.

    @GET
    @Path("/server-sent-events")
    @Produces(MediaType.SERVER_SENT_EVENTS)
    @SseElementType("text/plain")
    public Publisher<String> stream() {
        return newArticles;
    }

Exit the Editor via 'Ctrl-X', 'y' and 'Enter'.

Step 3: Deploy new Version

cd ~/cloud-native-starter/reactive/web-api-reactive
oc start-build web-api-reactive --from-dir=.

On the 'Builds' page wait until the new build has been completed.

Once completed, delete the 'Web-API' pod which causes a new pod with the latest image to be started.

Step 4: Verify new Version

Make sure all four pods in the 'cloud-native-starter' project are running. Note that it takes a couple of minutes until this happens.

To launch the application get the URLs via the following command.

~/cloud-native-starter/reactive/os4-scripts/show-urls.sh

Open the web application in a browser. Then invoke the curl post command. The web application should show the new entry.

In you saw how to publish messages to the in-memory channel 'stream-new-article'. A publisher to this channel can easily be injected via @Inject and @Channel.

Once you've entered everything the should look like this.

Lab 2
class
Server Sent Events
server events
server events
server events