spring websockets:向脱机用户发送消息 - 消息代理中未排队的消息 [英] spring websockets: sending a message to an offline user - messages not enqueued in message broker

查看:1110
本文介绍了spring websockets:向脱机用户发送消息 - 消息代理中未排队的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a webapp with spring and websockets using a message broker (activemq).

这是我的配置类:

@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:

I have a scheduled task that constantly pushing messages to the username "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消息转发给离线用户?我希望将过期时间的邮件排队,以便以后再次连接并且过期时间没有结束时推送离线用户。

Currently, if the user is NOT connected via a websocket to the server - all the messages for him are not being en-queued for him, they simply discarded. whenever he connects - fresh messages are being en-queued for him. Is it possible for me to "convertAndSendToUser" a message to an offline user in any way? i would like to en-queue messages with an expired time for offline users to be later on pushed when they are connecting again and the expired time wasn't over.

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

how can i achieve that? Obviously using a real message broker (activemq) supposed to help achieving that, but how?

谢谢!

推荐答案

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

Indeed this feature can only be used to send messages to a (presently) connected user.

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

We plan to provide better ways to track failed messages (see SPR-10891). In the meantime as a workaround you could inject the UserSessionRegistry into your @Scheduled component and check if the getSessionIds methods returns a non-empty Set. That would indicate the user is connected.

一种替代方法可能是使用您自己的约定来获取每个用户可以订阅的队列名称(可能基于他们的用户名或为了接收持久性消息,其他一些非常独特的东西。 ActiveMQ STOMP页面包含有关持久消息和到期时间的部分。

One alternative may be to use your own convention for a queue name that each user can subscribe to (probably based on their user name or something else that's unique enough) in order to receive persistent messages. The ActiveMQ STOMP page has a section on persistent messages and expiration times.

这篇关于spring websockets:向脱机用户发送消息 - 消息代理中未排队的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