Search code examples
javajava-ee-7jsr352jberet

Java EE JSR 352 - Jberet Dynamic Job Builder and Execution


Understood from Jberet userguide that a job can be build dynamically as alternative to static xml, but the guide doesn't have any input on how to execute the job build, So willing to know how to execute the job.

Job job = new JobBuilder(jobName)
  .restartable(false)
  .property("jobk1", "J")
  .property("jobk2", "J")
  .listener("jobListener1", new String[]{"jobListenerk1", "#{jobParameters['jobListenerPropVal']}"},
          new String[]{"jobListenerk2", "#{jobParameters['jobListenerPropVal']}"})

  .step(new StepBuilder(stepName)
          .properties(new String[]{"stepk1", "S"}, new String[]{"stepk2", "S"})
          .batchlet(batchlet1Name, new String[]{"batchletk1", "B"}, new String[]{"batchletk2", "B"})
          .listener("stepListener1", stepListenerProps)
          .stopOn("STOP").restartFrom(stepName).exitStatus()
          .endOn("END").exitStatus("new status for end")
          .failOn("FAIL").exitStatus()
          .nextOn("*").to(step2Name)
          .build())

  .step(new StepBuilder(step2Name)
          .batchlet(batchlet1Name)
          .build())

          .build();

Solution

  • In short, you use the following method to start a job created with Java JSL:

    org.jberet.operations.AbstractJobOperator#start(org.jberet.job.model.Job, java.util.Properties)
    

    Version 1.3.0 beta

    Since Java JSL is an extra feature, you will need to obtain JBeret implementation of JobOperator and call the above start method:

    import org.jberet.job.model.Job;
    import org.jberet.operations.JobOperatorImpl;
    import org.jberet.spi.JobOperatorContext;
    
    JobOperatorImpl jobOperator = (JobOperatorImpl) JobOperatorContext.getJobOperatorContext().getJobOperator();
    
    Job job = new JobBuilder(jobName)
                    .step(new StepBuilder(stepName)
                            .reader(...)
                            .writer(...)
                            .build())
                    .build();
    Properties params = null;
    long jobExecutionId = jobOperator.start(job, params);
    

    Version 1.2.0-Final

    JobOperatorImpl jobOperator = (JobOperatorImpl) BatchRuntime.getJobOperator();
    Properties jobProperties = new Properties();
    long jobExecutionId = jobOperator.start(job, jobProperties);
    

    For more details, see JBeret sample app javaJSL, and code how to get JBeret job operator.