【问题标题】:Spring integration websocket throws " 'outputChannel' or 'outputChannelName' is required " ExceptionSpring集成websocket抛出“'outputChannel'或'outputChannelName'是必需的”异常
【发布时间】:2017-11-03 11:56:48
【问题描述】:

我是 Spring 集成框架的新手。 在运行 spring 集成 websocket 示例 code 我得到 需要“outputChannel”或“outputChannelName”例外。

我错过了什么吗?

以下是我的代码,

@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());
    }
}

异常堆栈,

java.lang.IllegalStateException: 'outputChannel' or 'outputChannelName' is required
    at org.springframework.util.Assert.state(Assert.java:70)
    at org.springframework.integration.endpoint.MessageProducerSupport.afterSingletonsInstantiated(MessageProducerSupport.java:153)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:781)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)

【问题讨论】:

    标签: java spring-boot spring-integration spring-websocket


    【解决方案1】:
    java.lang.IllegalStateException: 'outputChannel' or 'outputChannelName' is required
    at org.springframework.util.Assert.state(Assert.java:70)
    at org.springframework.integration.endpoint.MessageProducerSupport.afterSingletonsInstantiated(MessageProducerSupport.java:153)
    

    注意 - MessageProducerSupport。您的代码没有显示任何类型的内容,因此您只是在您的应用程序中向我们隐藏了一些内容。

    我猜你可能有类似的东西:

    @Bean
    public WebSocketInboundChannelAdapter webSocketInboundChannelAdapter() {
        ...
    }
    

    而正是这个必须用setOutputChannel() 声明。

    或者,如果您使用它作为 Java DSL 的起点 - IntegrationFlows.from(webSocketInboundChannelAdapter()),则不要使用 @Bean 声明它。框架会在之后为您处理正确的配置和注册。

    【讨论】:

    • 非常感谢您的支持。正如您所说,有一个带有 WebSocketInboundChannelAdapter bean 定义的配置类。我正在尝试为云流启动器构建一个源应用程序,我非常陌生,因为 github 上没有 websocket 源。我想启动一个 websocket 服务器作为源并将收到的消息传递给代理(Kafka/rabit)。我想知道框架处理了什么以及我们需要在Source部分写什么。可以简单介绍一下吗?
    • 好吧,这值得一个单独的 SO 问题。与您当前的无关。但是我们可以在适当的 GH 问题中讨论它:github.com/spring-cloud-stream-app-starters/websocket/issues/2。如果它足以解决上述问题,您应该考虑在这里接受我的回答
    • 我创建了一个新的 SO 问题 stackoverflow.com/questions/47175066/…
    猜你喜欢
    • 1970-01-01
    • 2021-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-03
    • 2010-10-15
    • 2014-07-27
    • 2016-09-10
    相关资源
    最近更新 更多