title: How to process two files paired with Apache Camel
body: <p>I am building applications using Apache Camel.
  In this application, two files with the same name as the xml file and extension of jpg are placed in a specific directory.
  We will process this file using Apache Camel's file2 component.
  And I'm using Apache Camel Version 2.19.0</p>
  
  <p>I would like to meet the following specifications.
  1. When the processing is completed, move the xml file and the paired jpg file to the done directory
  2. When the processing fails, move the xml file and the paired jpg file to the error directory</p>
  
  <p>The directory structure is as follows.</p>
  
  <pre>
  main/ftp/20170605-110000.xml
          /20170605-110000.jpg
  main/done/20170604-090000.xml
            20170604-090000.jpg
  main/error/20170604-090000.xml
            20170604-090000.jpg
  </pre>
  
  <p>I have satisfied the desired behavior with the following code.
  </p>
  
  <pre class="lang-java prettyprint-override"><code>public class ExampleRoute extends RouteBuilder {
  
      private final File ftpDir;
  
      private final File doneDir;
  
      private final File errorDir;
  
      public ExampleRoute() {
          this.ftpDir = new File("work/main/ftp");
          this.doneDir = new File("work/main/done");
          this.errorDir = new File("work/main/error");
      }
  
  
  
      @Override
      public void configure() throws Exception {
          String format = "file://%s?include=.*.xml&amp;move=%s&amp;moveFailed=%s";
          String from = String.format(format,
                  ftpDir.getAbsolutePath(),
                  doneDir.getAbsolutePath(),
                  errorDir.getAbsolutePath());
  
          // @formatter:off
          onException(Exception.class)
          .handled(false)
          .process(new MoveResourceProcessor((errorDir)))
          .stop();
          // @formatter:on
  
  
          // @formatter:off
          from(from)
          .process(exchange -&gt; {
               // Nothing to do...
          })
          .process(new MoveResourceProcessor(doneDir))
          .end();
          // @formatter:on
      }
  
      private class MoveResourceProcessor implements Processor {
  
          private final File dir;
  
          public MoveResourceProcessor(File dir) {
              this.dir = dir;
          }
  
          @Override
          public void process(Exchange exchange) throws Exception {
              String parent = (String) exchange.getIn().getHeader(Exchange.FILE_PARENT);
              File parentDir = new File(parent);
  
              String filename = (String) exchange.getIn().getHeader(Exchange.FILE_NAME_ONLY);
              String baseName = FilenameUtils.getBaseName(filename);
  
              File source = new File(parentDir, baseName + ".jpg");
              if (source.exists()) {
                  File dest = new File(dir, source.getName());
                  FileUtils.moveFile(source, dest);
              }
          }
      }
  }
  </code></pre>
  
  <p>But is there a better method for arranging multiple files related to such target files?</p>