Messaging With STOMP Over WebSockets using Spring Angular ActiveMQ

Web Socket

I will create push notify messaging with STOMP over WebSockets using Spring and ActiveMQ.

You might have seen similar tutorial at link WebSockets Using Spring Boot And Angular, but I used in-memory message broker but here I will use external message broker – ActiveMQ. I will use Angular for building front end or UI where message will be updated and displayed.

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

Prerequisites

ActiveMQ 5.15.9/5.18.3, Netty API, ActiveMQ API, ActiveMQ STOMP API

Spring Boot WebSocket Angular Example

Configuring ActiveMQ

Please look at the tutorial how to configure ActiveMQ.

Project Setup

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

For the maven based project you can use the following pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.roytuts</groupId>
	<artifactId>Spring-WebSocket-ActiveMQ</artifactId>
	<version>0.0.1-SNAPSHOT</version>





	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.compiler.source>19</maven.compiler.source>
		<maven.compiler.target>19</maven.compiler.target>
	</properties>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>3.1.5</version>
	</parent>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-websocket</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-activemq</artifactId>
		</dependency>

		<dependency>
			<groupId>io.projectreactor.netty</groupId>
			<artifactId>reactor-netty</artifactId>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

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 you are going to use external message broker – ActiveMQ.

package com.roytuts.spring.websocket.vo;

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.

I have enabled stompBrokerRelay with ActiveMQ host and port.

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

	@Override
	public void registerStompEndpoints(StompEndpointRegistry registry) {
		// registry.addEndpoint("/websocket").setAllowedOrigins("*").withSockJS(); //
		// spring boot 2.x.x
		registry.addEndpoint("/websocket").setAllowedOrigins("http://localhost:4200").withSockJS(); // spring boot 3.x.x
	}

	@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 the client application.

Running ActiveMQ Broker

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

\apache-activemq-5.xy.z\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 Example.

Source Code

Download

Share

Related posts

1 comment

  1. david

    nice article

    Reply

Leave a comment