取得領先
VMware 提供培訓和認證,以加速您的進度。
了解更多作者:Josh Long
Spring Integration 4.1 剛發布,其中包含許多很棒的新功能! 我最喜歡的功能之一是什麼? 與 Spring 4 WebSocket 支援的智慧整合。 現在,您可以組合一個整合流程,其最終目的地是 WebSocket 客戶端。 也支援作為 WebSocket 服務的客戶端。
為了編譯它,您將需要 Java 8(我們在這裡大量使用 lambda)和以下 Maven 依賴項
org.springframework.integration
,artifactId:spring-integration-java-dsl
,version:1.0.0.RC1
。org.springframework.integration
,artifactId:spring-integration-websocket
,version:4.1.0.RELEASE
。org.springframework.boot
,artifactId:spring-boot-starter-websocket
,version:1.2.0.RC1
。為了解析這些依賴項,您需要 snapshot
和 milestone
Maven 儲存庫。
所有監聽 /names
的客戶端都將收到發送到 requestChannel
通道的任何訊息。 Spring 4 MessageChannel
是一個具名的管道 - 或多或少類似於 java.util.Queue<T>
。 此範例使用在新的 Spring Integration 4.1 Web Socket 支援之上的Spring Integration Java 組態 DSL。 這是範例
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());
}
}
IntegrationFlow
很簡單。 對於每個傳入的訊息,複製它並通過添加具有 SimpMessageHeaderAccessor.SESSION_ID_HEADER
的標頭將其發送到每個監聽的 WebSocket
會話,然後將其發送到出站 webSocketOutboundAdapter
,後者會將其傳遞到每個監聽的客戶端。 若要使其運作,請在一個瀏覽器視窗中打開 http://localhost:8080/
,然後在另一個視窗中打開 http://localhost:8080/hi/Spring
。 在此技術提示的程式碼儲存庫中示範了一個簡單的客戶端。
在 Spring Integration 4.1 文件中有關於如何使用 Web Socket 元件的出色文件。 在 Spring Integration 範例目錄中也有一個更鼓舞人心的範例。