title: Launch Spring Batch Job from Spring Integration
body: <p>I need to download a file from remote SFTP server and process them using spring batch. I already implemented code using Spring Integration to download files. But I can't launch Spring Batch job from spring integration components. I have following code:</p>
  
  <pre><code>    @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&lt;File&gt; sftpMessageSource() {
      final SftpInboundFileSynchronizingMessageSource source =
              new SftpInboundFileSynchronizingMessageSource(sftpInboundFileSynchronizer());
      source.setLocalDirectory(new File(OUTPUT_DIR));
      source.setAutoCreateLocalDirectory(true);
      source.setLocalFilter(new AcceptOnceFileListFilter&lt;&gt;());
      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");
  }
  </code></pre>
  
  <p>But this does not work (error in last method <code>adapt</code>), because not bean of type File found. I can't clue this two parts together. How to wire integration and batch processing?</p>