Spring 集成如何将控制总线与 JavaConfig 一起使用,无 DSL

Spring Integration how to use Control Bus with JavaConfig, no DSL

我在 Spring 集成和控制总线方面遇到了一些问题。我需要在 InboundChannelAdapter 上关闭自动启动。但是,当我这样做时,我无法让 ControlBus 启动通道适配器。

我在网上搜索了答案,但大多数示例都使用 XML 配置。

这是我的全部代码:

package com.example.springintegrationdemo;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.integration.annotation.InboundChannelAdapter;
import org.springframework.integration.annotation.Poller;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.config.EnableIntegration;
import org.springframework.integration.config.ExpressionControlBusFactoryBean;
import org.springframework.integration.core.MessageSource;
import org.springframework.integration.file.FileReadingMessageSource;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.support.GenericMessage;

import java.io.File;

@SpringBootApplication
@EnableIntegration
public class SpringIntegrationDemoApplication {

    @Bean
    public MessageChannel fileChannel() {
        return new DirectChannel();
    }

    @Bean(name = "fileMessageSource")
    @InboundChannelAdapter(channel = "fileChannel", poller = @Poller(fixedDelay = "1000"),autoStartup = "false")
    public MessageSource<File> fileMessageSource() {
        FileReadingMessageSource fileReadingMessageSource = new FileReadingMessageSource();
        fileReadingMessageSource.setDirectory(new File("lz"));
        return fileReadingMessageSource;
    }

    @Bean
    @ServiceActivator(inputChannel = "fileChannel")
    public MessageHandler messageHandler() {
        MessageHandler messageHandler = message -> {
            File f = (File) message.getPayload();
            System.out.println(f.getAbsolutePath());
        };
        return messageHandler;
    }

    @Bean
    MessageChannel controlChannel() {
        return new DirectChannel();
    }

    @Bean
    @ServiceActivator(inputChannel = "controlChannel")
    ExpressionControlBusFactoryBean controlBus() {
        ExpressionControlBusFactoryBean expressionControlBusFactoryBean = new ExpressionControlBusFactoryBean();
        return expressionControlBusFactoryBean;
    }

    @Bean
    CommandLineRunner commandLineRunner(@Qualifier("controlChannel") MessageChannel controlChannel) {
        return (String[] args)-> {
            System.out.println("Starting incoming file adapter: ");
            boolean sent = controlChannel.send(new GenericMessage<>("@fileMessageSource.start()"));
            System.out.println("Sent control message successfully? " + sent);
            while(System.in.available() == 0) {
                Thread.sleep(50);
            }
        };
    }

    public static void main(String[] args) {
        SpringApplication.run(SpringIntegrationDemoApplication.class, args);
    }

}

消息已成功发送到控制总线组件,但入站通道适配器从未启动。

如有任何帮助,我将不胜感激。

谢谢, 戴夫

看这里:https://docs.spring.io/spring-integration/docs/current/reference/html/configuration.html#annotations_on_beans

fileMessageSource bean 名称恰好是 FileReadingMessageSource。从 InboundChannelAdapter 创建的 SourcePollingChannelAdapter 具有以下 bean 名称:springIntegrationDemoApplication.fileMessageSource.inboundChannelAdapter.

@EndpointId可以帮助您简化它。

换句话说:您的配置一切正常,唯一的问题是您没有使用正确的端点 ID 来启动 SourcePollingChannelAdapter