Order property for SimpUserRegistry
Issue:SPR-17142
This commit is contained in:
@@ -27,6 +27,7 @@ import org.springframework.beans.factory.BeanInitializationException;
|
|||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.context.ApplicationContextAware;
|
import org.springframework.context.ApplicationContextAware;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.event.SmartApplicationListener;
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
import org.springframework.messaging.MessageHandler;
|
import org.springframework.messaging.MessageHandler;
|
||||||
import org.springframework.messaging.converter.ByteArrayMessageConverter;
|
import org.springframework.messaging.converter.ByteArrayMessageConverter;
|
||||||
@@ -419,15 +420,32 @@ public abstract class AbstractMessageBrokerConfiguration implements ApplicationC
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
@SuppressWarnings("deprecation")
|
||||||
public SimpUserRegistry userRegistry() {
|
public SimpUserRegistry userRegistry() {
|
||||||
return (getBrokerRegistry().getUserRegistryBroadcast() != null ?
|
SimpUserRegistry registry = createLocalUserRegistry();
|
||||||
new MultiServerUserRegistry(createLocalUserRegistry()) : 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
|
* Return a {@link org.springframework.validation.Validator
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ package org.springframework.messaging.simp.config;
|
|||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import org.springframework.context.event.SmartApplicationListener;
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
import org.springframework.messaging.MessageChannel;
|
import org.springframework.messaging.MessageChannel;
|
||||||
import org.springframework.messaging.SubscribableChannel;
|
import org.springframework.messaging.SubscribableChannel;
|
||||||
@@ -54,6 +55,9 @@ public class MessageBrokerRegistry {
|
|||||||
@Nullable
|
@Nullable
|
||||||
private String userDestinationPrefix;
|
private String userDestinationPrefix;
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private Integer userRegistryOrder;
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private PathMatcher pathMatcher;
|
private PathMatcher pathMatcher;
|
||||||
|
|
||||||
@@ -162,6 +166,22 @@ public class MessageBrokerRegistry {
|
|||||||
return this.userDestinationPrefix;
|
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
|
* Configure the PathMatcher to use to match the destinations of incoming
|
||||||
* messages to {@code @MessageMapping} and {@code @SubscribeMapping} methods.
|
* messages to {@code @MessageMapping} and {@code @SubscribeMapping} methods.
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
|
|||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.support.StaticApplicationContext;
|
import org.springframework.context.support.StaticApplicationContext;
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
import org.springframework.messaging.Message;
|
import org.springframework.messaging.Message;
|
||||||
import org.springframework.messaging.MessageChannel;
|
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.stomp.StompHeaderAccessor;
|
||||||
import org.springframework.messaging.simp.user.DefaultUserDestinationResolver;
|
import org.springframework.messaging.simp.user.DefaultUserDestinationResolver;
|
||||||
import org.springframework.messaging.simp.user.MultiServerUserRegistry;
|
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.SimpUserRegistry;
|
||||||
import org.springframework.messaging.simp.user.UserDestinationMessageHandler;
|
import org.springframework.messaging.simp.user.UserDestinationMessageHandler;
|
||||||
import org.springframework.messaging.simp.user.UserRegistryMessageHandler;
|
import org.springframework.messaging.simp.user.UserRegistryMessageHandler;
|
||||||
@@ -430,6 +434,15 @@ public class MessageBrokerConfigurationTests {
|
|||||||
assertEquals(8192, registry.getCacheLimit());
|
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
|
@Test
|
||||||
public void userBroadcasts() {
|
public void userBroadcasts() {
|
||||||
ApplicationContext context = loadConfig(BrokerRelayConfig.class);
|
ApplicationContext context = loadConfig(BrokerRelayConfig.class);
|
||||||
@@ -559,8 +572,12 @@ public class MessageBrokerConfigurationTests {
|
|||||||
static class BaseTestMessageBrokerConfig extends AbstractMessageBrokerConfiguration {
|
static class BaseTestMessageBrokerConfig extends AbstractMessageBrokerConfiguration {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected SimpUserRegistry createLocalUserRegistry() {
|
protected SimpUserRegistry createLocalUserRegistry(@Nullable Integer order) {
|
||||||
return mock(SimpUserRegistry.class);
|
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.setPathMatcher(new AntPathMatcher(".")).enableSimpleBroker("/topic", "/queue");
|
||||||
registry.setCacheLimit(8192);
|
registry.setCacheLimit(8192);
|
||||||
registry.setPreservePublishOrder(true);
|
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 {
|
private static class TestValidator implements Validator {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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
|
@Override
|
||||||
protected SimpUserRegistry createLocalUserRegistry() {
|
protected SimpUserRegistry createLocalUserRegistry(@Nullable Integer order) {
|
||||||
return new DefaultSimpUserRegistry();
|
DefaultSimpUserRegistry registry = new DefaultSimpUserRegistry();
|
||||||
|
if (order != null) {
|
||||||
|
registry.setOrder(order);
|
||||||
|
}
|
||||||
|
return registry;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
|||||||
Reference in New Issue
Block a user