gpt4 book ai didi

java - Spring websockets : sending a message to an offline user - messages not enqueued in message broker

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:46:54 25 4
gpt4 key购买 nike

我有一个使用消息代理 (activemq) 的带有 spring 和 websockets 的 webapp。

这是我的配置类:

@Configuration
@EnableWebSocketMessageBroker
@EnableScheduling
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableStompBrokerRelay("/topic","/queue/");
config.setApplicationDestinationPrefixes("/app");
}

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/hello").withSockJS();
}

}

我有一个计划任务不断向用户名“StanTheMan”推送消息:

@Scheduled(fixedDelay = 1000)
public void sendGreetings() {
HelloMessage hello = new HelloMessage();
hello.setContent("timeStamp:" + System.currentTimeMillis());
String queueMapping = "/queue/greetings";
template.convertAndSendToUser(
"DannyD",
queueMapping,
hello);

}

目前,如果用户没有通过 websocket 连接到服务器 - 他的所有消息都没有被排队等待,它们只是丢弃。每当他连接时 - 新消息正在为他排队。我是否可以通过任何方式将消息“convertAndSendToUser”发送给离线用户?我想为离线用户加入过期时间的消息,以便稍后在他们再次连接且过期时间未结束时推送。

我怎样才能做到这一点?显然,使用真正的消息代理 (activemq) 应该有助于实现这一目标,但如何实现呢?

谢谢!

最佳答案

事实上,此功能只能用于向(当前)连接的用户发送消息。

我们计划提供更好的方法来跟踪失败的消息(参见 SPR-10891)。同时,作为一种解决方法,您可以将 UserSessionRegistry 注入(inject)到您的 @Scheduled 组件中,并检查 getSessionIds 方法是否返回非空 Set。这将表明用户已连接。

一种替代方法可能是使用您自己的队列名称约定,每个用户都可以订阅该队列名称(可能基于他们的用户名或其他足够独特的名称)以便接收持久消息。 ActiveMQ STOMP page有一个关于持久消息和过期时间的部分。

关于java - Spring websockets : sending a message to an offline user - messages not enqueued in message broker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23782390/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com