Skip to content

@Scheduled does not work on @ServerEndpoint deployed with per-connection lifecycle [SPR-10611] #15240

@spring-projects-issues

Description

@spring-projects-issues

Nick Williams opened SPR-10611 and commented

I created the following class:

@ServerEndpoint(value = "/chat/{sessionId}",
        encoders = ChatMessageDecoderCodec.class,
        decoders = ChatMessageDecoderCodec.class,
        configurator = SpringConfigurator.class)
public class ChatEndpoint
{
    ...
    @Inject SessionRegistry sessionRegistry;
    @Inject ChatService chatService;
    ...
    @Scheduled(fixedDelay = 25_000L, initialDelay = 25_000L)
    public void sendPing()
    {
        log.info("Sending ping to WebSocket client.");
        try
        {
            this.wsSession.getBasicRemote()
                    .sendPing(ByteBuffer.wrap(ChatEndpoint.pongData));
        }
        catch(IOException e)
        {
            log.warn("Failed to send ping message to WebSocket client.", e);
        }
    }
    ...
    @PostConstruct
    public void initialize()
    {
        this.sessionRegistry.registerOnRemoveCallback(this::httpSessionRemoved);
    }
}

When I connect to the WebSocket connection, the @Inject dependencies get properly injected and the initialize method gets properly called. However, the @Scheduled method never executes. A @Scheduled method on a separate @Service executes just fine (so my scheduler/executor are configured correctly), but not the method on the endpoint.

This is likely to be a common use case. Pings need to be sent periodically to keep connections alive, and @Scheduled is the perfect way to do this. Whatever is keeping this from working should be resolved.


Affects: 4.0 M1

Issue Links:

0 votes, 5 watchers

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)status: declinedA suggestion or change that we don't feel we should currently apply

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions