title: How to read file using Apache Camel, process the file using Spring batch, read each line and route it back through Apache Camel
body: <p>Would like to know how to pass the JAXBElement to a Camel route, which processed from each line of the batch file read through Spring batch loaded through Camel Route.</p>
  
  <p>Code snippets given below uses a customerWriter method to call JMSTemplate to write the message to a Queue. Instead I need to route the message to another Camel route.</p>
  
  <p><b>Current:</b> CamelRoute -> ReadFile -> Spring Batch -> Process Each line -> Queue</p>
  
  <p><b>Expected:</b> CamelRoute -> ReadFile -> Spring Batch -> Process Each line -> Camel Route</p>
  
  <p><b>Camel Route to read the file:</b></p>
  
  <pre><code>@Override
  public void configure()  {
  
      String fromUri = batchLoadPath + "?" + batchFileOptions;
      from(fromUri).process(new Processor() {
          public void process(Exchange msg)  {
              File file = msg.getIn().getBody(File.class);
              String fileName = file.getAbsolutePath();
  
              try {
                  JobParameters jobParameters = new JobParametersBuilder().addString("input.file.name", fileName).addDate("dateTime", new Date()).toJobParameters();
                  jobLauncher.run(importCustomerJob, jobParameters);
              } catch (Exception e) {
                  log.error(Process file encountered error:" + e.getMessage(), e);
              }
          }
  
      })
      .to("log:EndBatch");
  </code></pre>
  
  <p><b>Batch Config:</b></p>
  
  <pre><code>@Bean
  public JmsItemWriter&lt;String&gt; customerWriter() {
      JmsItemWriter&lt;String&gt; writer = new JmsItemWriter&lt;String&gt;();
      writer.setJmsTemplate(jmsTemplate);
      return writer;
  }
  
  public Job importCustomerJob(JobCompletionNotificationListener listener, JobBuilderFactory jobBuilderFactory, Step step1) {
      JobBuilder builder = jobBuilderFactory.get("importCustomerJob");
      builder.incrementer(new RunIdIncrementer());
      builder.listener(listener);
      JobFlowBuilder jfb = builder.flow(step1);
      jfb.end();
      Job job = jfb.build().build();
      return job;
  }
  
  @Bean
  public Step step1(StepBuilderFactory stepBuilderFactory) {
      // Read chunk of 10 records and writing those as messages to queue
      return stepBuilderFactory.get("step1")
              .&lt;Customer, String&gt;chunk(10)
              .reader(customerReader())
              .faultTolerant()
              .skipPolicy(fileVerificationSkipper())
              .processor(customerItemProcessor())
              .writer(customerWriter())
              .listener(customerReader())
              .build();
  }
  </code></pre>
  
  <p><b>Batch Processor:</b></p>
  
  <pre><code>public class CustomerItemProcessor implements ItemProcessor&lt;Customer, String&gt; {
      @Autowired
      JaxbUtil jaxbUtil;
  
      public String process(Customer item) throws Exception {
          // Mapping code goes here
          JAXBElement&lt;CustomerX&gt; mobj = customerFactory.createCustomerX(cp);
          return jaxbUtil.objectToXml(mobj);
      }
  }
  </code></pre>