Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

320
Visualizações
What is MultiServerUserRegistry in spring websocket?

In package org.springframework.messaging.simp.user there is a class MultiServerUserRegistry.

This class looks like it would work on multi-server application, but I could not find any documentation that could help me understand how it works or how should I use it.

What does this class do and how do I use it? How do I use websocket to work on multi-server application?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

The default publish mechanism use a local sessions repository to resolve socket id for a user.
If you are setting up a cluster, you can't use this strategy. We need to use a remote repository.
The removeRepository already exists in Spring as MultiServerUserRegistry. To active it you just have to configure your MessageBrokerRegistry :

registry.enableStompBrokerRelay("/topic/", "/queue/", "/exchange/")
                .setUserDestinationBroadcast("/topic/unresolved-user")
                .setUserRegistryBroadcast("/topic/user-registry")

It works fine for me. I hope that it will help.

over 4 years ago · Santiago Trujillo Relatório

0

According to the MultiServerUserRegistry's JavaDoc, it's an implementation of SimpUserRegistry, which enable us to look up both local and remote user registries.

SimpUserRegistry that looks up users in a "local" user registry as well as a set of "remote" user registries. The local registry is provided as a constructor argument while remote registries are updated via broadcasts handled by UserRegistryMessageHandler which in turn notifies this registry when updates are received.

Another SimpUserRegistry's implementation is DefaultSimpUserRegistry, which could only get local user.

I read the related source code knowing that when we config setUserRegistryBroadcast in the enableStompBrokerRelay, a MultiServerUserRegistry will be created, and we can get all the users' online state regardless which server they connected in cluster.

Here is the key source code in AbstractMessageBrokerConfiguration:

@Bean
@SuppressWarnings("deprecation")
public SimpUserRegistry userRegistry() {
    SimpUserRegistry registry = createLocalUserRegistry();
    if (registry == null) {
        registry = createLocalUserRegistry(getBrokerRegistry().getUserRegistryOrder());
    }
    boolean broadcast = getBrokerRegistry().getUserRegistryBroadcast() != null;
    return (broadcast ? new MultiServerUserRegistry(registry) : registry);
}

As this function annotated with @Bean, we can get the SimpUserRegistry instance by @Autowired:

@Autowired
private SimpUserRegistry simpUserRegistry;

Using this interface, we have the ability to:

/**
 * Get the user for the given name.
 * @param userName the name of the user to look up
 * @return the user, or {@code null} if not connected
 */
@Nullable
SimpUser getUser(String userName);

/**
 * Return a snapshot of all connected users.
 * <p>The returned set is a copy and will not reflect further changes.
 * @return the connected users, or an empty set if none
 */
Set<SimpUser> getUsers();

/**
 * Return the count of all connected users.
 * @return the number of connected users
 * @since 4.3.5
 */
int getUserCount();

/**
 * Find subscriptions with the given matcher.
 * @param matcher the matcher to use
 * @return a set of matching subscriptions, or an empty set if none
 */
Set<SimpSubscription> findSubscriptions(SimpSubscriptionMatcher matcher);

We can use it to get the count of all the online users (by getUserCount) in our whole cluster, and detect whether a specific user is online or not (by getUser).

As my practice, the following message broker configure is suggested in multi-server scenario:

@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
    registry.setApplicationDestinationPrefixes("/app");
    registry.enableStompBrokerRelay("/topic", "/queue")
            .setUserDestinationBroadcast("/topic/log-unresolved-user")
            .setUserRegistryBroadcast("/topic/log-user-registry")
            .setRelayHost(brokerConfig.getHost())
            .setRelayPort(brokerConfig.getPort())
            .setVirtualHost(brokerConfig.getVirtualHost())
            .setClientLogin(brokerConfig.getUsername())
            .setClientPasscode(brokerConfig.getPassword())
            .setSystemLogin(systemUsername)
            .setSystemPasscode(systemPassword);
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda