Spring integration Spring将XML集成到Java HowTo-同一类中具有不同参数的多个@ServiceActivator

Spring integration Spring将XML集成到Java HowTo-同一类中具有不同参数的多个@ServiceActivator,spring-integration,Spring Integration,在将Spring批处理和Spring集成从xml转换为Java的过程中,我看到了一个用于多个@ServiceActivator的类,该类在创建文件后具有不同的inputChannel和文件夹。看起来像这样: @MessageEndpoint public class ServiceActivatorClass implements InitializingBean { private Job job; private String doneFolder; private

在将Spring批处理和Spring集成从xml转换为Java的过程中,我看到了一个用于多个@ServiceActivator的类,该类在创建文件后具有不同的inputChannel和文件夹。看起来像这样:

@MessageEndpoint
public class ServiceActivatorClass implements InitializingBean {
    private Job job;
    private String doneFolder;
    private String errorFolder;
    private Database database;


public void afterPropertiesSet() throws Exception {
    Assert.notNull(job, "A Job must be provided");
    Assert.notNull(doneFolder, "done folder must be provided");
    Assert.notNull(errorFolder, "error folder must be provided");
    Assert.notNull(database, "error database must be provided");
}

@ServiceActivator
public JobLaunchRequest adapt(File file) throws NoSuchJobException, Exception {
    [method......]

    return new JobLaunchRequest(job, jobParameters);
}

}
XML配置看起来是这样的:

<si:service-activator input-channel="bean1-input"
        output-channel="job-requests" ref="bean1JobLauncher" />

<bean id="bean1JobLauncher" class="com.example.ServiceActivatorClass">
    <property name="job" ref="bean1Job" />
    <property name="doneFolder" value="${done.dir.bean1}" />
    <property name="errorFolder" value="${error.dir.bean1}" />
    <property name="database" ref="database" />

…好办法吗?

好吧,到目前为止你做的很好。可以考虑直接移动到java DSL:

@Bean
IntegrationFlow bean1Flow() {
    return IntegrationFlow.from("bean1Input")
                     .handle(bean1JobLauncher(), null)
                     .channel("jobRequestsChannel")
                     .get();
}

@Bean
IntegrationFlow bean2Flow() {
    return IntegrationFlow.from("bean2Input")
                     .handle(bean2JobLauncher(), null)
                     .channel("jobRequestsChannel")
                     .get();
}

或您可以考虑委派变体:

@ServiceActivator(inputChannel="bean1Input", outputChannel="jobRequestsChannel")
public JobLaunchRequest adapt1(File file) {
    return bean1JobLauncher().adapt(file);
}

@ServiceActivator(inputChannel="bean2Input", outputChannel="jobRequestsChannel")
public JobLaunchRequest adapt2(File file) {
    return bean2JobLauncher().adapt(file);
}
但没有其他方法可以在不同通道和不同目标组件之间分配逻辑。即使存在一定数量的复制/粘贴,也必须单独声明它们


你可以考虑使用动态流注册,但是这已经是一个不同的故事,在你的例子中看起来不是很强的要求:

谢谢。随着IntegrationFlow的引入,我必须将MessageEndpoint和ServiceActivator注释保留在类上,对吗?嗯,不一定,但将它们保留在那里不会有什么坏处。无论如何,您必须多次将您的
ServiceActivatorClass
声明为
@Bean
,以获得不同的配置选项。
@ServiceActivator
在这里仅用于方法扫描,但是如果您明确使用它的名称,它就不再重要了。
@Bean
IntegrationFlow bean1Flow() {
    return IntegrationFlow.from("bean1Input")
                     .handle(bean1JobLauncher(), null)
                     .channel("jobRequestsChannel")
                     .get();
}

@Bean
IntegrationFlow bean2Flow() {
    return IntegrationFlow.from("bean2Input")
                     .handle(bean2JobLauncher(), null)
                     .channel("jobRequestsChannel")
                     .get();
}
@ServiceActivator(inputChannel="bean1Input", outputChannel="jobRequestsChannel")
public JobLaunchRequest adapt1(File file) {
    return bean1JobLauncher().adapt(file);
}

@ServiceActivator(inputChannel="bean2Input", outputChannel="jobRequestsChannel")
public JobLaunchRequest adapt2(File file) {
    return bean2JobLauncher().adapt(file);
}