title: Spring Batch and Spring TaskScheduler
body: <p>So I have two jobs with separate tasklets each. </p>
  
  <p><strong>JobA</strong> will do:</p>
  
  <pre><code>sysout("JobA executed at "+new Date());
  </code></pre>
  
  <p><strong>JobB</strong> will do:</p>
  
  <pre><code>sysout("JobB executed at "+new Date());
  </code></pre>
  
  <p>Both of which can either be in a separate xml or in one xml. But I am currently have separate xml for both. I defined both jobs with the same way like this:</p>
  
  <pre><code>&lt;batch:job id="task_scheduler" restartable="false"&gt;
      &lt;batch:step id="test_task_scheduler"
          allow-start-if-complete="true"&gt;
          &lt;batch:tasklet ref="job1" /&gt;
      &lt;/batch:step&gt;
  &lt;/batch:job&gt;
  
  &lt;bean id="job1" class="com.irsis.batch.tasklet.TestOut" /&gt;
  &lt;bean id="runScheduler" class="com.irsis.batch.taskscheduler.RunScheduler1" /&gt;
  </code></pre>
  
  <p>Then at my <code>application-context.xml</code> I defined the task scheduler</p>
  
  <pre><code>    &lt;task:scheduled-tasks&gt;
      &lt;task:scheduled ref="runScheduler1" method="run"
          cron="*0 0/1 * 1/1 * ? " /&gt;
      &lt;task:scheduled ref="runScheduler2" method="run"
          cron="0 0/2 * 1/1 * ? "/&gt;
  &lt;/task:scheduled-tasks&gt;
  </code></pre>
  
  <p>When running I have this exception </p>
  
  <pre><code>Could not autowire field: org.springframework.batch.core.Job com.irsis.batch.taskscheduler.RunScheduler1.job; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.batch.core.Job] is defined: expected single matching bean but found 2: task_scheduler,task_scheduler2
  </code></pre>
  
  <p>btw, this is my RunScheduler1.java file</p>
  
  <pre><code>@Autowired 
  JobLauncher jobLauncher;
  
  @Autowired 
  Job job;
  
  public void run() {
  
  
      try {
  
          String dateParam = new Date().toString();
          JobParameters param = new JobParametersBuilder().addString("date",
                  dateParam).toJobParameters();
  
          JobExecution execution = jobLauncher.run(job, param);
          System.out.println("Exit Status : " + execution.getStatus());
  
      } catch (Exception e) {
          e.printStackTrace();
      }
  
      System.out.println("Done");
  
  }
  </code></pre>
  
  <p>Now my question is it possible for spring batch to have multiple jobs? Because from what I'm seeing from my exception is that it's only expecting a single job.</p>
  
  <p>Though I tried another way. What I did was something like this in the application-context.xml</p>
  
  <pre><code>&lt;batch:job id="job" restartable="false"&gt;
      &lt;batch:step id="test_task_scheduler"
          allow-start-if-complete="true"&gt;
          &lt;batch:tasklet ref="job1" /&gt;
      &lt;/batch:step&gt;
  &lt;/batch:job&gt;
  
  &lt;bean id="job1" class="com.irsis.batch.tasklet.TestOut" /&gt;
  &lt;bean id="runScheduler1" class="com.irsis.batch.taskscheduler.RunScheduler1" /&gt;
  
  
      &lt;batch:job id="job2" restartable="false"&gt;
      &lt;batch:step id="test_task2_scheduler"
          allow-start-if-complete="true"&gt;
          &lt;batch:tasklet ref="job2" /&gt;
      &lt;/batch:step&gt;
  &lt;/batch:job&gt;
  
  &lt;bean id="job2" class="com.irsis.batch.tasklet.TestOut2" /&gt;
  &lt;bean id="runScheduler2" class="com.irsis.batch.taskscheduler.RunScheduler2" /&gt;
  
  
  &lt;task:scheduled-tasks&gt;
      &lt;task:scheduled ref="runScheduler1" method="run"
          cron="*0 0/1 * 1/1 * ? " /&gt;
  
  &lt;/task:scheduled-tasks&gt;
  
  &lt;task:scheduled-tasks&gt;
      &lt;task:scheduled ref="runScheduler2" method="run"
          cron="0 0/2 * 1/1 * ? " /&gt;
  &lt;/task:scheduled-tasks&gt;
  </code></pre>
  
  <p>It ran the first job then whenever it tried to run the 2nd job this exception appears:</p>
  
  <blockquote>
    <p>PreparedStatementCallback; 
    SQL [INSERT into BATCH_JOB_INSTANCE(JOB_INSTANCE_ID, JOB_NAME, JOB_KEY, VERSION)<br>
    values (?, ?, ?, ?)]; ERROR: duplicate key value violates unique
    constraint "job_inst_un"   Detail: Key (job_name, job_key)=(job,
    8a87688793625c6f3b8f982dbe6689d2) already exists.; nested exception is
    org.postgresql.util.PSQLException: ERROR: duplicate key value violates
    unique constraint "job_inst_un"   Detail: Key (job_name,
    job_key)=(job, 8a87688793625c6f3b8f982dbe6689d2) already exists.</p>
  </blockquote>
  
  <p>UPDATE: I think I'm gonna change my approach. Another question link<a href="https://stackoverflow.com/questions/22521046/spring-taskscheduler">here</a></p>
  
  <p>Thanks,<br>
  Jet</p>