Introduction

We will create push notify messaging with STOMP over WebSockets using Spring and ActiveMQ. We have seen similar tutorial at link https://www.roytuts.com/spring-boot-websocket-angularjs-gradle-example/, but we used in-memory message broker but here we will use external message broker – ActiveMQ. We will use Angular 8 for building front end or UI where message will be updated and displayed.

We will use the same tutorial with some modification to the existing code.

Prerequisites

ActiveMQ 5.15.9, Netty API, ActiveMQ API, ActiveMQ STOMP API

Spring Boot WebSocket Angular 8 Gradle Example

Configuring ActiveMQ

Please look at the tutorial how to configure ActiveMQ.

Updating Build Script

I am assuming that you are following the same tutorial at Spring Boot WebSocket Angular 8 Gradle Example .

Update the build.gradle file with the following content.

buildscript { ext { springBootVersion = '2.1.8.RELEASE' } repositories { mavenLocal() mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") } } apply plugin: 'java' apply plugin: 'org.springframework.boot' sourceCompatibility = 12 targetCompatibility = 12 repositories { mavenLocal() mavenCentral() maven { url 'https://artifacts.alfresco.com/nexus/content/repositories/public/' } } dependencies { implementation("org.springframework.boot:spring-boot-starter-websocket:${springBootVersion}") implementation("org.springframework.boot:spring-boot-starter-activemq:${springBootVersion}") implementation("org.apache.activemq:activemq-stomp:5.15.10") implementation("io.projectreactor.netty:reactor-netty:0.8.11.RELEASE") }

Updating POJO

Update the POJO class Message to implement Serializable interface as we are going to use external message broker – ActiveMQ.

package com.roytuts.spring.websocket.vo; import java.io.Serializable; public class Message implements Serializable { private static final long serialVersionUID = 1L; private String msg; public Message(String msg) { this.msg = msg; } public String getMsg() { return msg; } }

Updating WebSocket Configuration

Updating WebSocket configuration class WebSocketConfig for configuring ActiveMQ with STOMP.

We enable stompBrokerRelay with ActiveMQ host and port.

package com.roytuts.spring.websocket.config; import org.springframework.context.annotation.Configuration; import org.springframework.messaging.simp.config.MessageBrokerRegistry; import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; import org.springframework.web.socket.config.annotation.StompEndpointRegistry; import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer; @Configuration @EnableWebSocketMessageBroker public class WebSocketConfig implements WebSocketMessageBrokerConfigurer { @Override public void registerStompEndpoints(StompEndpointRegistry registry) { registry.addEndpoint("/websocket").setAllowedOrigins("*").withSockJS(); } @Override public void configureMessageBroker(MessageBrokerRegistry config) { config.enableStompBrokerRelay("/topic").setRelayHost("localhost").setRelayPort(61613).setClientLogin("admin") .setClientPasscode("admin"); config.setApplicationDestinationPrefixes("/app"); } }

You don’t need to update on client application.

Running ActiveMQ Broker

Execute command from command line tool after navigating to the appropriate bin directory of ActiveMQ 5.15.9.

\apache-activemq-5.15.9\bin\start activemq

Related Posts:

Testing the Application

Make sure you run the server application followed by client application.

Then your client application will open at http://localhost:4200 in the browser and you will see the similar kind of message as you had seen for the output of Spring Boot WebSocket Angular 8 Gradle Example.

Source Code

download source code

Thanks for reading.