JobOperator job = BatchRuntime.getJobOperator(); long id = job.start("sample-job", null);
job.start(xml, properties)
xml | META-INF/batch-jobs/ 以下にある JOB 定義 XML ファイル名 |
properties | 実行時のパラメータ (cf.Glassfish jBatch Parameter) |
返り値は Job Execution 番号
package com.mycompany.jbatchexam;
import java.io.IOException;
import java.io.PrintWriter;
import javax.batch.operations.JobOperator;
import javax.batch.runtime.BatchRuntime;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name = "BatchEntry", urlPatterns = {"/BatchEntry"})
public class BatchEntry extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet BatchEntry</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet BatchEntry at " + request.getContextPath() + "</h1>");
JobOperator job = BatchRuntime.getJobOperator();
System.out.println("Batch Start");
long id = job.start("sample-job", null);
System.out.println("Batch Queued " +id);
out.println("Task Queued. No." + id);
out.println("</body>");
out.println("</html>");
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}
}
META-INF/batch-jobs/ 内の XML ファイルで定義する。JSL(Job Specification Language)
<?xml version="1.0" encoding="UTF-8"?>
<job id="sample-job"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
version="1.0">
<step id="sample-chunk" next="sample-batchlet">
<chunk item-count="5">
<reader ref="MyItemReader"/>
<processor ref="MyItemProcessor"/>
<writer ref="MyItemWriter"/>
</chunk>
</step>
<step id="sample-batchlet">
<batchlet ref="MyBatchlet"/>
</step>
</job>
(RP RP RP RP RP W) (RP RP RP RP RP W) (RP RP W)
(RP RP RP RP RP RP RP RP RP RP W) (RP RP W)
package com.mycompany.jbatchexam;
import java.io.Serializable;
import javax.batch.api.chunk.ItemReader;
import javax.enterprise.context.Dependent;
import javax.inject.Named;
@Dependent
@Named("MyItemReader")
public class MyItemReader implements ItemReader {
private int counter;
@Override
public void open(Serializable checkpoint) throws Exception {
System.out.println("MyItermReader#open");
counter = 0;
}
@Override
public void close() throws Exception {
System.out.println("MyItermReader#close");
}
@Override
public Object readItem() throws Exception {
counter += 1;
if (counter > 13) {
return null;
}
System.out.println("MyItermReader#readItem " + (counter));
return counter;
}
@Override
public Serializable checkpointInfo() throws Exception {
System.out.println("MyItermReader#checkpointInfo");
return null;
}
}
package com.mycompany.jbatchexam;
import javax.batch.api.chunk.ItemProcessor;
import javax.enterprise.context.Dependent;
import javax.inject.Named;
@Dependent
@Named("MyItemProcessor")
public class MyItemProcessor implements ItemProcessor {
@Override
public Object processItem(Object item) throws Exception {
System.out.println("MyItemProcessor#processItem " + item);
return "P" + item;
}
}
package com.mycompany.jbatchexam;
import java.io.Serializable;
import java.util.List;
import javax.batch.api.chunk.ItemWriter;
import javax.batch.runtime.context.JobContext;
import javax.enterprise.context.Dependent;
import javax.inject.Inject;
import javax.inject.Named;
@Dependent
@Named("MyItemWriter")
public class MyItemWriter implements ItemWriter {
@Inject
private JobContext jobCtx;
@Override
public void open(Serializable checkpoint) throws Exception {
System.out.println("MyItemWriter#open");
}
@Override
public void close() throws Exception {
System.out.println("MyItemWriter#close");
jobCtx.setTransientUserData("***** The treasure map is here ! ****");
}
@Override
public void writeItems(List<Object> items) throws Exception {
System.out.println("MyItemWriter#writeItems " + items);
}
@Override
public Serializable checkpointInfo() throws Exception {
System.out.println("MyItemWriter#checkpointInfo");
return null;
}
}
package com.mycompany.jbatchexam;
import javax.batch.api.Batchlet;
import javax.batch.runtime.context.JobContext;
import javax.enterprise.context.Dependent;
import javax.inject.Inject;
import javax.inject.Named;
@Dependent
@Named("MyBatchlet")
public class MyBatchlet implements Batchlet {
@Inject
private JobContext jobCtx;
@Override
public String process() throws Exception {
System.out.println("MyBatchlet#process");
System.out.println(jobCtx.getTransientUserData());
return null;
}
@Override
public void stop() throws Exception {
System.out.println("MyBatchlet#stop");
}
}
情報: Batch Start
情報: Batch Queued 11
情報: MyItermReader#open
情報: MyItemWriter#open
情報: MyItermReader#readItem 1
情報: MyItemProcessor#processItem 1
情報: MyItermReader#readItem 2
情報: MyItemProcessor#processItem 2
情報: MyItermReader#readItem 3
情報: MyItemProcessor#processItem 3
情報: MyItermReader#readItem 4
情報: MyItemProcessor#processItem 4
情報: MyItermReader#readItem 5
情報: MyItemProcessor#processItem 5
情報: MyItemWriter#writeItems [P1, P2, P3, P4, P5]
情報: MyItermReader#checkpointInfo
情報: MyItemWriter#checkpointInfo
情報: MyItermReader#readItem 6
情報: MyItemProcessor#processItem 6
情報: MyItermReader#readItem 7
情報: MyItemProcessor#processItem 7
情報: MyItermReader#readItem 8
情報: MyItemProcessor#processItem 8
情報: MyItermReader#readItem 9
情報: MyItemProcessor#processItem 9
情報: MyItermReader#readItem 10
情報: MyItemProcessor#processItem 10
情報: MyItemWriter#writeItems [P6, P7, P8, P9, P10]
情報: MyItermReader#checkpointInfo
情報: MyItemWriter#checkpointInfo
情報: MyItermReader#readItem 11
情報: MyItemProcessor#processItem 11
情報: MyItermReader#readItem 12
情報: MyItemProcessor#processItem 12
情報: MyItermReader#readItem 13
情報: MyItemProcessor#processItem 13
情報: MyItemWriter#writeItems [P11, P12, P13]
情報: MyItermReader#checkpointInfo
情報: MyItemWriter#checkpointInfo
情報: MyItermReader#close
情報: MyItemWriter#close
情報: MyBatchlet#process
情報: ***** The treasure map is here ! ****