pom.xml へ Spring Batch 設定追加
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-core</artifactId>
<version>2.2.5.RELEASE</version>
</dependency>
Spring Batch 制御テーブル作成
eclipse のパッケージ・エクスプローラーから
プロジェクト中のMaven依存関係ツリーを開き、
spring-batch-core-2.2.5.RELEASE.jar の
org.springframework.batch.core.schema-mysql.sql を
MySQLWorkbench で実行、制御テーブルを作成する。
src/main/webapp/WEB-INF/spring/root-context.xml へバッチ定義追加
<bean id="jobOperator" class="org.springframework.batch.core.launch.support.SimpleJobOperator">
<property name="jobLauncher" ref="jobLauncher"/>
<property name="jobExplorer" ref="jobExplorer"/>
<property name="jobRepository" ref="jobRepository"/>
<property name="jobRegistry" ref="jobRegistry"/>
</bean>
<bean id="jobExplorer" class="org.springframework.batch.core.explore.support.JobExplorerFactoryBean">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="jobRegistry" class="org.springframework.batch.core.configuration.support.MapJobRegistry" />
<bean class="org.springframework.batch.core.configuration.support.JobRegistryBeanPostProcessor">
<property name="jobRegistry" ref="jobRegistry"/>
</bean>
<bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository" />
<property name="taskExecutor">
<bean class="org.springframework.core.task.SimpleAsyncTaskExecutor" />
</property>
</bean>
<bean id="jobRepository" class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="transactionManager" ref="transactionManager"/>
<property name="maxVarCharLength" value="2000"/>
<property name="isolationLevelForCreate" value="ISOLATION_SERIALIZABLE"/>
</bean>
<bean id="sampleTasklet" class="jp.s6131.sample.batch.SampleTasklet" />
<batch:job id="sampleJob">
<batch:step id="sampleTaskletStep">
<batch:tasklet ref="sampleTasklet" />
</batch:step>
</batch:job>
<bean id="sampleLauncher" class="jp.s6131.sample.batch.SampleLauncher">
<property name="jobLauncher" ref="jobLauncher"/>
<property name="job" ref="sampleJob"/>
</bean>
サンプルバッチラウンチャ
jp.s6131.sample.SampleLauncher.java
package jp.s6131.sample.batch;
・・・
public class SampleLauncher {
private static final Logger logger = LoggerFactory.getLogger(SampleLauncher.class);
private JobLauncher jobLauncher;
private Job job;
public void launch(String param) {
JobParameters jobParameters = new JobParametersBuilder()
.addLong("time", System.currentTimeMillis())
.addString("param", param)
.toJobParameters();
try {
JobExecution execution = jobLauncher.run(job, jobParameters);
logger.debug("Exit Status : " + execution.getStatus());
} catch (Exception ex) {
ex.printStackTrace();
}
}
public JobLauncher getJobLauncher() {
return jobLauncher;
}
public void setJobLauncher(JobLauncher jobLauncher) {
this.jobLauncher = jobLauncher;
}
public Job getJob() {
return job;
}
public void setJob(Job job) {
this.job = job;
}
}
サンプルバッチタスクレット
jp.s6131.sample.SampleTasklet.java
package jp.s6131.sample.batch;
・・・
public class SampleTasklet implements Tasklet {
private static final Logger logger = LoggerFactory.getLogger(SampleTasklet.class);
@Autowired
VelocityEngine velocityEngine;
@Override
public RepeatStatus execute(StepContribution paramStepContribution,
ChunkContext paramChunkContext) throws Exception {
Map<String, Object> param = new HashMap<String, Object>();
Map<String, String> data = new HashMap<String, String>();
for (int i = 0; i < 10; i++) {
data.put(String.format("%05d", i), String.format("名称%02d", i));
}
param.put("datalist", data);
try {
String outputFile = paramChunkContext.getStepContext().getStepExecution().getJobParameters().getString("param");
logger.debug("output html = " + outputFile);
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(outputFile), "utf-8");
VelocityEngineUtils.mergeTemplate(velocityEngine, "sample.vm", param, osw);
osw.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return RepeatStatus.FINISHED;
}
}
バッチ起動コントローラ
jp.s6131.sample.controller.HomeController.java
@Autowired
SampleLauncher sampleLauncher;
・・・
sampleLauncher.launch(req.getSession().getServletContext().getRealPath("/html/sample.html"));
http://localhost:8080/sample 実行後、バッチテーブルにログが出力されている事を確認。

人気ブログランキングへ