@SubscribeMapping和ChannelInterceptorAdapter.preSend之间的竞争条件

时间:2017-12-22 18:03:21

标签: java spring spring-boot spring-websocket

在使用Spring Websockets的Spring Boot 1.5.9应用程序中,使用SPR-16323的实现拦截了Web套接字订阅:

@Override
public Message<?> preSend(Message<?> message, MessageChannel channel) {
    StompHeaderAccessor accessor = StompHelper.accessor(message);
    StompCommand command = accessor.getCommand();

    if (null != command) {
        StompHelper.authentication(message, tokenHandler);

        switch (command) {

        case SUBSCRIBE:
            this.stompHandler.subscribe(message);
            break;
        case UNSUBSCRIBE:
            this.stompHandler.unsubscribe(message);
            break;
        // ...
        }
    }
    return super.preSend(message, channel);
}

捕获订户和目的地,以便发送特定于订户的消息。在Web套接字控制器中,@SubscribeMapping实现向订阅者发布初始化数据。但@SubscribeMappingChannelInterceptorAdapter.preSend在不同的线程中被调用,因此@SubscribeMapping偶尔会在ChannelInterceptorAdapter.preSend完成之前执行,这会导致订阅者无法获取初始化数据。

这看起来像是一个Spring Websocket设计问题,但是有一种(优雅的)解决方法吗?

更新:提交了一个错误:{{3}}

0 个答案:

没有答案
相关问题