2017-09-28 5 views
1

リモートSFTPサーバからファイルをダウンロードし、Springバッチを使用して処理する必要があります。私はすでにファイルをダウンロードするためにSpring Integrationを使ってコードを実装しました。しかし、私はバネ統合コンポーネントからバネバッチジョブを起動することはできません。 File型のないBeanが見つけたので、(最後の方法adaptでエラー)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"); 
} 

は、しかし、これは動作しません:私は、次のコードを持っています。私はこの2つの部分を一緒に結びつけることはできません。統合とバッチ処理を結ぶには?

答えて

1

adapt()メソッドから@Bean注釈を削除するだけです。 MessageHandler beanを実際に構築する場合は、JobLaunchingMessageHandlerを受け入れるには、JobLaunchingMessageHandlerJobLaunchRequestのペイロードを受け入れるには、@Beanが必要です。https://docs.spring.io/spring-batch/trunk/reference/html/springBatchIntegration.html#launching-batch-jobs-through-messages

は、リファレンスマニュアルにメッセージング注釈についての詳細情報を参照してください:https://docs.spring.io/spring-integration/docs/4.3.12.RELEASE/reference/html/configuration.html#annotations_on_beans

UPDATE

@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; 
} 
+0

とどのように '' JobLaunchingMessageHandlerと私の 'FileWritingMessageHandler'の両方を組み合わせること? –

+0

あなたの懸念事項では分かりません。 'adapt()'メソッドから '@ Bean'を削除し、その結果を' JobLaunchingMessageHandler'エンドポイント定義に送るために 'OutputService'を' @ServiceActivator'に追加する必要があります。そして、あなたが 'FileWritingMessageHandler'で持っているものに似ているはずです –

+0

私はオリジナルの質問を更新しました - 新しいコードを追加しました。私はあなたが述べたように、より多くの設定を追加しようとしましたが、うまくいきません。最後の2つのメソッドでデバッグポイントを作成しましたが、このコードは決して呼び出されませんでした。私は間違っているの? –

関連する問題