DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Building a Spring Integration 4.1 WebSocket Endpoint

Building a Spring Integration 4.1 WebSocket Endpoint

Pieter Humphrey user avatar by
Pieter Humphrey
CORE ·
Nov. 17, 14 · Java Zone · Interview
Like (0)
Save
Tweet
7.64K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Spring Integration Spring Framework Integration WebSocket

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • DZone's Article Submission Guidelines
  • Autowiring in Spring
  • Which JVM Version Is the Fastest?
  • OPC-UA, MQTT, and Apache Kafka: The Trinity of Data Streaming in IoT

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo