Provided Advice Classes

除了提供应用 AOP 建议类的通用机制外,Spring Integration 还在开箱即用的情况下提供这些建议实现:

In addition to providing the general mechanism to apply AOP advice classes, Spring Integration provides these out-of-the-box advice implementations:

Retry Advice

重试建议 (o.s.i.handler.advice.RequestHandlerRetryAdvice) 充分利用了 Spring Retry项目提供的丰富的重试机制。spring-retry`的核心组件是 `RetryTemplate,它允许配置复杂重试场景,包括 `RetryPolicy`和 `BackoffPolicy`策略(以及多种实现),以及 `RecoveryCallback`策略以确定在重试耗尽时采取的操作。

The retry advice (o.s.i.handler.advice.RequestHandlerRetryAdvice) leverages the rich retry mechanisms provided by the Spring Retry project. The core component of spring-retry is the RetryTemplate, which allows configuration of sophisticated retry scenarios, including RetryPolicy and BackoffPolicy strategies (with a number of implementations) as well as a RecoveryCallback strategy to determine the action to take when retries are exhausted.

Stateless Retry

Stateless retry is the case where the retry activity is handled entirely within the advice. The thread pauses (if configured to do so) and retries the action.

Stateful Retry

Stateful retry is the case where the retry state is managed within the advice but where an exception is thrown and the caller resubmits the request. An example for stateful retry is when we want the message originator (for example,JMS) to be responsible for resubmitting, rather than performing it on the current thread. Stateful retry needs some mechanism to detect a retried submission.

有关 `spring-retry`的更多信息,请参见 the project’s JavadocSpring Batch的参考文档,其中 `spring-retry`就是源自该文档。

For more information on spring-retry, see the project’s Javadoc and the reference documentation for Spring Batch, where spring-retry originated.

默认的退避行为是不退避。立即尝试重试。使用导致线程在尝试之间暂停的退避策略可能会导致性能问题,包括过度使用内存和线程饥饿。在高容量环境中,应谨慎使用退避策略。

The default back off behavior is to not back off. Retries are attempted immediately. Using a back off policy that causes threads to pause between attempts may cause performance issues, including excessive memory use and thread starvation. In high-volume environments, back off policies should be used with caution.

Configuring the Retry Advice

本节中的示例使用始终抛出异常的以下 <service-activator>

The examples in this section use the following <service-activator> that always throws an exception:

public class FailingService {

    public void service(String message) {
        throw new RuntimeException("error");
    }
}
Simple Stateless Retry

The default RetryTemplate has a SimpleRetryPolicy which tries three times. There is no BackOffPolicy, so the three attempts are made back-to-back-to-back with no delay between attempts. There is no RecoveryCallback, so the result is to throw the exception to the caller after the final failed retry occurs. In a Spring Integration environment, this final exception might be handled by using an error-channel on the inbound endpoint. The following example uses RetryTemplate and shows its DEBUG output:

<int:service-activator input-channel="input" ref="failer" method="service">
    <int:request-handler-advice-chain>
        <bean class="o.s.i.handler.advice.RequestHandlerRetryAdvice"/>
    </int:request-handler-advice-chain>
</int:service-activator>

DEBUG [task-scheduler-2]preSend on channel 'input', message: [Payload=...]
DEBUG [task-scheduler-2]Retry: count=0
DEBUG [task-scheduler-2]Checking for rethrow: count=1
DEBUG [task-scheduler-2]Retry: count=1
DEBUG [task-scheduler-2]Checking for rethrow: count=2
DEBUG [task-scheduler-2]Retry: count=2
DEBUG [task-scheduler-2]Checking for rethrow: count=3
DEBUG [task-scheduler-2]Retry failed last attempt: count=3
Simple Stateless Retry with Recovery

The following example adds a RecoveryCallback to the preceding example and uses an ErrorMessageSendingRecoverer to send an ErrorMessage to a channel:

<int:service-activator input-channel="input" ref="failer" method="service">
    <int:request-handler-advice-chain>
        <bean class="o.s.i.handler.advice.RequestHandlerRetryAdvice">
            <property name="recoveryCallback">
                <bean class="o.s.i.handler.advice.ErrorMessageSendingRecoverer">
                    <constructor-arg ref="myErrorChannel" />
                </bean>
            </property>
        </bean>
    </int:request-handler-advice-chain>
</int:service-activator>

DEBUG [task-scheduler-2]preSend on channel 'input', message: [Payload=...]
DEBUG [task-scheduler-2]Retry: count=0
DEBUG [task-scheduler-2]Checking for rethrow: count=1
DEBUG [task-scheduler-2]Retry: count=1
DEBUG [task-scheduler-2]Checking for rethrow: count=2
DEBUG [task-scheduler-2]Retry: count=2
DEBUG [task-scheduler-2]Checking for rethrow: count=3
DEBUG [task-scheduler-2]Retry failed last attempt: count=3
DEBUG [task-scheduler-2]Sending ErrorMessage :failedMessage:[Payload=...]
Stateless Retry with Customized Policies, and Recovery

For more sophistication, we can provide the advice with a customized RetryTemplate. This example continues to use the SimpleRetryPolicy but increases the attempts to four. It also adds an ExponentialBackoffPolicy where the first retry waits one second, the second waits five seconds and the third waits 25 (for four attempts in all). The following listing shows the example and its DEBUG output:

<int:service-activator input-channel="input" ref="failer" method="service">
    <int:request-handler-advice-chain>
        <bean class="o.s.i.handler.advice.RequestHandlerRetryAdvice">
            <property name="recoveryCallback">
                <bean class="o.s.i.handler.advice.ErrorMessageSendingRecoverer">
                    <constructor-arg ref="myErrorChannel" />
                </bean>
            </property>
            <property name="retryTemplate" ref="retryTemplate" />
        </bean>
    </int:request-handler-advice-chain>
</int:service-activator>

<bean id="retryTemplate" class="org.springframework.retry.support.RetryTemplate">
    <property name="retryPolicy">
        <bean class="org.springframework.retry.policy.SimpleRetryPolicy">
            <property name="maxAttempts" value="4" />
        </bean>
    </property>
    <property name="backOffPolicy">
        <bean class="org.springframework.retry.backoff.ExponentialBackOffPolicy">
            <property name="initialInterval" value="1000" />
            <property name="multiplier" value="5.0" />
            <property name="maxInterval" value="60000" />
        </bean>
    </property>
</bean>

27.058 DEBUG [task-scheduler-1]preSend on channel 'input', message: [Payload=...]
27.071 DEBUG [task-scheduler-1]Retry: count=0
27.080 DEBUG [task-scheduler-1]Sleeping for 1000
28.081 DEBUG [task-scheduler-1]Checking for rethrow: count=1
28.081 DEBUG [task-scheduler-1]Retry: count=1
28.081 DEBUG [task-scheduler-1]Sleeping for 5000
33.082 DEBUG [task-scheduler-1]Checking for rethrow: count=2
33.082 DEBUG [task-scheduler-1]Retry: count=2
33.083 DEBUG [task-scheduler-1]Sleeping for 25000
58.083 DEBUG [task-scheduler-1]Checking for rethrow: count=3
58.083 DEBUG [task-scheduler-1]Retry: count=3
58.084 DEBUG [task-scheduler-1]Checking for rethrow: count=4
58.084 DEBUG [task-scheduler-1]Retry failed last attempt: count=4
58.086 DEBUG [task-scheduler-1]Sending ErrorMessage :failedMessage:[Payload=...]
Namespace Support for Stateless Retry

Starting with version 4.0, the preceding configuration can be greatly simplified, thanks to the namespace support for the retry advice, as the following example shows:

<int:service-activator input-channel="input" ref="failer" method="service">
    <int:request-handler-advice-chain>
        <ref bean="retrier" />
    </int:request-handler-advice-chain>
</int:service-activator>

<int:handler-retry-advice id="retrier" max-attempts="4" recovery-channel="myErrorChannel">
    <int:exponential-back-off initial="1000" multiplier="5.0" maximum="60000" />
</int:handler-retry-advice>

在前面的示例中,建议被定义为顶级 Bean,以便在多个 request-handler-advice-chain 实例中使用。你也可以直接在链中定义建议,如下面的示例所示:

In the preceding example, the advice is defined as a top-level bean so that it can be used in multiple request-handler-advice-chain instances. You can also define the advice directly within the chain, as the following example shows:

<int:service-activator input-channel="input" ref="failer" method="service">
    <int:request-handler-advice-chain>
        <int:retry-advice id="retrier" max-attempts="4" recovery-channel="myErrorChannel">
            <int:exponential-back-off initial="1000" multiplier="5.0" maximum="60000" />
        </int:retry-advice>
    </int:request-handler-advice-chain>
</int:service-activator>

<handler-retry-advice> 可以有 <fixed-back-off><exponential-back-off> 子元素,或没有子元素。没有子元素的 <handler-retry-advice> 不使用后退。如果没有 recovery-channel,则重试耗尽时会抛出异常。名称空间只能与无状态重试一起使用。

A <handler-retry-advice> can have a <fixed-back-off> or <exponential-back-off> child element or have no child element. A <handler-retry-advice> with no child element uses no back off. If there is no recovery-channel, the exception is thrown when retries are exhausted. The namespace can only be used with stateless retry.

对于更复杂的环境(自定义策略等),请使用普通的 <bean> 定义。

For more complex environments (custom policies etc.), use normal <bean> definitions.

Simple Stateful Retry with Recovery

To make retry stateful, we need to provide the advice with a RetryStateGenerator implementation. This class is used to identify a message as being a resubmission so that the RetryTemplate can determine the current state of retry for this message. The framework provides a SpelExpressionRetryStateGenerator, which determines the message identifier by using a SpEL expression. This example again uses the default policies (three attempts with no back off). As with stateless retry, these policies can be customized. The following listing shows the example and its DEBUG output:

<int:service-activator input-channel="input" ref="failer" method="service">
    <int:request-handler-advice-chain>
        <bean class="o.s.i.handler.advice.RequestHandlerRetryAdvice">
            <property name="retryStateGenerator">
                <bean class="o.s.i.handler.advice.SpelExpressionRetryStateGenerator">
                    <constructor-arg value="headers['jms_messageId']" />
                </bean>
            </property>
            <property name="recoveryCallback">
                <bean class="o.s.i.handler.advice.ErrorMessageSendingRecoverer">
                    <constructor-arg ref="myErrorChannel" />
                </bean>
            </property>
        </bean>
    </int:request-handler-advice-chain>
</int:service-activator>

