spring ServiceActivator 上的集成 adviceChain 未执行建议的 beforeReceive

spring integration adviceChain on ServiceActivator is not executing the beforeReceive of an advice

我用一个带有 adviceChain 的服务激活器定义了我的轮询器,如下所示:

   @ServiceActivator(inputChannel = EVENT_CHANNEL,
            adviceChain = {"globalLockAdvice"},
            poller = @Poller(
                    maxMessagesPerPoll = "${event.poller.maxMessagesPerPoll:1}",
                    fixedDelay = "${event.poller.fixedDelay:1000}",
                    receiveTimeout = "${event.poller.receiveTimeout:1000}"))
    public void handleEventMessage(Message<String> message) throws MessagingException {
...
}

@Component
public class GlobalLockAdvice implements ReceiveMessageAdvice {

    private final LockConfiguration lockConfiguration;

    public GlobalLockAdvice(LockConfiguration lockConfiguration) {
        this.lockConfiguration = lockConfiguration;
    }

    @Override
    public boolean beforeReceive(Object source) {
        return lockConfiguration.isLeader();
    }

    @Override
    public Message<?> afterReceive(Message<?> result, Object source) {
        return result;
    }

}

但是没有调用beforeReceive。 调试时我看到 'target' 是一个 ServiceActivator class,导致跳过调用 beforeReceive:

@FunctionalInterface
public interface ReceiveMessageAdvice extends MethodInterceptor {

    @Override
    @Nullable
    default Object invoke(MethodInvocation invocation) throws Throwable {
        Object target = invocation.getThis();
        if (!(target instanceof MessageSource) && !(target instanceof PollableChannel)) {
            return invocation.proceed();
        }
...

我做错了什么?

服务激活器上的建议链建议消息处理程序,而不是轮询器轮询的通道。

要向轮询器添加建议链,您必须将轮询器定义为 PollerMetadata bean,而不是使用 @Poller 注释属性。 @Poller("metadata").

请参阅 @Poller 的 javadoc。

/**
 * Provides the {@link org.springframework.integration.scheduling.PollerMetadata} options
 * for the Messaging annotations for polled endpoints. It is an analogue of the XML
 * {@code <poller/>} element, but provides only simple attributes. If the
 * {@link org.springframework.integration.scheduling.PollerMetadata} requires more options
 * (e.g. Transactional and other Advices) or {@code initialDelay} etc, the
 * {@link org.springframework.integration.scheduling.PollerMetadata} should be configured
 * as a generic bean and its bean name can be specified as the {@code value} attribute of
 * this annotation. In that case, the other attributes are not allowed.
 * <p>
 * Non-reference attributes support Property Placeholder resolutions.