Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从Spring集成启动Spring批处理作业_Java_Spring_Spring Integration_Spring Batch - Fatal编程技术网

Java 从Spring集成启动Spring批处理作业

Java 从Spring集成启动Spring批处理作业,java,spring,spring-integration,spring-batch,Java,Spring,Spring Integration,Spring Batch,我需要从远程SFTP服务器下载一个文件,并使用SpringBatch处理它们。我已经实现了使用Spring集成下载文件的代码。但我无法从Spring集成组件启动Spring批处理作业。我有以下代码: @Autowired private JobLauncher jobLauncher; public String OUTPUT_DIR = "temp_dir"; @Value("${sftp.remote.host}") private String sftpRemoteHost;

我需要从远程SFTP服务器下载一个文件,并使用SpringBatch处理它们。我已经实现了使用Spring集成下载文件的代码。但我无法从Spring集成组件启动Spring批处理作业。我有以下代码:

    @Autowired
private JobLauncher jobLauncher;

public String OUTPUT_DIR = "temp_dir";

@Value("${sftp.remote.host}")
private String sftpRemoteHost;

@Value("${sftp.remote.user}")
private String sftpUsername;

@Value("${sftp.remote.password}")
private String sftpPassword;

@Value("${sftp.remote.folder}")
private String sftpFolder;

@Bean
public DefaultSftpSessionFactory sftpSessionFactory() {
    final DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory();
    factory.setHost(sftpRemoteHost);
    factory.setAllowUnknownKeys(true);
    factory.setUser(sftpUsername);
    factory.setPassword(sftpPassword);
    return factory;
}

@Bean
public SftpInboundFileSynchronizer sftpInboundFileSynchronizer() {
    final SftpInboundFileSynchronizer fileSynchronizer = new SftpInboundFileSynchronizer(sftpSessionFactory());
    fileSynchronizer.setDeleteRemoteFiles(false);
    fileSynchronizer.setRemoteDirectory(sftpFolder);
    fileSynchronizer.setFilter(new SftpSimplePatternFileListFilter("*.csv"));
    return fileSynchronizer;
}

@Bean
@InboundChannelAdapter(channel = "sftpChannel", poller = @Poller(fixedDelay = "5000"))
public MessageSource<File> sftpMessageSource() {
    final SftpInboundFileSynchronizingMessageSource source =
            new SftpInboundFileSynchronizingMessageSource(sftpInboundFileSynchronizer());
    source.setLocalDirectory(new File(OUTPUT_DIR));
    source.setAutoCreateLocalDirectory(true);
    source.setLocalFilter(new AcceptOnceFileListFilter<>());
    return source;
}

@Bean
@ServiceActivator(inputChannel = "sftpChannel")
public MessageHandler handler() {
    final FileWritingMessageHandler handler = new FileWritingMessageHandler(new File(OUTPUT_DIR));
    handler.setFileExistsMode(FileExistsMode.REPLACE);
    handler.setExpectReply(true);
    handler.setOutputChannelName("parse-csv-channel");
    return handler;
}

@ServiceActivator(inputChannel = "parse-csv-channel", outputChannel = "job-channel")
public JobLaunchRequest adapt(final File file) throws Exception {
    final JobParameters jobParameters = new JobParametersBuilder().addString(
            "input.file", file.getAbsolutePath()).toJobParameters();
    return new JobLaunchRequest(batchConfiguration.job(), jobParameters);
}

@ServiceActivator(inputChannel = "job-channel", outputChannel = "finish")
public JobLaunchingMessageHandler jobHandler(JobLaunchRequest request) throws JobExecutionException {
    return new JobLaunchingMessageHandler(jobLauncher);//.launch(request);
}