24.351 DEBUG [Container#0-1]preSend on channel 'input', message: [Payload=...]
24.368 DEBUG [Container#0-1]Retry: count=0
24.387 DEBUG [Container#0-1]Checking for rethrow: count=1
24.387 DEBUG [Container#0-1]Rethrow in retry for policy: count=1
24.387 WARN  [Container#0-1]failure occurred in gateway sendAndReceive
org.springframework.integration.MessagingException: Failed to invoke handler
...
Caused by: java.lang.RuntimeException: foo
...
24.391 DEBUG [Container#0-1]Initiating transaction rollback on application exception
...
25.412 DEBUG [Container#0-1]preSend on channel 'input', message: [Payload=...]
25.412 DEBUG [Container#0-1]Retry: count=1
25.413 DEBUG [Container#0-1]Checking for rethrow: count=2
25.413 DEBUG [Container#0-1]Rethrow in retry for policy: count=2
25.413 WARN  [Container#0-1]failure occurred in gateway sendAndReceive
org.springframework.integration.MessagingException: Failed to invoke handler
...
Caused by: java.lang.RuntimeException: foo
...
25.414 DEBUG [Container#0-1]Initiating transaction rollback on application exception
...
26.418 DEBUG [Container#0-1]preSend on channel 'input', message: [Payload=...]
26.418 DEBUG [Container#0-1]Retry: count=2
26.419 DEBUG [Container#0-1]Checking for rethrow: count=3
26.419 DEBUG [Container#0-1]Rethrow in retry for policy: count=3
26.419 WARN  [Container#0-1]failure occurred in gateway sendAndReceive
org.springframework.integration.MessagingException: Failed to invoke handler
...
Caused by: java.lang.RuntimeException: foo
...
26.420 DEBUG [Container#0-1]Initiating transaction rollback on application exception
...
27.425 DEBUG [Container#0-1]preSend on channel 'input', message: [Payload=...]
27.426 DEBUG [Container#0-1]Retry failed last attempt: count=3
27.426 DEBUG [Container#0-1]Sending ErrorMessage :failedMessage:[Payload=...]

如果你将前面的示例与无状态示例进行比较,你可以看到,对于有状态重试,异常在每次失败时都会抛出给调用者。

If you compare the preceding example with the stateless examples, you can see that, with stateful retry, the exception is thrown to the caller on each failure.

Exception Classification for Retry

Spring Retry has a great deal of flexibility for determining which exceptions can invoke retry. The default configuration retries for all exceptions and the exception classifier looks at the top-level exception. If you configure it to, say, retry only on MyException and your application throws a SomeOtherException where the cause is a MyException, retry does not occur.

自 Spring Retry 1.0.3 以来,BinaryExceptionClassifier 有一个名为 traverseCauses 的属性(默认值为 false)。当为 true 时,它会遍历异常原因,直到找到匹配项或遍历完原因。

Since Spring Retry 1.0.3, the BinaryExceptionClassifier has a property called traverseCauses (the default is false). When true, it traverses exception causes until it finds a match or runs out of causes traversing.

要在重试中使用此分类器,请使用采用最大尝试次数、Exception 对象的 MaptraverseCauses 布尔值构造函数创建的 SimpleRetryPolicy。然后你可以将此策略注入到 RetryTemplate 中。

To use this classifier for retry, use a SimpleRetryPolicy created with the constructor that takes the max attempts, the Map of Exception objects, and the traverseCauses boolean. Then you can inject this policy into the RetryTemplate.

这种情况下需要 traverseCauses,因为用户异常可能包装在 MessagingException 中。

traverseCauses is required in this case because user exceptions may be wrapped in a MessagingException.

Circuit Breaker Advice

断路器模式的总体思路是,如果服务当前不可用,则不要浪费时间(和资源)尝试使用它。o.s.i.handler.advice.RequestHandlerCircuitBreakerAdvice 实现了这一模式。当断路器处于闭合状态时,端点会尝试调用服务。如果连续尝试次数超过一定数量,则断路器将处于打开状态。当它处于打开状态时,新的请求“快速失败”,并且在一段时间过去之前不会尝试调用该服务。

The general idea of the circuit breaker pattern is that, if a service is not currently available, do not waste time (and resources) trying to use it. The o.s.i.handler.advice.RequestHandlerCircuitBreakerAdvice implements this pattern. When the circuit breaker is in the closed state, the endpoint attempts to invoke the service. The circuit breaker goes to the open state if a certain number of consecutive attempts fail. When it is in the open state, new requests “fail fast” and no attempt is made to invoke the service until some time has expired.

在该时间过期时,将断路器设置为半开状态。在此状态下,如果单次尝试失败,则断路器将立即进入打开状态。如果尝试成功,则断路器进入关闭状态,在这种情况下,直到再次发生配置的连续故障次数,才进入打开状态。任何成功的尝试都会将状态重置为零故障,以确定断路器何时可能再次进入打开状态。

When that time has expired, the circuit breaker is set to the half-open state. When in this state, if even a single attempt fails, the breaker immediately goes to the open state. If the attempt succeeds, the breaker goes to the closed state, in which case it does not go to the open state again until the configured number of consecutive failures again occur. Any successful attempt resets the state to zero failures for the purpose of determining when the breaker might go to the open state again.

通常,此建议可用于可能需要一段时间才能失败(例如尝试建立网络连接的超时)的外部服务。

Typically, this advice might be used for external services, where it might take some time to fail (such as a timeout attempting to make a network connection).

RequestHandlerCircuitBreakerAdvice 有两个属性:thresholdhalfOpenAfterthreshold 属性表示断路器打开之前需要发生的连续故障次数。默认为 5halfOpenAfter 属性表示上次故障后断路器在尝试另一个请求之前等待的时间。默认值为 1000 毫秒。

The RequestHandlerCircuitBreakerAdvice has two properties: threshold and halfOpenAfter. The threshold property represents the number of consecutive failures that need to occur before the breaker goes open. It defaults to 5. The halfOpenAfter property represents the time after the last failure that the breaker waits before attempting another request. The default is 1000 milliseconds.

以下示例配置了断路器并显示了其 DEBUGERROR 输出:

The following example configures a circuit breaker and shows its DEBUG and ERROR output:

<int:service-activator input-channel="input" ref="failer" method="service">
    <int:request-handler-advice-chain>
        <bean class="o.s.i.handler.advice.RequestHandlerCircuitBreakerAdvice">
            <property name="threshold" value="2" />
            <property name="halfOpenAfter" value="12000" />
        </bean>
    </int:request-handler-advice-chain>
</int:service-activator>

05.617 DEBUG [task-scheduler-1]preSend on channel 'input', message: [Payload=...]
05.638 ERROR [task-scheduler-1]org.springframework.messaging.MessageHandlingException: java.lang.RuntimeException: foo
...
10.598 DEBUG [task-scheduler-2]preSend on channel 'input', message: [Payload=...]
10.600 ERROR [task-scheduler-2]org.springframework.messaging.MessageHandlingException: java.lang.RuntimeException: foo
...
15.598 DEBUG [task-scheduler-3]preSend on channel 'input', message: [Payload=...]
15.599 ERROR [task-scheduler-3]org.springframework.messaging.MessagingException: Circuit Breaker is Open for ServiceActivator
...
20.598 DEBUG [task-scheduler-2]preSend on channel 'input', message: [Payload=...]
20.598 ERROR [task-scheduler-2]org.springframework.messaging.MessagingException: Circuit Breaker is Open for ServiceActivator
...
25.598 DEBUG [task-scheduler-5]preSend on channel 'input', message: [Payload=...]
25.601 ERROR [task-scheduler-5]org.springframework.messaging.MessageHandlingException: java.lang.RuntimeException: foo
...
30.598 DEBUG [task-scheduler-1]preSend on channel 'input', message: [Payload=foo...]
30.599 ERROR [task-scheduler-1]org.springframework.messaging.MessagingException: Circuit Breaker is Open for ServiceActivator

在前一个示例中,阈值设置为 2halfOpenAfter 设置为 12 秒。每 5 秒就会到达一个新请求。前两次尝试调用了服务。第三次和第四次尝试失败,并抛出一个异常,表明断路器已打开。第五次请求已尝试,因为该请求在最后一次失败后 15 秒。第六次尝试立即失败,因为断路器立即进入打开状态。

In the preceding example, the threshold is set to 2 and halfOpenAfter is set to 12 seconds. A new request arrives every 5 seconds. The first two attempts invoked the service. The third and fourth failed with an exception indicating that the circuit breaker is open. The fifth request was attempted because the request was 15 seconds after the last failure. The sixth attempt fails immediately because the breaker immediately went to open.

Expression Evaluating Advice

最后提供的建议类是 o.s.i.handler.advice.ExpressionEvaluatingRequestHandlerAdvice。此建议比其他两条建议更通用。它提供了一种在发送到端点的原始入站消息上求值表达式的机制。可以求值单独的表达式,以便在成功或失败后求值。可以选择将包含求值结果和输入消息的消息发送到消息通道。

The final supplied advice class is the o.s.i.handler.advice.ExpressionEvaluatingRequestHandlerAdvice. This advice is more general than the other two advices. It provides a mechanism to evaluate an expression on the original inbound message sent to the endpoint. Separate expressions are available to be evaluated, after either success or failure. Optionally, a message containing the evaluation result, together with the input message, can be sent to a message channel.

此建议的典型用例可能是 <ftp:outbound-channel-adapter/>,可能在传输成功时将文件移至一个目录,或在传输失败时将文件移至另一个目录:

A typical use case for this advice might be with an <ftp:outbound-channel-adapter/>, perhaps to move the file to one directory if the transfer was successful or to another directory if it fails:

该建议具有在成功时设置表达式的属性、失败时的表达式以及每个表达式的相应通道。对于成功的情况,发送到 successChannel 的消息是 AdviceMessage,有效负载是表达式求值的结果。一个名为 inputMessage 的附加属性包含发送到处理程序的原始消息。发送到 failureChannel(当处理程序抛出异常时)的消息是带有 MessageHandlingExpressionEvaluatingAdviceException 有效负载的 ErrorMessage。像所有 MessagingException 实例一样,此有效负载具有 failedMessagecause 属性,以及一个名为 evaluationResult 的附加属性,其中包含表达式求值的結果。

The advice has properties to set an expression when successful, an expression for failures, and corresponding channels for each. For the successful case, the message sent to the successChannel is an AdviceMessage, with the payload being the result of the expression evaluation. An additional property, called inputMessage, contains the original message sent to the handler. A message sent to the failureChannel (when the handler throws an exception) is an ErrorMessage with a payload of MessageHandlingExpressionEvaluatingAdviceException. Like all MessagingException instances, this payload has failedMessage and cause properties, as well as an additional property called evaluationResult, which contains the result of the expression evaluation.

从版本 5.1.3 开始,如果配置了通道但未提供表达式,则使用默认表达式对消息 payload 进行评估。

Starting with version 5.1.3, if channels are configured, but expressions are not provided, the default expression is used to evaluate to the payload of the message.

当在建议的范围内抛出异常时,默认情况下,该异常将在任何 failureExpression 求值后抛给调用者。如果您希望禁止抛出异常,请将 trapException 属性设置为 true。以下建议显示如何使用 Java DSL 配置 advice

When an exception is thrown in the scope of the advice, by default, that exception is thrown to the caller after any failureExpression is evaluated. If you wish to suppress throwing the exception, set the trapException property to true. The following advice shows how to configure an advice with Java DSL:

@SpringBootApplication
public class EerhaApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(EerhaApplication.class, args);
        MessageChannel in = context.getBean("advised.input", MessageChannel.class);
        in.send(new GenericMessage<>("good"));
        in.send(new GenericMessage<>("bad"));
        context.close();
    }

    @Bean
    public IntegrationFlow advised() {
        return f -> f.<String>handle((payload, headers) -> {
            if (payload.equals("good")) {
                return null;
            }
            else {
                throw new RuntimeException("some failure");
            }
        }, c -> c.advice(expressionAdvice()));
    }

    @Bean
    public Advice expressionAdvice() {
        ExpressionEvaluatingRequestHandlerAdvice advice = new ExpressionEvaluatingRequestHandlerAdvice();
        advice.setSuccessChannelName("success.input");
        advice.setOnSuccessExpressionString("payload + ' was successful'");
        advice.setFailureChannelName("failure.input");
        advice.setOnFailureExpressionString(
                "payload + ' was bad, with reason: ' + #exception.cause.message");
        advice.setTrapException(true);
        return advice;
    }

    @Bean
    public IntegrationFlow success() {
        return f -> f.handle(System.out::println);
    }

    @Bean
    public IntegrationFlow failure() {
        return f -> f.handle(System.out::println);
    }

}

Rate Limiter Advice

速率限制器建议(RateLimiterRequestHandlerAdvice)确保端点不会因请求而过载。当速率限制遭到破坏时,请求将变为已阻止状态。

The Rate Limiter advice (RateLimiterRequestHandlerAdvice) allows to ensure that an endpoint does not get overloaded with requests. When the rate limit is breached the request will go in a blocked state.

此建议的典型用例可能是外部服务提供程序不允许每分钟超过 n 个请求。

A typical use case for this advice might be an external service provider not allowing more than n number of request per minute.

`RateLimiterRequestHandlerAdvice`实现完全基于 Resilience4j项目,并且需要 `RateLimiter`或 `RateLimiterConfig`注入。还可以配置默认值和/或自定义名称。

The RateLimiterRequestHandlerAdvice implementation is fully based on the Resilience4j project and requires either RateLimiter or RateLimiterConfig injections. Can also be configured with defaults and/or custom name.

以下示例配置了速率限制器建议,每 1 秒一个请求:

The following example configures a rate limiter advice with one request per 1 second:

@Bean
public RateLimiterRequestHandlerAdvice rateLimiterRequestHandlerAdvice() {
    return new RateLimiterRequestHandlerAdvice(RateLimiterConfig.custom()
            .limitRefreshPeriod(Duration.ofSeconds(1))
            .limitForPeriod(1)
            .build());
}

@ServiceActivator(inputChannel = "requestChannel", outputChannel = "resultChannel",
		adviceChain = "rateLimiterRequestHandlerAdvice")
public String handleRequest(String payload) {
    ...
}

Caching Advice

从 5.2 版本开始,引入了 CacheRequestHandlerAdvice。它基于 Spring Framework 中的缓存抽象,并与 @Caching 注释家族提供的概念和功能保持一致。内部逻辑基于 CacheAspectSupport 扩展,其中缓存操作代理围绕带有请求 Message<?> 作为参数的 AbstractReplyProducingMessageHandler.RequestHandler.handleRequestMessage 方法进行。可以使用 SpEL 表达式或 Function 来配置此建议以评估缓存键。请求 Message<?> 可用作 SpEL 评估上下文的根对象或作为 Function 输入参数。默认情况下,请求消息的 payload 用于缓存键。必须使用 cacheNames 配置 CacheRequestHandlerAdvice,当默认缓存操作为 CacheableOperation 或一组任意 CacheOperation 时。可以分别配置每个 CacheOperation,也可以具有共享选项,例如可以从 CacheRequestHandlerAdvice 配置中重用的 CacheManagerCacheResolverCacheErrorHandler。此配置功能类似于 Spring Framework 的 @CacheConfig@Caching 注释组合。如果没有提供 CacheManager,则默认情况下从 CacheAspectSupport 中的 BeanFactory 解析单个 bean。

Starting with version 5.2, the CacheRequestHandlerAdvice has been introduced. It is based on the caching abstraction in Spring Framework and aligned with the concepts and functionality provided by the @Caching annotation family. The logic internally is based on the CacheAspectSupport extension, where proxying for caching operations is done around the AbstractReplyProducingMessageHandler.RequestHandler.handleRequestMessage method with the request Message<?> as the argument. This advice can be configured with a SpEL expression or a Function to evaluate a cache key. The request Message<?> is available as the root object for the SpEL evaluation context, or as the Function input argument. By default, the payload of the request message is used for the cache key. The CacheRequestHandlerAdvice must be configured with cacheNames, when a default cache operation is a CacheableOperation, or with a set of any arbitrary CacheOperation s. Every CacheOperation can be configured separately or have shared options, like a CacheManager, CacheResolver and CacheErrorHandler, can be reused from the CacheRequestHandlerAdvice configuration. This configuration functionality is similar to Spring Framework’s @CacheConfig and @Caching annotation combination. If a CacheManager is not provided, a single bean is resolved by default from the BeanFactory in the CacheAspectSupport.

以下示例配置了两个具有不同缓存操作集的建议:

The following example configures two advices with different set of caching operations:

@Bean
public CacheRequestHandlerAdvice cacheAdvice() {
    CacheRequestHandlerAdvice cacheRequestHandlerAdvice = new CacheRequestHandlerAdvice(TEST_CACHE);
    cacheRequestHandlerAdvice.setKeyExpressionString("payload");
    return cacheRequestHandlerAdvice;
}

@Transformer(inputChannel = "transformerChannel", outputChannel = "nullChannel", adviceChain = "cacheAdvice")
public Object transform(Message<?> message) {
    ...
}

@Bean
public CacheRequestHandlerAdvice cachePutAndEvictAdvice() {
    CacheRequestHandlerAdvice cacheRequestHandlerAdvice = new CacheRequestHandlerAdvice();
    cacheRequestHandlerAdvice.setKeyExpressionString("payload");
    CachePutOperation.Builder cachePutBuilder = new CachePutOperation.Builder();
    cachePutBuilder.setCacheName(TEST_PUT_CACHE);
    CacheEvictOperation.Builder cacheEvictBuilder = new CacheEvictOperation.Builder();
    cacheEvictBuilder.setCacheName(TEST_CACHE);
    cacheRequestHandlerAdvice.setCacheOperations(cachePutBuilder.build(), cacheEvictBuilder.build());
    return cacheRequestHandlerAdvice;
}

@ServiceActivator(inputChannel = "serviceChannel", outputChannel = "nullChannel",
    adviceChain = "cachePutAndEvictAdvice")
public Message<?> service(Message<?> message) {
    ...
}