Browse Source

Polishing

pull/1155/head
Juergen Hoeller 10 years ago
parent
commit
fbc45ed7b6
  1. 38
      spring-messaging/src/main/java/org/springframework/messaging/simp/broker/DefaultSubscriptionRegistry.java
  2. 25
      spring-messaging/src/main/java/org/springframework/messaging/simp/broker/SimpleBrokerMessageHandler.java
  3. 21
      spring-messaging/src/main/java/org/springframework/messaging/simp/config/AbstractMessageBrokerConfiguration.java
  4. 23
      spring-messaging/src/main/java/org/springframework/messaging/simp/config/MessageBrokerRegistry.java
  5. 15
      spring-messaging/src/test/java/org/springframework/messaging/simp/config/MessageBrokerConfigurationTests.java

38
spring-messaging/src/main/java/org/springframework/messaging/simp/broker/DefaultSubscriptionRegistry.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2015 the original author or authors. * Copyright 2002-2016 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.
@ -50,7 +50,7 @@ import org.springframework.util.PathMatcher;
* in memory and uses a {@link org.springframework.util.PathMatcher PathMatcher} * in memory and uses a {@link org.springframework.util.PathMatcher PathMatcher}
* for matching destinations. * for matching destinations.
* *
* <p>As of 4.2 this class supports a {@link #setSelectorHeaderName selector} * <p>As of 4.2, this class supports a {@link #setSelectorHeaderName selector}
* header on subscription messages with Spring EL expressions evaluated against * header on subscription messages with Spring EL expressions evaluated against
* the headers to filter out messages in addition to destination matching. * the headers to filter out messages in addition to destination matching.
* *
@ -65,11 +65,10 @@ public class DefaultSubscriptionRegistry extends AbstractSubscriptionRegistry {
public static final int DEFAULT_CACHE_LIMIT = 1024; public static final int DEFAULT_CACHE_LIMIT = 1024;
/** The maximum number of entries in the cache */
private volatile int cacheLimit = DEFAULT_CACHE_LIMIT;
private PathMatcher pathMatcher = new AntPathMatcher(); private PathMatcher pathMatcher = new AntPathMatcher();
private volatile int cacheLimit = DEFAULT_CACHE_LIMIT;
private String selectorHeaderName = "selector"; private String selectorHeaderName = "selector";
private volatile boolean selectorHeaderInUse = false; private volatile boolean selectorHeaderInUse = false;
@ -82,32 +81,32 @@ public class DefaultSubscriptionRegistry extends AbstractSubscriptionRegistry {
/** /**
* Specify the maximum number of entries for the resolved destination cache. * Specify the {@link PathMatcher} to use.
* Default is 1024.
*/ */
public void setCacheLimit(int cacheLimit) { public void setPathMatcher(PathMatcher pathMatcher) {
this.cacheLimit = cacheLimit; this.pathMatcher = pathMatcher;
} }
/** /**
* Return the maximum number of entries for the resolved destination cache. * Return the configured {@link PathMatcher}.
*/ */
public int getCacheLimit() { public PathMatcher getPathMatcher() {
return this.cacheLimit; return this.pathMatcher;
} }
/** /**
* Specify the {@link PathMatcher} to use. * Specify the maximum number of entries for the resolved destination cache.
* Default is 1024.
*/ */
public void setPathMatcher(PathMatcher pathMatcher) { public void setCacheLimit(int cacheLimit) {
this.pathMatcher = pathMatcher; this.cacheLimit = cacheLimit;
} }
/** /**
* Return the configured {@link PathMatcher}. * Return the maximum number of entries for the resolved destination cache.
*/ */
public PathMatcher getPathMatcher() { public int getCacheLimit() {
return this.pathMatcher; return this.cacheLimit;
} }
/** /**
@ -123,12 +122,13 @@ public class DefaultSubscriptionRegistry extends AbstractSubscriptionRegistry {
* @since 4.2 * @since 4.2
*/ */
public void setSelectorHeaderName(String selectorHeaderName) { public void setSelectorHeaderName(String selectorHeaderName) {
Assert.notNull(selectorHeaderName); Assert.notNull(selectorHeaderName, "'selectorHeaderName' must not be null");
this.selectorHeaderName = selectorHeaderName; this.selectorHeaderName = selectorHeaderName;
} }
/** /**
* Return the name for the selector header. * Return the name for the selector header.
* @since 4.2
*/ */
public String getSelectorHeaderName() { public String getSelectorHeaderName() {
return this.selectorHeaderName; return this.selectorHeaderName;

25
spring-messaging/src/main/java/org/springframework/messaging/simp/broker/SimpleBrokerMessageHandler.java

@ -92,27 +92,30 @@ public class SimpleBrokerMessageHandler extends AbstractBrokerMessageHandler {
initPathMatcherToUse(); initPathMatcherToUse();
} }
private void initPathMatcherToUse() {
if (this.pathMatcher != null) {
if (this.subscriptionRegistry instanceof DefaultSubscriptionRegistry) {
((DefaultSubscriptionRegistry) this.subscriptionRegistry).setPathMatcher(this.pathMatcher);
}
}
}
public SubscriptionRegistry getSubscriptionRegistry() { public SubscriptionRegistry getSubscriptionRegistry() {
return this.subscriptionRegistry; return this.subscriptionRegistry;
} }
/** /**
* When configured, the given PathMatcher is passed down to the * When configured, the given PathMatcher is passed down to the underlying
* SubscriptionRegistry to use for matching destination to subscriptions. * SubscriptionRegistry to use for matching destination to subscriptions.
* <p>Default is a standard {@link org.springframework.util.AntPathMatcher}.
* @since 4.1
* @see #setSubscriptionRegistry
* @see DefaultSubscriptionRegistry#setPathMatcher
* @see org.springframework.util.AntPathMatcher
*/ */
public void setPathMatcher(PathMatcher pathMatcher) { public void setPathMatcher(PathMatcher pathMatcher) {
this.pathMatcher = pathMatcher; this.pathMatcher = pathMatcher;
initPathMatcherToUse(); initPathMatcherToUse();
} }
private void initPathMatcherToUse() {
if (this.pathMatcher != null && this.subscriptionRegistry instanceof DefaultSubscriptionRegistry) {
((DefaultSubscriptionRegistry) this.subscriptionRegistry).setPathMatcher(this.pathMatcher);
}
}
/** /**
* Configure the {@link org.springframework.scheduling.TaskScheduler} to * Configure the {@link org.springframework.scheduling.TaskScheduler} to
* use for providing heartbeat support. Setting this property also sets the * use for providing heartbeat support. Setting this property also sets the
@ -130,6 +133,7 @@ public class SimpleBrokerMessageHandler extends AbstractBrokerMessageHandler {
/** /**
* Return the configured TaskScheduler. * Return the configured TaskScheduler.
* @since 4.2
*/ */
public TaskScheduler getTaskScheduler() { public TaskScheduler getTaskScheduler() {
return this.taskScheduler; return this.taskScheduler;
@ -151,6 +155,7 @@ public class SimpleBrokerMessageHandler extends AbstractBrokerMessageHandler {
/** /**
* The configured value for the heart-beat settings. * The configured value for the heart-beat settings.
* @since 4.2
*/ */
public long[] getHeartbeatValue() { public long[] getHeartbeatValue() {
return this.heartbeatValue; return this.heartbeatValue;
@ -160,6 +165,7 @@ public class SimpleBrokerMessageHandler extends AbstractBrokerMessageHandler {
* Configure a {@link MessageHeaderInitializer} to apply to the headers * Configure a {@link MessageHeaderInitializer} to apply to the headers
* of all messages sent to the client outbound channel. * of all messages sent to the client outbound channel.
* <p>By default this property is not set. * <p>By default this property is not set.
* @since 4.1
*/ */
public void setHeaderInitializer(MessageHeaderInitializer headerInitializer) { public void setHeaderInitializer(MessageHeaderInitializer headerInitializer) {
this.headerInitializer = headerInitializer; this.headerInitializer = headerInitializer;
@ -167,6 +173,7 @@ public class SimpleBrokerMessageHandler extends AbstractBrokerMessageHandler {
/** /**
* Return the configured header initializer. * Return the configured header initializer.
* @since 4.1
*/ */
public MessageHeaderInitializer getHeaderInitializer() { public MessageHeaderInitializer getHeaderInitializer() {
return this.headerInitializer; return this.headerInitializer;

21
spring-messaging/src/main/java/org/springframework/messaging/simp/config/AbstractMessageBrokerConfiguration.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2015 the original author or authors. * Copyright 2002-2016 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.
@ -142,7 +142,7 @@ public abstract class AbstractMessageBrokerConfiguration implements ApplicationC
} }
/** /**
* A hook for sub-classes to customize the message channel for inbound messages * A hook for subclasses to customize the message channel for inbound messages
* from WebSocket clients. * from WebSocket clients.
*/ */
protected void configureClientInboundChannel(ChannelRegistration registration) { protected void configureClientInboundChannel(ChannelRegistration registration) {
@ -175,7 +175,7 @@ public abstract class AbstractMessageBrokerConfiguration implements ApplicationC
} }
/** /**
* A hook for sub-classes to customize the message channel for messages from * A hook for subclasses to customize the message channel for messages from
* the application or message broker to WebSocket clients. * the application or message broker to WebSocket clients.
*/ */
protected void configureClientOutboundChannel(ChannelRegistration registration) { protected void configureClientOutboundChannel(ChannelRegistration registration) {
@ -223,7 +223,7 @@ public abstract class AbstractMessageBrokerConfiguration implements ApplicationC
} }
/** /**
* A hook for sub-classes to customize message broker configuration through the * A hook for subclasses to customize message broker configuration through the
* provided {@link MessageBrokerRegistry} instance. * provided {@link MessageBrokerRegistry} instance.
*/ */
protected void configureMessageBroker(MessageBrokerRegistry registry) { protected void configureMessageBroker(MessageBrokerRegistry registry) {
@ -252,7 +252,7 @@ public abstract class AbstractMessageBrokerConfiguration implements ApplicationC
addReturnValueHandlers(returnValueHandlers); addReturnValueHandlers(returnValueHandlers);
handler.setCustomReturnValueHandlers(returnValueHandlers); handler.setCustomReturnValueHandlers(returnValueHandlers);
PathMatcher pathMatcher = this.getBrokerRegistry().getPathMatcher(); PathMatcher pathMatcher = getBrokerRegistry().getPathMatcher();
if (pathMatcher != null) { if (pathMatcher != null) {
handler.setPathMatcher(pathMatcher); handler.setPathMatcher(pathMatcher);
} }
@ -260,7 +260,7 @@ public abstract class AbstractMessageBrokerConfiguration implements ApplicationC
} }
/** /**
* Protected method for plugging in a custom sub-class of * Protected method for plugging in a custom subclass of
* {@link org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler * {@link org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler
* SimpAnnotationMethodMessageHandler}. * SimpAnnotationMethodMessageHandler}.
* @since 4.2 * @since 4.2
@ -320,7 +320,6 @@ public abstract class AbstractMessageBrokerConfiguration implements ApplicationC
} }
// Expose alias for 4.1 compatibility // Expose alias for 4.1 compatibility
@Bean(name={"messageBrokerTaskScheduler", "messageBrokerSockJsTaskScheduler"}) @Bean(name={"messageBrokerTaskScheduler", "messageBrokerSockJsTaskScheduler"})
public ThreadPoolTaskScheduler messageBrokerTaskScheduler() { public ThreadPoolTaskScheduler messageBrokerTaskScheduler() {
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler(); ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
@ -395,9 +394,9 @@ public abstract class AbstractMessageBrokerConfiguration implements ApplicationC
protected abstract SimpUserRegistry createLocalUserRegistry(); protected abstract SimpUserRegistry createLocalUserRegistry();
/** /**
* As of 4.2, UserSessionRegistry is deprecated in favor of SimpUserRegistry * As of 4.2, {@code UserSessionRegistry} is deprecated in favor of {@link SimpUserRegistry}
* exposing information about all connected users. The MultiServerUserRegistry * exposing information about all connected users. The {@link MultiServerUserRegistry}
* implementation in combination with UserRegistryMessageHandler can be used * implementation in combination with {@link UserRegistryMessageHandler} can be used
* to share user registries across multiple servers. * to share user registries across multiple servers.
*/ */
@Deprecated @Deprecated
@ -465,9 +464,9 @@ public abstract class AbstractMessageBrokerConfiguration implements ApplicationC
@Override @Override
public void handleMessage(Message<?> message) { public void handleMessage(Message<?> message) {
} }
} }
private class NoOpBrokerMessageHandler extends AbstractBrokerMessageHandler { private class NoOpBrokerMessageHandler extends AbstractBrokerMessageHandler {
public NoOpBrokerMessageHandler() { public NoOpBrokerMessageHandler() {

23
spring-messaging/src/main/java/org/springframework/messaging/simp/config/MessageBrokerRegistry.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2015 the original author or authors. * Copyright 2002-2016 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.
@ -96,6 +96,16 @@ public class MessageBrokerRegistry {
return this.brokerChannelRegistration; return this.brokerChannelRegistration;
} }
protected String getUserDestinationBroadcast() {
return (this.brokerRelayRegistration != null ?
this.brokerRelayRegistration.getUserDestinationBroadcast() : null);
}
protected String getUserRegistryBroadcast() {
return (this.brokerRelayRegistration != null ?
this.brokerRelayRegistration.getUserRegistryBroadcast() : null);
}
/** /**
* Configure one or more prefixes to filter destinations targeting application * Configure one or more prefixes to filter destinations targeting application
* annotated methods. For example destinations prefixed with "/app" may be * annotated methods. For example destinations prefixed with "/app" may be
@ -137,16 +147,6 @@ public class MessageBrokerRegistry {
return this.userDestinationPrefix; return this.userDestinationPrefix;
} }
protected String getUserDestinationBroadcast() {
return (this.brokerRelayRegistration != null ?
this.brokerRelayRegistration.getUserDestinationBroadcast() : null);
}
protected String getUserRegistryBroadcast() {
return (this.brokerRelayRegistration != null ?
this.brokerRelayRegistration.getUserRegistryBroadcast() : null);
}
/** /**
* 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.
@ -162,6 +162,7 @@ public class MessageBrokerRegistry {
* <p>When the simple broker is enabled, the PathMatcher configured here is * <p>When the simple broker is enabled, the PathMatcher configured here is
* also used to match message destinations when brokering messages. * also used to match message destinations when brokering messages.
* @since 4.1 * @since 4.1
* @see org.springframework.messaging.simp.broker.DefaultSubscriptionRegistry#setPathMatcher
*/ */
public MessageBrokerRegistry setPathMatcher(PathMatcher pathMatcher) { public MessageBrokerRegistry setPathMatcher(PathMatcher pathMatcher) {
this.pathMatcher = pathMatcher; this.pathMatcher = pathMatcher;

15
spring-messaging/src/test/java/org/springframework/messaging/simp/config/MessageBrokerConfigurationTests.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2015 the original author or authors. * Copyright 2002-2016 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.
@ -16,9 +16,6 @@
package org.springframework.messaging.simp.config; package org.springframework.messaging.simp.config;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
@ -71,6 +68,9 @@ import org.springframework.validation.Errors;
import org.springframework.validation.Validator; import org.springframework.validation.Validator;
import org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean; import org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
/** /**
* Test fixture for {@link AbstractMessageBrokerConfiguration}. * Test fixture for {@link AbstractMessageBrokerConfiguration}.
* *
@ -435,6 +435,7 @@ public class MessageBrokerConfigurationTests {
} }
} }
static class BaseTestMessageBrokerConfig extends AbstractMessageBrokerConfiguration { static class BaseTestMessageBrokerConfig extends AbstractMessageBrokerConfiguration {
@Override @Override
@ -443,6 +444,7 @@ public class MessageBrokerConfigurationTests {
} }
} }
@SuppressWarnings("unused") @SuppressWarnings("unused")
@Configuration @Configuration
static class SimpleBrokerConfig extends BaseTestMessageBrokerConfig { static class SimpleBrokerConfig extends BaseTestMessageBrokerConfig {
@ -471,6 +473,7 @@ public class MessageBrokerConfigurationTests {
} }
} }
@Configuration @Configuration
static class BrokerRelayConfig extends SimpleBrokerConfig { static class BrokerRelayConfig extends SimpleBrokerConfig {
@ -482,10 +485,12 @@ public class MessageBrokerConfigurationTests {
} }
} }
@Configuration @Configuration
static class DefaultConfig extends BaseTestMessageBrokerConfig { static class DefaultConfig extends BaseTestMessageBrokerConfig {
} }
@Configuration @Configuration
static class CustomConfig extends BaseTestMessageBrokerConfig { static class CustomConfig extends BaseTestMessageBrokerConfig {
@ -534,6 +539,7 @@ public class MessageBrokerConfigurationTests {
} }
} }
private static class TestValidator implements Validator { private static class TestValidator implements Validator {
@Override @Override
@ -546,6 +552,7 @@ public class MessageBrokerConfigurationTests {
} }
} }
@SuppressWarnings("serial") @SuppressWarnings("serial")
private static class CustomThreadPoolTaskExecutor extends ThreadPoolTaskExecutor { private static class CustomThreadPoolTaskExecutor extends ThreadPoolTaskExecutor {
} }

Loading…
Cancel
Save