Checkpoint †
簡単な Chunk で動かしてみる †
- "13"を読んだところで、Status STOP で終了
- checkpoint-job.xml
<?xml version="1.0" encoding="UTF-8"?>
<job id="checkpoint-job"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
version="1.0">
<step id="checkpoint-chunk">
<chunk item-count="5">
<reader ref="CpItemReader"/>
<processor ref="CpItemProcessor"/>
<writer ref="CpItemWriter"/>
</chunk>
<stop on="STOP"/>
<end on="COMPLETE"/>
</step>
</job>
- CpItemReader?.java
package com.mycompany.jbatchexam;
import java.io.Serializable;
import javax.batch.api.chunk.ItemReader;
import javax.batch.runtime.context.StepContext;
import javax.enterprise.context.Dependent;
import javax.inject.Inject;
import javax.inject.Named;
@Dependent
@Named("CpItemReader")
public class CpItemReader implements ItemReader {
private MyCheckPoint ckpt;
@Inject
StepContext stepCtx;
@Override
public void open(Serializable checkpoint) throws Exception {
System.out.println("CpItemReader#open");
if (null == checkpoint) {
System.out.println("Checkpoint Initialize");
ckpt = new MyCheckPoint();
} else {
System.out.println("Checkpoint Hand over");
ckpt = (MyCheckPoint) checkpoint;
}
}
@Override
public void close() throws Exception {
System.out.println("CpItemReader#close");
}
@Override
public Object readItem() throws Exception {
ckpt.countup();
if (ckpt.getCounter() == 13) {
stepCtx.setExitStatus("STOP");
return null;
}
if (ckpt.getCounter() > 23) {
stepCtx.setExitStatus("COMPLETE");
return null;
}
System.out.println("CpItemReader#readItem " + (ckpt.getCounter()));
return ckpt.getCounter();
}
@Override
public Serializable checkpointInfo() throws Exception {
System.out.println("CpItemReader#checkpointInfo");
return ckpt;
}
}
- CpItemProcessor?.java
package com.mycompany.jbatchexam;
import javax.batch.api.chunk.ItemProcessor;
import javax.enterprise.context.Dependent;
import javax.inject.Named;
@Dependent
@Named("CpItemProcessor")
public class CpItemProcessor implements ItemProcessor {
@Override
public Object processItem(Object item) throws Exception {
System.out.println("CpItemProcessor#processItem " + item);
return "P" + item;
}
}
- CpItemWriter?.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.mycompany.jbatchexam;
import java.io.Serializable;
import java.util.List;
import javax.batch.api.chunk.ItemWriter;
import javax.enterprise.context.Dependent;
import javax.inject.Named;
@Dependent
@Named("CpItemWriter")
public class CpItemWriter implements ItemWriter {
private MyCheckPoint ckpt;
@Override
public void open(Serializable checkpoint) throws Exception {
System.out.println("CpItemWriter#open");
if (null == checkpoint) {
ckpt = new MyCheckPoint();
} else {
ckpt = (MyCheckPoint) checkpoint;
}
}
@Override
public void close() throws Exception {
System.out.println("CpItemWriter#close");
}
@Override
public void writeItems(List<Object> items) throws Exception {
System.out.println("CpItemWriter#writeItems " + items);
}
@Override
public Serializable checkpointInfo() throws Exception {
System.out.println("CpItemWriter#checkpointInfo");
return ckpt;
}
}
- kick job
JobOperator job = BatchRuntime.getJobOperator();
Properties prop = new Properties();
prop.setProperty("work_file", UUID.randomUUID().toString());
System.out.println("Batch Start");
long id = job.start("checkpoint-job", prop);
System.out.println("Batch Queued " +id);
- 実行結果
情報: Batch Start
情報: Batch Queued 32
情報: CpItemReader#open
情報: Checkpoint Initialize
情報: CpItemWriter#open
情報: CpItemReader#readItem 1
情報: CpItemProcessor#processItem 1
情報: CpItemReader#readItem 2
情報: CpItemProcessor#processItem 2
情報: CpItemReader#readItem 3
情報: CpItemProcessor#processItem 3
情報: CpItemReader#readItem 4
情報: CpItemProcessor#processItem 4
情報: CpItemReader#readItem 5
情報: CpItemProcessor#processItem 5
情報: CpItemWriter#writeItems [P1, P2, P3, P4, P5]
情報: CpItemReader#checkpointInfo
情報: CpItemWriter#checkpointInfo
情報: CpItemReader#readItem 6
情報: CpItemProcessor#processItem 6
情報: CpItemReader#readItem 7
情報: CpItemProcessor#processItem 7
情報: CpItemReader#readItem 8
情報: CpItemProcessor#processItem 8
情報: CpItemReader#readItem 9
情報: CpItemProcessor#processItem 9
情報: CpItemReader#readItem 10
情報: CpItemProcessor#processItem 10
情報: CpItemWriter#writeItems [P6, P7, P8, P9, P10]
情報: CpItemReader#checkpointInfo
情報: CpItemWriter#checkpointInfo
情報: CpItemReader#readItem 11
情報: CpItemProcessor#processItem 11
情報: CpItemReader#readItem 12
情報: CpItemProcessor#processItem 12
情報: CpItemWriter#writeItems [P11, P12]
情報: CpItemReader#checkpointInfo
情報: CpItemWriter#checkpointInfo
情報: CpItemReader#close
情報: CpItemWriter#close
- 管理コンソール
JOBの検索 †
out.println("<table border=\"1\"><tbody>");
JobOperator job = BatchRuntime.getJobOperator();
for (String name : job.getJobNames()){
int instanceCount = job.getJobInstanceCount(name);
for (JobInstance instance : job.getJobInstances(name, 0, instanceCount)) {
for (JobExecution execution : job.getJobExecutions(instance)) {
out.println(
String.format("<tr><td>%s</td><td>%d</td><td>%s</td></tr>"
,instance.getJobName()
,instance.getInstanceId()
,execution.getBatchStatus()));
}
}
}
out.println("</tbody></table>");
JOBの消去 †
JOBの再開 †
Java#Glassfish