简体   繁体   English

spring websockets:向离线用户发送消息 - 消息未在消息代理中排队

[英]spring websockets: sending a message to an offline user - messages not enqueued in message broker

I have a webapp with spring and websockets using a message broker (activemq).我有一个使用消息代理 (activemq) 的带有 spring 和 websockets 的 webapp。

here is my config class:这是我的配置类:

@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();
 }

}

I have a scheduled task that constantly pushing messages to the username "StanTheMan" :我有一个计划任务,不断向用户名“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);

}

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 .目前,如果用户没有通过 websocket 连接到服务器 - 他的所有消息都没有为他排队,他们只是丢弃了 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?我是否可以以任何方式“convertAndSendToUser”向离线用户发送消息? 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.我想将具有过期时间的消息排入队列,以便离线用户稍后在再次连接并且过期时间尚未结束时推送。

how can i achieve that?我怎样才能做到这一点? Obviously using a real message broker (activemq) supposed to help achieving that, but how?显然使用真正的消息代理(activemq)应该有助于实现这一目标,但如何实现?

Thanks!谢谢!

Indeed this feature can only be used to send messages to a (presently) connected user.实际上,此功能只能用于向(当前)连接的用户发送消息。

We plan to provide better ways to track failed messages (see SPR-10891 ).我们计划提供更好的方法来跟踪失败的消息(参见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.同时,作为一种解决方法,您可以将UserSessionRegistry注入 @Scheduled 组件并检查 getSessionIds 方法是否返回非空 Set。 That would indicate the user is connected.这将表明用户已连接。

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. ActiveMQ STOMP 页面有一个关于持久消息和过期时间的部分。

This is a default behavior for message brokers.这是消息代理的默认行为。 And you haven't setup the ActiveMQ broker as your default broker so Spring is setting up a in-memory broker.而且您还没有将 ActiveMQ 代理设置为默认代理,因此 Spring 正在设置一个内存中代理。

To achieve what you want setup/give your details of activeMQ to your spring websocket message broker.为了实现您想要的设置/将您的 activeMQ 详细信息提供给您的 spring websocket 消息代理。 As Spring doesn't provide these settings, you have to do all the persistence settings at the ActiveMQ side.由于 Spring 不提供这些设置,您必须在 ActiveMQ 端进行所有持久性设置。 . .

To setup ActiveMQ for spring websocket message broker you also need to enable stomp and use this:要为 spring websocket 消息代理设置 ActiveMQ,您还需要启用 stomp 并使用它:

registry.enableStompBrokerRelay("/topic").setRelayHost("hostName").setRelayPort("00000");

For more information checkout: http://docs.spring.io/spring/docs/4.0.0.RELEASE/javadoc-api/org/springframework/messaging/simp/config/StompBrokerRelayRegistration.html有关更多信息结帐: http : //docs.spring.io/spring/docs/4.0.0.RELEASE/javadoc-api/org/springframework/messaging/simp/config/StompBrokerRelayRegistration.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM