Over a million developers have joined DZone.

Building a Spring Integration 4.1 WebSocket Endpoint

· Java Zone

Learn more about the advantages of moving from a monolithic to microservices architecture.  Brought to you in partnership with IBM.

Originally authored by Josh Long  on the Spring blog

Spring Integration 4.1 was just released and it includes a lot of great new features! One of my favorites? Smart integration with the Spring 4 WebSocket support. Now you can compose a integration flow whose final destination is a WebSocket client. There is also support for acting as the client to a WebSocket service.

In order to compile it, you will need Java 8 (we make heavy use of lambas here) and the following Maven dependencies:

  • groupId:org.springframework.integration, artifactId:spring-integration-java-dsl, version: 1.0.0.RC1.
  • groupId:org.springframework.integration, artifactId:spring-integration-websocket, version: 4.1.0.RELEASE.
  • groupId:org.springframework.boot, artifactId:spring-boot-starter-websocket,version: 1.2.0.RC1.

In order to resolve those dependencies you will need the snapshot and milestone Maven repositories.

All clients listening on /names will receive whatever message is sent into therequestChannel channel. A Spring 4 MessageChannel is a named conduit - more or less analogous to a java.util.Queue<T>. This example uses the Spring Integration Java configuration DSL on top of the new Spring Integration 4.1 web socket support. Here’s the example:

package demo ;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.*;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.dsl.support.Function;
import org.springframework.integration.websocket.ServerWebSocketContainer;
import org.springframework.integration.websocket.outbound.WebSocketOutboundMessageHandler;
import org.springframework.messaging.*;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.Executors;
import java.util.stream.Collectors;

 * @author Artem Bilan
 * @author Josh Long
public class Application {

    public static void main(String args[]) throws Throwable {
        SpringApplication.run(Application.class, args);

    ServerWebSocketContainer serverWebSocketContainer() {
        return new ServerWebSocketContainer("/names").withSockJs();

    MessageHandler webSocketOutboundAdapter() {
        return new WebSocketOutboundMessageHandler(serverWebSocketContainer());

    @Bean(name = "webSocketFlow.input")
    MessageChannel requestChannel() {
        return new DirectChannel();

    IntegrationFlow webSocketFlow() {
        return f -> {
            Function<Message , Object> splitter = m -> serverWebSocketContainer()
                    .map(s -> MessageBuilder.fromMessage(m)
                            .setHeader(SimpMessageHeaderAccessor.SESSION_ID_HEADER, s)
            f.split( Message.class, splitter)
                    .channel(c -> c.executor(Executors.newCachedThreadPool()))

    public void send(@PathVariable String name) {

The IntegrationFlow is simple. For each message that comes in, copy it and address it to each listening WebSocket session by adding a header having theSimpMessageHeaderAccessor.SESSION_ID_HEADER, then send it the outboundwebSocketOutboundAdapter which will deliver it to each listening client. To see it work, openhttp://localhost:8080/ in one browser window, and thenhttp://localhost:8080/hi/Spring in another. There is a simple client demonstrated in this techtip's code repository.

There is great documentation on how to use the web socket components in Spring Integration 4.1 documentation. There's a more inspiring example in the Spring Integration samples directory, too.

From Idea to Application gives you the architecture to quickly build, manage and run a range of applications (web, mobile, big data, new smart devices, etc.) on an open-standard, cloud-based platform. See why developers are using IBM Bluemix. Brought to you in partnership with IBM.


Published at DZone with permission of Pieter Humphrey, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}