Search code examples
springsessionwebsocketstomp

Spring4 session


Excuse me ! I have a question here. Which version of Spring is “org.springframework.session.*” ? I can't find it in Spring4.0 jar at all. here is the class:

public abstract class AbstractSessionWebSocketMessageBrokerConfigurer<S extends ExpiringSession>
            extends AbstractWebSocketMessageBrokerConfigurer {
    @Autowired
    @SuppressWarnings("rawtypes")
    private SessionRepository sessionRepository;
    @Autowired
    private ApplicationEventPublisher eventPublisher;

    @Override
    public void configureClientInboundChannel(ChannelRegistration registration) {
        registration.setInterceptors(sessionRepositoryInterceptor());
    }

    @Override
    public final void registerStompEndpoints(StompEndpointRegistry registry) {
        configureStompEndpoints(new SessionStompEndpointRegistry(registry,sessionRepositoryInterceptor()));
    }

    /**
     * Register STOMP endpoints mapping each to a specific URL and (optionally)
     * enabling and configuring SockJS fallback options with a
     * {@link SessionRepositoryMessageInterceptor} automatically added as an
     * interceptor.
     *
     * @param registry
     *            the {@link StompEndpointRegistry} which automatically has a
     *            {@link SessionRepositoryMessageInterceptor} added to it.
     */
    protected abstract void configureStompEndpoints(StompEndpointRegistry registry);

    @Override
    public void configureWebSocketTransport(
            WebSocketTransportRegistration registration) {
        registration.addDecoratorFactory(wsConnectHandlerDecoratorFactory());
    }

    @Bean
    public WebSocketRegistryListener webSocketRegistryListener() {
        return new WebSocketRegistryListener();
    }

    @Bean
    public WebSocketConnectHandlerDecoratorFactory wsConnectHandlerDecoratorFactory() {
        return new WebSocketConnectHandlerDecoratorFactory(eventPublisher);
    }

    @Bean
    @SuppressWarnings("unchecked")
    public SessionRepositoryMessageInterceptor<S> sessionRepositoryInterceptor() {
        return new SessionRepositoryMessageInterceptor<S>(sessionRepository);
    }

    static class SessionStompEndpointRegistry implements StompEndpointRegistry {
        private final StompEndpointRegistry registry;
        private final HandshakeInterceptor interceptor;

        public SessionStompEndpointRegistry(StompEndpointRegistry registry,HandshakeInterceptor interceptor) {
            this.registry = registry;
            this.interceptor = interceptor;
        }

        public StompWebSocketEndpointRegistration addEndpoint(String... paths) {
            StompWebSocketEndpointRegistration endpoints = registry.addEndpoint(paths);
            endpoints.addInterceptors(interceptor);
            return endpoints;
        }
    }
}

Solution

  • Spring Session is a separate project: https://github.com/spring-projects/spring-session.

    You should use some dependency management tool (Gradle or Maven) to have a control over artifacts for your application.

    See WebScoket sample there: https://github.com/spring-projects/spring-session/tree/master/samples/websocket .

    A Spring Session artifact is:

    compile "org.springframework.session:spring-session:1.0.0.RC1"