如何在不同的端口号上运行spring integration websocket程序以及如何检查心跳

时间:2014-12-03 10:43:58

标签: spring spring-integration spring-websocket

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;

@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("/messages").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());
    }
}

在上面的程序中,我可以将消息发送到其他注册浏览器。但是当我尝试更改端口号时我遇到了问题,我无法检查Web套接字的心跳。

我在这里使用http://localhost:8080/http://localhost:8080/hi/Shiva Kumar

访问服务器

提前感谢您的宝贵意见

2 个答案:

答案 0 :(得分:0)

由于这个应用程序是Spring Boot one,我们在这里使用一个嵌入式Servlet容器,你可以按照Boot的建议来定制这个环境:

  

可以使用Spring Environment属性配置公共servlet容器设置。通常,您需要在application.properties文件中定义属性。

     

常用服务器设置包括:

server.port — The listen port for incoming HTTP requests.
server.address — The interface address to bind to.
server.sessionTimeout — A session timeout. 
  

有关完整列表,请参阅ServerProperties课程。

关于heartBeat。来自SockJsServiceRegistration JavaDocs:

/**
 * The amount of time in milliseconds when the server has not sent any
 * messages and after which the server should send a heartbeat frame to the
 * client in order to keep the connection from breaking.
 * <p>The default value is 25,000 (25 seconds).
 */
public SockJsServiceRegistration setHeartbeatTime(long heartbeatTime) {
    this.heartbeatTime = heartbeatTime;
    return this;
}

只要您使用.withSockJs()作为默认值,heartbeatTime就是25 sec

您对check the heart beat有何看法?

答案 1 :(得分:0)

来自@EnableAutoConfiguration

的文档
  

启用Spring Application Context的自动配置,尝试猜测和配置您可能需要的bean。自动配置类通常基于您的类路径和您定义的bean来应用。例如,如果您的类路径上有tomcat-embedded.jar,则可能需要TomcatEmbeddedServletContainerFactory(除非您已经定义了自己的EmbeddedServletContainerFactory bean)。

示例:

@Configuration
@EnableAutoConfiguration
public class MyConfiguration{

    @Bean
    public EmbeddedServletContainerFactory servletContainer() {
        TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
        factory.setPort("1337");
        return factory;
    }

}