public static long runJob(String batchJobName) {
    JobOperator jo = BatchRuntime.getJobOperator();
    Properties props = new Properties();

    long id = jo.start(batchJobName, props);
    return id;
  }
  private static void executeJob(String jobId) {
    long execID = 0;
    // ジョブの開始時はXML読み込みなど同時実行されるとおかしくなる処理があるので、ジョブID単位で同期化しておく。
    JobOperator jobOperator = BatchRuntime.getJobOperator();
    synchronized (jobId) {
      Properties props = new Properties();
      props.setProperty(
          "date", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
      execID = jobOperator.start(jobId, props);
      System.out.println("job stated id:" + execID);
    }
    JobExecution jobExec = null;
    // ジョブが終了するまでポーリング
    while (true) {
      jobExec = jobOperator.getJobExecution(execID);

      if (jobExec.getEndTime() != null) {
        break;
      }

      try {
        Thread.sleep(1000L);
      } catch (InterruptedException ex) {
      }
    }
    System.out.println("JOB END:Status is " + jobExec.getExitStatus());
  }
 /**
  * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
  *
  * @param request servlet request
  * @param response servlet response
  * @throws ServletException if a servlet-specific error occurs
  * @throws IOException if an I/O error occurs
  */
 protected void processRequest(HttpServletRequest request, HttpServletResponse response)
     throws ServletException, IOException {
   response.setContentType("text/html;charset=UTF-8");
   try (PrintWriter out = response.getWriter()) {
     out.println("<html>");
     out.println("<head>");
     out.println("<title>Servlet TestServlet</title>");
     out.println("</head>");
     out.println("<body>");
     out.println("<h1>Servlet TestServlet at " + request.getContextPath() + "</h1>");
     out.println("About to start the job<br>");
     JobOperator jo = BatchRuntime.getJobOperator();
     out.println("Got the job operator: " + jo + "<br>");
     try {
       jo.start("myJob", new Properties());
     } catch (JobSecurityException ex) {
       Logger.getLogger(TestServlet.class.getName()).log(Level.SEVERE, null, ex);
     }
     out.println("Job submitted<br>");
     out.println("<br><br>Check server.log for output");
     out.println("</body>");
     out.println("</html>");
   } catch (JobStartException ex) {
     Logger.getLogger(TestServlet.class.getName()).log(Level.SEVERE, null, ex);
   }
 }
Beispiel #4
0
 public void abandonJob(JobExecution jobExecution) {
   JobOperator jo = BatchRuntime.getJobOperator();
   jo.abandon(jobExecution.getInstanceId());
   LOGGER.log(Level.INFO, "Abandoned job: {0}", jobExecution.getInstanceId());
   jobExecution.setStatus(JobStatus.ABANDONED);
   em.merge(jobExecution);
 }
Beispiel #5
0
  private void testReadWrite0(
      final String writeResource,
      final String start,
      final String end,
      final Class<?> beanType,
      final String expect,
      final String forbid)
      throws Exception {
    final Properties params =
        CsvItemReaderWriterTest.createParams(CsvProperties.BEAN_TYPE_KEY, beanType.getName());
    final File writeResourceFile = new File(CsvItemReaderWriterTest.tmpdir, writeResource);
    params.setProperty("writeResource", writeResourceFile.getPath());
    params.setProperty(CsvProperties.CELL_PROCESSORS_KEY, cellProcessors);

    if (start != null) {
      params.setProperty(CsvProperties.START_KEY, start);
    }
    if (end != null) {
      params.setProperty(CsvProperties.END_KEY, end);
    }
    if (this.partialNameMapping != null) {
      params.setProperty(CsvProperties.NAME_MAPPING_KEY, partialNameMapping);
    }
    params.setProperty(CsvProperties.HEADER_KEY, header);
    CsvItemReaderWriterTest.setRandomWriteMode(params);

    final long jobExecutionId = jobOperator.start(jobName, params);
    final JobExecutionImpl jobExecution =
        (JobExecutionImpl) jobOperator.getJobExecution(jobExecutionId);
    jobExecution.awaitTermination(CsvItemReaderWriterTest.waitTimeoutMinutes, TimeUnit.MINUTES);
    Assert.assertEquals(BatchStatus.COMPLETED, jobExecution.getBatchStatus());

    CsvItemReaderWriterTest.validate(writeResourceFile, expect, forbid);
  }
Beispiel #6
0
 public void stopJob(JobExecution jobExecution) {
   try {
     JobOperator jo = BatchRuntime.getJobOperator();
     jo.stop(jobExecution.getInstanceId());
     LOGGER.log(Level.INFO, "Stopped job: {0}", jobExecution.getInstanceId());
     jobExecution.setStatus(JobStatus.STOPPED);
     em.merge(jobExecution);
   } catch (JobExecutionNotRunningException ex) {
     LOGGER.log(Level.WARNING, ex.getMessage(), ex);
   }
 }
Beispiel #7
0
 public void restartJob(JobExecution jobExecution) {
   try {
     JobOperator jo = BatchRuntime.getJobOperator();
     jobExecution.setInstanceId(
         jo.restart(jobExecution.getInstanceId(), new java.util.Properties()));
     LOGGER.log(Level.INFO, "Restarted job: {0}", jobExecution.getInstanceId());
     jobExecution.setStatus(JobStatus.STARTED);
     em.merge(jobExecution);
   } catch (NoSuchJobExecutionException | JobRestartException ex) {
     LOGGER.log(Level.WARNING, ex.getMessage(), ex);
   }
 }
Beispiel #8
0
 public void startJob(JobExecution jobExecution) {
   try {
     JobOperator jo = BatchRuntime.getJobOperator();
     long instanceId =
         jo.start(jobExecution.getJobScheduler().getName(), new java.util.Properties());
     LOGGER.log(Level.INFO, "Started job: {0}", instanceId);
     jobExecution.setInstanceId(instanceId);
     jobExecution.setStatus(JobStatus.STARTED);
     em.merge(jobExecution);
   } catch (JobStartException ex) {
     LOGGER.log(Level.SEVERE, ex.getMessage(), ex);
   }
 }
  @Test
  public void write() throws Exception {
    final String path = "target/work/StaxItemWriter.xml";

    final Properties jobParams = new Properties();
    jobParams.setProperty("output", path);

    final JobOperator jobOperator = BatchRuntime.getJobOperator();
    Batches.waitForEnd(jobOperator, jobOperator.start("stax-writer", jobParams));
    final String content = IOs.slurp(path);
    assertEquals(
        content.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", ""),
        "<root><foo><value>item 1</value></foo><foo><value>item 2</value></foo></root>");
  }
Beispiel #10
0
  /**
   * Waits for job to finish. JSR 352 does not provide a mechanism to launch jobs in the synchronous
   * mode. As a result, the polling needs to occur
   *
   * @param jobOperator
   * @param executionId
   * @param pollingWait
   * @param maxTries
   * @return
   */
  public static JobExecution waitForJobToEnd(
      JobOperator jobOperator, final long executionId, final long pollingWait, final int maxTries) {

    int countTries = 0;
    JobExecution jobExecution = null;
    do {
      try {
        countTries++;
        Thread.sleep(pollingWait);
      } catch (InterruptedException e) {
        throw new RuntimeException(e);
      }
      jobExecution = jobOperator.getJobExecution(executionId);
    } while (INCOMPLETE_STATUSES.contains(jobExecution.getBatchStatus())
        || (countTries < maxTries));

    return jobExecution;
  }
 @Override
 public void run() {
   JobOperator jobOperator = BatchRuntime.getJobOperator();
   long id = jobOperator.start(AWSEC2_INSTANCE_JOB, new Properties());
   logger.debug("Start the job:  " + AWSEC2_INSTANCE_JOB + " with ID: " + id);
 }
 @Override
 protected long doStart(final JobOperator operator) {
   final long nid = operator.restart(id, toProperties(properties));
   info("Batch " + nid + " restarted with id #" + nid);
   return nid;
 }