Make spring-task-launch-request-function as auto-config
* Also make a `spring-spel-function`, `spring-filter-function` as auto-config * Fix Checkstyle violations in those module, as well as in the `spring-payload-converter-function`
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2020-2020 the original author or authors.
|
||||
* Copyright 2020-2024 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.
|
||||
@@ -23,14 +23,14 @@ import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.expression.EvaluationContext;
|
||||
import org.springframework.expression.Expression;
|
||||
import org.springframework.expression.spel.standard.SpelExpressionParser;
|
||||
import org.springframework.integration.expression.ExpressionUtils;
|
||||
import org.springframework.integration.context.IntegrationContextUtils;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.util.StringUtils;
|
||||
@@ -40,11 +40,11 @@ import org.springframework.util.StringUtils;
|
||||
* that can be composed with other Suppliers or Functions to transform any {@link Message}
|
||||
* to a {@link TaskLaunchRequest} which may be used as input to the
|
||||
* {@code TaskLauncherFunction} to launch a task.
|
||||
*
|
||||
* <p>
|
||||
* Command line arguments used by the task, as well as the task name itself may be
|
||||
* statically configured or extracted from the message contents, using SpEL. See
|
||||
* {@link TaskLaunchRequestFunctionProperties} for details.
|
||||
*
|
||||
* <p>
|
||||
* It is also possible to provide your own implementations of
|
||||
* {@link CommandLineArgumentsMessageMapper} and {@link TaskNameMessageMapper}.
|
||||
*
|
||||
@@ -57,7 +57,7 @@ public class TaskLaunchRequestFunctionConfiguration {
|
||||
/**
|
||||
* The function name.
|
||||
*/
|
||||
public final static String TASK_LAUNCH_REQUEST_FUNCTION_NAME = "taskLaunchRequestFunction";
|
||||
public static final String TASK_LAUNCH_REQUEST_FUNCTION_NAME = "taskLaunchRequestFunction";
|
||||
|
||||
/**
|
||||
* A {@link java.util.function.Function} to transform a {@link Message} payload to a
|
||||
@@ -67,22 +67,24 @@ public class TaskLaunchRequestFunctionConfiguration {
|
||||
* @return a {@code TaskLaunchRequest} Message.
|
||||
*/
|
||||
@Bean(name = TASK_LAUNCH_REQUEST_FUNCTION_NAME)
|
||||
public TaskLaunchRequestFunction taskLaunchRequest(
|
||||
TaskLaunchRequestMessageProcessor taskLaunchRequestMessageProcessor) {
|
||||
return message -> taskLaunchRequestMessageProcessor.postProcessMessage(message);
|
||||
TaskLaunchRequestFunction taskLaunchRequest(TaskLaunchRequestMessageProcessor taskLaunchRequestMessageProcessor) {
|
||||
|
||||
return taskLaunchRequestMessageProcessor::postProcessMessage;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public TaskLaunchRequestSupplier taskLaunchRequestInitializer(
|
||||
TaskLaunchRequestSupplier taskLaunchRequestInitializer(
|
||||
TaskLaunchRequestFunctionProperties taskLaunchRequestProperties) {
|
||||
|
||||
return new TaskLaunchRequestPropertiesInitializer(taskLaunchRequestProperties);
|
||||
}
|
||||
|
||||
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
|
||||
@Bean
|
||||
public TaskLaunchRequestMessageProcessor taskLaunchRequestMessageProcessor(
|
||||
TaskLaunchRequestMessageProcessor taskLaunchRequestMessageProcessor(
|
||||
TaskLaunchRequestSupplier taskLaunchRequestInitializer, TaskLaunchRequestFunctionProperties properties,
|
||||
EvaluationContext evaluationContext, @Nullable TaskNameMessageMapper taskNameMessageMapper,
|
||||
@Qualifier(IntegrationContextUtils.INTEGRATION_EVALUATION_CONTEXT_BEAN_NAME) EvaluationContext evaluationContext,
|
||||
@Nullable TaskNameMessageMapper taskNameMessageMapper,
|
||||
@Nullable CommandLineArgumentsMessageMapper commandLineArgumentsMessageMapper) {
|
||||
|
||||
if (taskNameMessageMapper == null) {
|
||||
@@ -97,13 +99,9 @@ public class TaskLaunchRequestFunctionConfiguration {
|
||||
commandLineArgumentsMessageMapper);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public EvaluationContext evaluationContext(BeanFactory beanFactory) {
|
||||
return ExpressionUtils.createStandardEvaluationContext(beanFactory);
|
||||
}
|
||||
|
||||
private TaskNameMessageMapper taskNameMessageMapper(TaskLaunchRequestFunctionProperties taskLaunchRequestProperties,
|
||||
EvaluationContext evaluationContext) {
|
||||
|
||||
if (StringUtils.hasText(taskLaunchRequestProperties.getTaskNameExpression())) {
|
||||
SpelExpressionParser expressionParser = new SpelExpressionParser();
|
||||
Expression taskNameExpression = expressionParser
|
||||
@@ -111,12 +109,13 @@ public class TaskLaunchRequestFunctionConfiguration {
|
||||
return new ExpressionEvaluatingTaskNameMessageMapper(taskNameExpression, evaluationContext);
|
||||
}
|
||||
|
||||
return message -> taskLaunchRequestProperties.getTaskName();
|
||||
return (message) -> taskLaunchRequestProperties.getTaskName();
|
||||
}
|
||||
|
||||
private CommandLineArgumentsMessageMapper commandLineArgumentsMessageMapper(
|
||||
TaskLaunchRequestFunctionProperties taskLaunchRequestFunctionProperties,
|
||||
EvaluationContext evaluationContext) {
|
||||
|
||||
return new ExpressionEvaluatingCommandLineArgsMapper(taskLaunchRequestFunctionProperties.getArgExpressions(),
|
||||
evaluationContext);
|
||||
}
|
||||
@@ -130,25 +129,17 @@ public class TaskLaunchRequestFunctionConfiguration {
|
||||
this.deploymentPropertiesSupplier(() -> KeyValueListParser
|
||||
.parseCommaDelimitedKeyValuePairs(taskLaunchRequestProperties.getDeploymentProperties()));
|
||||
|
||||
this.taskNameSupplier(() -> taskLaunchRequestProperties.getTaskName());
|
||||
this.taskNameSupplier(taskLaunchRequestProperties::getTaskName);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static class ExpressionEvaluatingTaskNameMessageMapper implements TaskNameMessageMapper {
|
||||
|
||||
private final Expression expression;
|
||||
|
||||
private final EvaluationContext evaluationContext;
|
||||
|
||||
ExpressionEvaluatingTaskNameMessageMapper(Expression expression, EvaluationContext evaluationContext) {
|
||||
this.evaluationContext = evaluationContext;
|
||||
this.expression = expression;
|
||||
}
|
||||
private record ExpressionEvaluatingTaskNameMessageMapper(Expression expression,
|
||||
EvaluationContext evaluationContext) implements TaskNameMessageMapper {
|
||||
|
||||
@Override
|
||||
public String processMessage(Message<?> message) {
|
||||
return expression.getValue(evaluationContext, message).toString();
|
||||
return this.expression.getValue(this.evaluationContext, message).toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -166,7 +157,7 @@ public class TaskLaunchRequestFunctionConfiguration {
|
||||
SpelExpressionParser expressionParser = new SpelExpressionParser();
|
||||
|
||||
KeyValueListParser.parseCommaDelimitedKeyValuePairs(argExpressions)
|
||||
.forEach((k, v) -> argExpressionsMap.put(k, expressionParser.parseExpression(v)));
|
||||
.forEach((k, v) -> this.argExpressionsMap.put(k, expressionParser.parseExpression(v)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2020-2020 the original author or authors.
|
||||
* Copyright 2020-2024 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.
|
||||
@@ -82,7 +82,7 @@ public class TaskLaunchRequestFunctionProperties {
|
||||
}
|
||||
|
||||
public String getTaskName() {
|
||||
return taskName;
|
||||
return this.taskName;
|
||||
}
|
||||
|
||||
public void setTaskName(String taskName) {
|
||||
@@ -90,7 +90,7 @@ public class TaskLaunchRequestFunctionProperties {
|
||||
}
|
||||
|
||||
public String getTaskNameExpression() {
|
||||
return taskNameExpression;
|
||||
return this.taskNameExpression;
|
||||
}
|
||||
|
||||
public void setTaskNameExpression(String taskNameExpression) {
|
||||
@@ -98,7 +98,7 @@ public class TaskLaunchRequestFunctionProperties {
|
||||
}
|
||||
|
||||
public String getArgExpressions() {
|
||||
return argExpressions;
|
||||
return this.argExpressions;
|
||||
}
|
||||
|
||||
public void setArgExpressions(String argExpressions) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2020-2020 the original author or authors.
|
||||
* Copyright 2020-2024 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.
|
||||
@@ -46,22 +46,22 @@ class TaskLaunchRequestMessageProcessor implements MessagePostProcessor {
|
||||
|
||||
@Override
|
||||
public Message<TaskLaunchRequest> postProcessMessage(Message<?> message) {
|
||||
TaskLaunchRequest taskLaunchRequest = taskLaunchRequestInitializer.get();
|
||||
TaskLaunchRequest taskLaunchRequest = this.taskLaunchRequestInitializer.get();
|
||||
|
||||
if (!StringUtils.hasText(taskLaunchRequest.getTaskName())) {
|
||||
taskLaunchRequest.setTaskName(taskNameMessageMapper.processMessage(message));
|
||||
taskLaunchRequest.setTaskName(this.taskNameMessageMapper.processMessage(message));
|
||||
Assert.hasText(taskLaunchRequest.getTaskName(),
|
||||
() -> "'taskName' is required in " + TaskLaunchRequest.class.getName());
|
||||
}
|
||||
|
||||
taskLaunchRequest.addCommmandLineArguments(commandLineArgumentsMessageMapper.processMessage(message));
|
||||
taskLaunchRequest.addCommmandLineArguments(this.commandLineArgumentsMessageMapper.processMessage(message));
|
||||
|
||||
MessageBuilder<TaskLaunchRequest> builder = MessageBuilder.withPayload(taskLaunchRequest)
|
||||
.copyHeaders(message.getHeaders());
|
||||
return adjustHeaders(builder).build();
|
||||
}
|
||||
|
||||
private MessageBuilder<TaskLaunchRequest> adjustHeaders(MessageBuilder<TaskLaunchRequest> builder) {
|
||||
private static MessageBuilder<TaskLaunchRequest> adjustHeaders(MessageBuilder<TaskLaunchRequest> builder) {
|
||||
builder.setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON);
|
||||
return builder;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2020-2020 the original author or authors.
|
||||
* Copyright 2020-2024 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.
|
||||
@@ -30,18 +30,18 @@ class TaskLaunchRequestSupplier implements Supplier<TaskLaunchRequest> {
|
||||
|
||||
private Supplier<Map<String, String>> deploymentPropertiesSupplier;
|
||||
|
||||
public TaskLaunchRequestSupplier taskNameSupplier(Supplier<String> taskNameSupplier) {
|
||||
TaskLaunchRequestSupplier taskNameSupplier(Supplier<String> taskNameSupplier) {
|
||||
this.taskNameSupplier = taskNameSupplier;
|
||||
return this;
|
||||
}
|
||||
|
||||
public TaskLaunchRequestSupplier commandLineArgumentSupplier(Supplier<List<String>> commandLineArgumentsSupplier) {
|
||||
TaskLaunchRequestSupplier commandLineArgumentSupplier(Supplier<List<String>> commandLineArgumentsSupplier) {
|
||||
this.commandLineArgumentsSupplier = commandLineArgumentsSupplier;
|
||||
return this;
|
||||
}
|
||||
|
||||
public TaskLaunchRequestSupplier deploymentPropertiesSupplier(
|
||||
Supplier<Map<String, String>> deploymentPropertiesSupplier) {
|
||||
TaskLaunchRequestSupplier deploymentPropertiesSupplier(Supplier<Map<String, String>> deploymentPropertiesSupplier) {
|
||||
|
||||
this.deploymentPropertiesSupplier = deploymentPropertiesSupplier;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
/**
|
||||
* The Task Launcher function auto-configuration support.
|
||||
*/
|
||||
package org.springframework.cloud.fn.task.launch.request;
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2020-2020 the original author or authors.
|
||||
* Copyright 2020-2024 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.
|
||||
@@ -194,13 +194,13 @@ public class TaskLaunchRequestFunctionApplicationTests {
|
||||
@Bean
|
||||
@ConditionalOnProperty("customTaskNameExtractor")
|
||||
TaskNameMessageMapper taskNameExtractor() {
|
||||
return message -> ((String) (message.getPayload())).equalsIgnoreCase("foo") ? "fooTask" : "defaultTask";
|
||||
return (message) -> ((String) (message.getPayload())).equalsIgnoreCase("foo") ? "fooTask" : "defaultTask";
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnProperty("enhanceTLRArgs")
|
||||
CommandLineArgumentsMessageMapper commandLineArgumentsProvider() {
|
||||
return message -> Collections.singletonList("runtimeArg");
|
||||
return (message) -> Collections.singletonList("runtimeArg");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user