在将Spring Batch和Spring Integration从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" />
<si:service-activator input-channel="bean2-input"
output-channel="job-requests" ref="bean2JobLauncher" />
<bean id="bean2JobLauncher" class="com.example.ServiceActivatorClass">
<property name="job" ref="bean2Job" />
<property name="doneFolder" value="${done.dir.bean2}" />
<property name="errorFolder" value="${error.dir.bean2}" />
<property name="database" ref="database" />
X次...... 从ServiceActivatorClass中删除@MessageEnpoint和@ServiceActivator并为Java Config做类似的事情......:
@Bean
@ServiceActivator(inputChannel="bean1Input", outputChannel="jobRequestsChannel")
public MessageHandler bean1JobLauncher() {
ServiceActivatorClass bean1JobLauncher = new ServiceActivatorClass();
bean1JobLauncher.setJob(manifestJob());
bean1JobLauncher.setDoneFolder(manifestDoneDir);
bean1JobLauncher.setErrorFolder(manifestErrorDir);
bean1JobLauncher.setDatabase(database);
return new MethodInvokingMessageHandler(bean1JobLauncher, "adapt");
}
......好方法?
答案 0 :(得分:1)
嗯,到目前为止你做的很好。您可以考虑直接迁移到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);
}
但是没有其他方法可以在不同的通道和不同的目标组件之间分配逻辑。你必须单独声明它们,即使有一定数量的复制/粘贴。
您可以考虑使用动态流注册,但这已经是一个不同的故事,在您的案例中看起来不是一个强烈要求:https://docs.spring.io/spring-integration/docs/5.0.3.RELEASE/reference/html/java-dsl.html#java-dsl-runtime-flows