@ServiceActivator(inputChannel = "finish")
public void finish() {
    System.out.println("FINISH");
}
@Autowired
私有JobLauncher JobLauncher;
公共字符串输出\u DIR=“temp\u DIR”;
@值(${sftp.remote.host}”)
私有字符串主机;
@值(${sftp.remote.user}”)
私用字符串;
@值(${sftp.remote.password}”)
私有字符串;
@值(${sftp.remote.folder}”)
私人字符串文件夹;
@豆子
公共默认sftpSessionFactory sftpSessionFactory(){
最终DefaultSftpSessionFactory=新的DefaultSftpSessionFactory();
setHost(sftpRemoteHost);
工厂。setAllowUnknownKeys(真);
工厂设置用户(sftpUsername);
出厂设置密码(sftpassword);
返回工厂;
}
@豆子
公共SftpInboundFileSynchronizer SftpInboundFileSynchronizer(){
final SftpInboundFileSynchronizer fileSynchronizer=新的SftpInboundFileSynchronizer(sftpSessionFactory());
fileSynchronizer.setDeleteRemoteFiles(false);
setRemoteDirectory(sftpFolder);
setFilter(新的SftpSimplePatternFileListFilter(“*.csv”);
返回文件同步器;
}
@豆子
@InboundChannelAdapter(channel=“sftpChannel”,poller=@poller(fixedDelay=“5000”))
public MessageSource sftpMessageSource(){
最终SftpInboundFileSynchronizingMessageSource源=
新的SftpInboundFileSynchronizingMessageSource(sftpInboundFileSynchronizer());
setLocalDirectory(新文件(OUTPUT_DIR));
source.setAutoCreateLocalDirectory(true);
setLocalFilter(新的AcceptOnceFileListFilter());
返回源;
}
@豆子
@ServiceActivator(inputChannel=“sftpChannel”)
public MessageHandler(){
final FileWritingMessageHandler=newfilewritingmessagehandler(新文件(OUTPUT_DIR));
setFileExistsMode(FileExistsMode.REPLACE);
handler.setExpectReply(true);
setOutputChannelName(“解析csv通道”);
返回处理程序;
}
@ServiceActivator(inputChannel=“解析csv通道”,outputChannel=“作业通道”)
public JobLaunchRequest adapt(最终文件)引发异常{
final JobParameters JobParameters=新JobParametersBuilder().addString(
“input.file”,file.getAbsolutePath()).toJobParameters();
返回新的JobLaunchRequest(batchConfiguration.job(),jobParameters);
}
@ServiceActivator(inputChannel=“作业通道”,outputChannel=“完成”)
public JobLaunchingMessageHandler jobHandler(JobLaunchRequest请求)引发JobExecutionException异常{
返回新的JobLaunchingMessageHandler(jobLauncher);//.launch(请求);
}
@ServiceActivator(inputChannel=“finish”)
公共空间整理(){
系统输出打印(“完成”);
}

但这不起作用(上一个方法中的错误
adapt
),因为找不到文件类型的bean。我不能把这两部分连在一起。如何连接集成和批处理?

您肯定需要从
adapt()
方法中删除
@Bean
注释。如果我们真的构建
MessageHandler
Bean,我们需要
@Bean
,例如
JobLaunchingMessageHandler
来接受
JobLaunchRequest
负载:

请参阅参考手册中有关消息传递批注的更多信息:

更新

@Bean
@ServiceActivator(inputChannel = "sftpChannel")
public MessageHandler handler() {
    final FileWritingMessageHandler handler = new FileWritingMessageHandler(new File(OUTPUT_DIR));
    handler.setFileExistsMode(FileExistsMode.REPLACE);
    handler.setExpectReply(true);
    handler.setOutputChannelName("parse-csv-channel");
    return handler;
}

@ServiceActivator(inputChannel = "parse-csv-channel", outputChannel = "job-channel")
public JobLaunchRequest adapt(final File file) throws Exception {
    final JobParameters jobParameters = new JobParametersBuilder().addString(
            "input.file", file.getAbsolutePath()).toJobParameters();
    return new JobLaunchRequest(batchConfiguration.job(), jobParameters);
}

@Bean
@ServiceActivator(inputChannel = "job-channel")
public JobLaunchingGateway jobHandler() {
    JobLaunchingGateway jobLaunchingGateway = new JobLaunchingGateway(jobLauncher);
    jobLaunchingGateway.setOutputChannelName("finish");
    return jobLaunchingGateway;
}

以及如何将
JobLaunchingMessageHandler
和my
FileWritingMessageHandler
结合起来?您不太清楚。您应该从
adapt()
方法中删除
@Bean
,并将
outputChannel
添加到其
@ServiceActivator
中,以将结果发送到
JobLaunchingMessageHandler
端点定义。其中一个应该与您使用的
FileWritingMessageHandler
I更新了原始问题-添加了新代码类似。正如您所提到的,我试图添加更多配置,但它不起作用。我在最后2个方法中做了调试点,但这段代码从未调用过。我做错了什么?
FileWritingMessageHandler
必须使用
setExpectReply(true)
——尽管是默认值。文件将被发送到
解析csv频道
进行
作业启动请求
转换。您的
launch()
方法是多余的。关于这件事,你有
JobLaunchingMessageHandler
。就这样,我不知道怎么了。我再次更新了代码。批处理作业不会启动,但文件下载每5秒工作一次。每次下载后,作业都不会运行。这让我发疯