Order property for SimpUserRegistry

Issue:SPR-17142
This commit is contained in:
Rossen Stoyanchev 2018-08-13 10:48:17 +03:00
parent 5322fa0fb7
commit 430065c31d
4 changed files with 97 additions and 9 deletions

View File

@ -27,6 +27,7 @@ import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.event.SmartApplicationListener;
import org.springframework.lang.Nullable;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.converter.ByteArrayMessageConverter;
@ -419,15 +420,32 @@ public abstract class AbstractMessageBrokerConfiguration implements ApplicationC
}
@Bean
@SuppressWarnings("deprecation")
public SimpUserRegistry userRegistry() {
return (getBrokerRegistry().getUserRegistryBroadcast() != null ?
new MultiServerUserRegistry(createLocalUserRegistry()) : createLocalUserRegistry());
SimpUserRegistry registry = createLocalUserRegistry();
if (registry == null) {
registry = createLocalUserRegistry(getBrokerRegistry().getUserRegistryOrder());
}
boolean broadcast = getBrokerRegistry().getUserRegistryBroadcast() != null;
return (broadcast ? new MultiServerUserRegistry(registry) : registry);
}
/**
* Create the user registry that provides access to the local users.
* Create the user registry that provides access to local users.
* @deprecated as of 5.1 in favor of {@link #createLocalUserRegistry(Integer)}
*/
protected abstract SimpUserRegistry createLocalUserRegistry();
@Deprecated
@Nullable
protected SimpUserRegistry createLocalUserRegistry() {
return null;
}
/**
* Create the user registry that provides access to local users.
* @param order the order to use as a {@link SmartApplicationListener}.
* @since 5.1
*/
protected abstract SimpUserRegistry createLocalUserRegistry(@Nullable Integer order);
/**
* Return a {@link org.springframework.validation.Validator

View File

@ -19,6 +19,7 @@ package org.springframework.messaging.simp.config;
import java.util.Arrays;
import java.util.Collection;
import org.springframework.context.event.SmartApplicationListener;
import org.springframework.lang.Nullable;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.SubscribableChannel;
@ -54,6 +55,9 @@ public class MessageBrokerRegistry {
@Nullable
private String userDestinationPrefix;
@Nullable
private Integer userRegistryOrder;
@Nullable
private PathMatcher pathMatcher;
@ -162,6 +166,22 @@ public class MessageBrokerRegistry {
return this.userDestinationPrefix;
}
/**
* Set the order for the
* {@link org.springframework.messaging.simp.user.SimpUserRegistry
* SimpUserRegistry} to use as a {@link SmartApplicationListener}.
* @param order the order value
* @since 5.0.8
*/
public void setUserRegistryOrder(int order) {
this.userRegistryOrder = order;
}
@Nullable
protected Integer getUserRegistryOrder() {
return this.userRegistryOrder;
}
/**
* Configure the PathMatcher to use to match the destinations of incoming
* messages to {@code @MessageMapping} and {@code @SubscribeMapping} methods.

View File

@ -30,6 +30,7 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.StaticApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.lang.Nullable;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
@ -57,6 +58,9 @@ import org.springframework.messaging.simp.stomp.StompCommand;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.messaging.simp.user.DefaultUserDestinationResolver;
import org.springframework.messaging.simp.user.MultiServerUserRegistry;
import org.springframework.messaging.simp.user.SimpSubscription;
import org.springframework.messaging.simp.user.SimpSubscriptionMatcher;
import org.springframework.messaging.simp.user.SimpUser;
import org.springframework.messaging.simp.user.SimpUserRegistry;
import org.springframework.messaging.simp.user.UserDestinationMessageHandler;
import org.springframework.messaging.simp.user.UserRegistryMessageHandler;
@ -430,6 +434,15 @@ public class MessageBrokerConfigurationTests {
assertEquals(8192, registry.getCacheLimit());
}
@Test
public void customUserRegistryOrder() {
ApplicationContext context = loadConfig(CustomConfig.class);
SimpUserRegistry registry = context.getBean(SimpUserRegistry.class);
assertTrue(registry instanceof TestUserRegistry);
assertEquals(99, ((TestUserRegistry) registry).getOrder());
}
@Test
public void userBroadcasts() {
ApplicationContext context = loadConfig(BrokerRelayConfig.class);
@ -559,8 +572,12 @@ public class MessageBrokerConfigurationTests {
static class BaseTestMessageBrokerConfig extends AbstractMessageBrokerConfiguration {
@Override
protected SimpUserRegistry createLocalUserRegistry() {
return mock(SimpUserRegistry.class);
protected SimpUserRegistry createLocalUserRegistry(@Nullable Integer order) {
TestUserRegistry registry = new TestUserRegistry();
if (order != null) {
registry.setOrder(order);
}
return registry;
}
}
@ -647,6 +664,7 @@ public class MessageBrokerConfigurationTests {
registry.setPathMatcher(new AntPathMatcher(".")).enableSimpleBroker("/topic", "/queue");
registry.setCacheLimit(8192);
registry.setPreservePublishOrder(true);
registry.setUserRegistryOrder(99);
}
}
@ -717,6 +735,34 @@ public class MessageBrokerConfigurationTests {
}
private static class TestUserRegistry implements SimpUserRegistry, Ordered {
private Integer order;
public void setOrder(int order) {
this.order = order;
}
@Override
public int getOrder() {
return this.order;
}
@Override
public SimpUser getUser(String userName) { return null; }
@Override
public Set<SimpUser> getUsers() { return null; }
@Override
public int getUserCount() { return 0; }
@Override
public Set<SimpSubscription> findSubscriptions(SimpSubscriptionMatcher matcher) { return null; }
}
private static class TestValidator implements Validator {
@Override

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -61,8 +61,12 @@ public abstract class WebSocketMessageBrokerConfigurationSupport extends Abstrac
}
@Override
protected SimpUserRegistry createLocalUserRegistry() {
return new DefaultSimpUserRegistry();
protected SimpUserRegistry createLocalUserRegistry(@Nullable Integer order) {
DefaultSimpUserRegistry registry = new DefaultSimpUserRegistry();
if (order != null) {
registry.setOrder(order);
}
return registry;
}
@Bean