Configure PathPattern for RSocket routing

This commit configures a `PathPatternRouteMatcher` in the RSocket
support if spring-web is on the classpath. This `RouteMatcher`
implementation is more efficient than the `SimpleRouteMatcher`, which is
based on the `AntPathMatcher`.

Closes gh-16865
pull/16829/merge
Brian Clozel 6 years ago
parent a6fdfaccaa
commit 5b2de5c81d

@ -28,6 +28,9 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.rsocket.MessageHandlerAcceptor;
import org.springframework.messaging.rsocket.RSocketRequester;
import org.springframework.messaging.rsocket.RSocketStrategies;
import org.springframework.util.ClassUtils;
import org.springframework.web.util.pattern.PathPatternParser;
import org.springframework.web.util.pattern.PathPatternRouteMatcher;
/**
* {@link EnableAutoConfiguration Auto-configuration} for Spring RSocket support in Spring
@ -42,12 +45,19 @@ import org.springframework.messaging.rsocket.RSocketStrategies;
@AutoConfigureAfter(RSocketStrategiesAutoConfiguration.class)
public class RSocketMessagingAutoConfiguration {
private static String PATHPATTERN_ROUTEMATCHER_CLASS = "org.springframework.web.util.pattern.PathPatternRouteMatcher";
@Bean
@ConditionalOnMissingBean
public MessageHandlerAcceptor messageHandlerAcceptor(
RSocketStrategies rSocketStrategies) {
MessageHandlerAcceptor acceptor = new MessageHandlerAcceptor();
acceptor.setRSocketStrategies(rSocketStrategies);
if (ClassUtils.isPresent(PATHPATTERN_ROUTEMATCHER_CLASS, null)) {
PathPatternParser parser = new PathPatternParser();
parser.setSeparator('.');
acceptor.setRouteMatcher(new PathPatternRouteMatcher(parser));
}
return acceptor;
}

@ -26,6 +26,7 @@ import org.springframework.core.codec.CharSequenceEncoder;
import org.springframework.core.codec.StringDecoder;
import org.springframework.messaging.rsocket.MessageHandlerAcceptor;
import org.springframework.messaging.rsocket.RSocketStrategies;
import org.springframework.web.util.pattern.PathPatternRouteMatcher;
import static org.assertj.core.api.Assertions.assertThat;
@ -43,8 +44,11 @@ public class RSocketMessagingAutoConfigurationTests {
@Test
public void shouldCreateDefaultBeans() {
this.contextRunner.run((context) -> assertThat(context)
.getBeans(MessageHandlerAcceptor.class).hasSize(1));
this.contextRunner.run((context) -> {
assertThat(context).getBeans(MessageHandlerAcceptor.class).hasSize(1);
assertThat(context.getBean(MessageHandlerAcceptor.class).getRouteMatcher())
.isInstanceOf(PathPatternRouteMatcher.class);
});
}
@Test

Loading…
Cancel
Save