Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Building a Spring Integration 4.1 WebSocket Endpoint

DZone's Guide to

Building a Spring Integration 4.1 WebSocket Endpoint

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

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
 */
@Configuration
@ComponentScan
@EnableAutoConfiguration
@RestController
public class Application {

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

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

    @Bean
    MessageHandler webSocketOutboundAdapter() {
        return new WebSocketOutboundMessageHandler(serverWebSocketContainer());
    }

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

    @Bean
    IntegrationFlow webSocketFlow() {
        return f -> {
            Function<Message , Object> splitter = m -> serverWebSocketContainer()
                    .getSessions()
                    .keySet()
                    .stream()
                    .map(s -> MessageBuilder.fromMessage(m)
                            .setHeader(SimpMessageHeaderAccessor.SESSION_ID_HEADER, s)
                            .build())
                    .collect(Collectors.toList());
            f.split( Message.class, splitter)
                    .channel(c -> c.executor(Executors.newCachedThreadPool()))
                    .handle(webSocketOutboundAdapter());
        };
    }

    @RequestMapping("/hi/{name}")
    public void send(@PathVariable String name) {
        requestChannel().send(MessageBuilder.withPayload(name).build());
    }
}

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.

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}