Code cleanup for rsocket module

This commit is contained in:
Oleg Zhurakousky
2020-08-28 17:47:54 +02:00
parent 525ce743b0
commit e4e8d22f7f
5 changed files with 24 additions and 24 deletions

View File

@@ -59,7 +59,7 @@ import org.springframework.util.ReflectionUtils;
* *
* @since 3.1 * @since 3.1
*/ */
public class FunctionRSocketMessageHandler extends RSocketMessageHandler { class FunctionRSocketMessageHandler extends RSocketMessageHandler {
private final FunctionCatalog functionCatalog; private final FunctionCatalog functionCatalog;
@@ -73,7 +73,7 @@ public class FunctionRSocketMessageHandler extends RSocketMessageHandler {
FrameType.REQUEST_STREAM, FrameType.REQUEST_STREAM,
FrameType.REQUEST_CHANNEL); FrameType.REQUEST_CHANNEL);
public FunctionRSocketMessageHandler(FunctionCatalog functionCatalog) { FunctionRSocketMessageHandler(FunctionCatalog functionCatalog) {
setHandlerPredicate((clazz) -> false); setHandlerPredicate((clazz) -> false);
this.functionCatalog = functionCatalog; this.functionCatalog = functionCatalog;
} }
@@ -85,14 +85,18 @@ public class FunctionRSocketMessageHandler extends RSocketMessageHandler {
super.afterPropertiesSet(); super.afterPropertiesSet();
} }
/**
* Will check if there is a function handler registered for destination before proceeding.
* This typically happens when user avoids using 'spring.cloud.function.definition' property.
*/
@Override @Override
public Mono<Void> handleMessage(Message<?> message) throws MessagingException { public Mono<Void> handleMessage(Message<?> message) throws MessagingException {
if (!FrameType.SETUP.equals(message.getHeaders().get("rsocketFrameType"))) { if (!FrameType.SETUP.equals(message.getHeaders().get("rsocketFrameType"))) {
String destination = this.getDestination(message).value(); String destination = this.getDestination(message).value();
Set<String> mappings = this.getDestinationLookup().keySet(); Set<String> mappings = this.getDestinationLookup().keySet();
if (!mappings.contains(destination)) { if (!mappings.contains(destination)) {
FunctionRSocketUtils.registerRSocketForwardingFunctionIfNecessary(destination, functionCatalog, this.getApplicationContext()); FunctionInvocationWrapper function = FunctionRSocketUtils
FunctionInvocationWrapper function = functionCatalog.lookup(destination, "application/json"); .registerFunctionForDestination(destination, functionCatalog, this.getApplicationContext());
this.registerFunctionHandler(new RSocketListenerFunction(function), destination); this.registerFunctionHandler(new RSocketListenerFunction(function), destination);
} }
} }
@@ -100,7 +104,7 @@ public class FunctionRSocketMessageHandler extends RSocketMessageHandler {
return super.handleMessage(message); return super.handleMessage(message);
} }
public void registerFunctionHandler(Function<?, ?> function, String route) { void registerFunctionHandler(Function<?, ?> function, String route) {
CompositeMessageCondition condition = CompositeMessageCondition condition =
new CompositeMessageCondition(REQUEST_CONDITION, new CompositeMessageCondition(REQUEST_CONDITION,
new DestinationPatternsMessageCondition(new String[]{ route }, new DestinationPatternsMessageCondition(new String[]{ route },

View File

@@ -50,10 +50,16 @@ final class FunctionRSocketUtils {
} }
static String registerRSocketForwardingFunctionIfNecessary(String definition, FunctionCatalog functionCatalog, static FunctionInvocationWrapper registerFunctionForDestination(String destination, FunctionCatalog functionCatalog,
ApplicationContext applicationContext) {
registerRSocketForwardingFunctionIfNecessary(destination, functionCatalog, applicationContext);
FunctionInvocationWrapper function = functionCatalog.lookup(destination, "application/json");
return function;
}
static void registerRSocketForwardingFunctionIfNecessary(String definition, FunctionCatalog functionCatalog,
ApplicationContext applicationContext) { ApplicationContext applicationContext) {
String[] names = StringUtils.delimitedListToStringArray(definition.replaceAll(",", "|").trim(), "|"); String[] names = StringUtils.delimitedListToStringArray(definition.replaceAll(",", "|").trim(), "|");
String rootFunctionName = names[0];
for (String name : names) { for (String name : names) {
if (!applicationContext.containsBean(name)) { // this means RSocket if (!applicationContext.containsBean(name)) { // this means RSocket
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
@@ -65,18 +71,11 @@ final class FunctionRSocketUtils {
String[] hostPort = StringUtils.delimitedListToStringArray(functionToRSocketDefinition[1], ":"); String[] hostPort = StringUtils.delimitedListToStringArray(functionToRSocketDefinition[1], ":");
rootFunctionName = function.getFunctionDefinition();
String forwardingUrl = functionToRSocketDefinition[1]; String forwardingUrl = functionToRSocketDefinition[1];
RSocketRequester rsocketRequester;
Builder rsocketRequesterBuilder = applicationContext.getBean(Builder.class); Builder rsocketRequesterBuilder = applicationContext.getBean(Builder.class);
RSocketRequester rsocketRequester = (WS_URI_PATTERN.matcher(forwardingUrl).matches())
if (WS_URI_PATTERN.matcher(forwardingUrl).matches()) { ? rsocketRequesterBuilder.websocket(URI.create(forwardingUrl))
rsocketRequester = rsocketRequesterBuilder.websocket(URI.create(forwardingUrl)); : rsocketRequesterBuilder.tcp(hostPort[0], Integer.parseInt(hostPort[1]));
}
else {
rsocketRequester = rsocketRequesterBuilder.tcp(hostPort[0], Integer.parseInt(hostPort[1]));
}
RSocketForwardingFunction rsocketFunction = RSocketForwardingFunction rsocketFunction =
new RSocketForwardingFunction(function, rsocketRequester, null); new RSocketForwardingFunction(function, rsocketRequester, null);
@@ -87,7 +86,5 @@ final class FunctionRSocketUtils {
((FunctionRegistry) functionCatalog).register(functionRegistration); ((FunctionRegistry) functionCatalog).register(functionRegistration);
} }
} }
return rootFunctionName;
} }
} }

View File

@@ -70,9 +70,8 @@ class RSocketAutoConfiguration implements ApplicationContextAware {
FunctionCatalog functionCatalog, FunctionProperties functionProperties) { FunctionCatalog functionCatalog, FunctionProperties functionProperties) {
String definition = functionProperties.getDefinition(); String definition = functionProperties.getDefinition();
if (StringUtils.hasText(definition)) { if (StringUtils.hasText(definition)) {
FunctionRSocketUtils.registerRSocketForwardingFunctionIfNecessary(definition, functionCatalog, this.applicationContext); FunctionInvocationWrapper function = FunctionRSocketUtils
//TODO externalize content-type .registerFunctionForDestination(definition, functionCatalog, this.applicationContext);
FunctionInvocationWrapper function = functionCatalog.lookup(definition, "application/json");
rsocketMessageHandler.registerFunctionHandler(new RSocketListenerFunction(function), definition); rsocketMessageHandler.registerFunctionHandler(new RSocketListenerFunction(function), definition);
rsocketMessageHandler.registerFunctionHandler(new RSocketListenerFunction(function), ""); rsocketMessageHandler.registerFunctionHandler(new RSocketListenerFunction(function), "");
} }

View File

@@ -34,7 +34,7 @@ import org.springframework.messaging.support.GenericMessage;
* which will use the result of the invocation of such function as an input to another RSocket * which will use the result of the invocation of such function as an input to another RSocket
* effectively composing two functions over RSocket. * effectively composing two functions over RSocket.
* <p> * <p>
* Note: the remote RSocket route is not necessary to be as a Spring Cloud Function binding. * Note: the remote RSocket route is not required to represent Spring Cloud Function binding.
* *
* @author Oleg Zhurakousky * @author Oleg Zhurakousky
* @author Artem Bilan * @author Artem Bilan

View File

@@ -40,7 +40,7 @@ import org.springframework.messaging.support.MessageBuilder;
* *
* @since 3.1 * @since 3.1
*/ */
public class RSocketListenerFunction implements Function<Message<Flux<byte[]>>, Publisher<?>> { class RSocketListenerFunction implements Function<Message<Flux<byte[]>>, Publisher<?>> {
private final FunctionInvocationWrapper targetFunction; private final FunctionInvocationWrapper targetFunction;