Spring Batch instrumentation tests: flows
MODIFY
instrumentation/spring/spring-batch-3.0/javaagent/src/test/groovy/SpringBatchApplication.groovy
instrumentation/spring/spring-batch-3.0/javaagent/src/test/groovy/SpringBatchApplication.groovy
@@ -8,10 +8,13 @@ import org.springframework.batch.core.Step
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory
+import org.springframework.batch.core.job.builder.FlowBuilder
+import org.springframework.batch.core.job.flow.Flow
+import org.springframework.batch.core.job.flow.support.SimpleFlow
import org.springframework.batch.item.ItemProcessor
import org.springframework.batch.item.ItemReader
import org.springframework.batch.item.ItemWriter
-import org.springframework.beans.factory.annotation.Qualifier
+import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import springbatch.TestItemProcessor
@@ -22,48 +25,50 @@ import springbatch.TestTasklet
@Configuration
@EnableBatchProcessing
class SpringBatchApplication {
+
+ @Autowired
+ JobBuilderFactory jobs
+ @Autowired
+ StepBuilderFactory steps
+
+ // simple tasklet job
@Bean
- Job taskletJob(@Qualifier("step") Step taskletStep, JobBuilderFactory jobBuilderFactory) {
- jobBuilderFactory.get("taskletJob")
- .start(taskletStep)
+ Job taskletJob() {
+ jobs.get("taskletJob")
+ .start(step())
.build()
}
@Bean
- Step step(StepBuilderFactory stepBuilderFactory) {
- stepBuilderFactory.get("step")
+ Step step() {
+ steps.get("step")
.tasklet(new TestTasklet())
.build()
}
+ // 2-step tasklet + chunked items job
@Bean
- Job itemsAndTaskletJob(@Qualifier("itemStep") Step itemStep,
- @Qualifier("taskletStep") Step taskletStep,
- JobBuilderFactory jobBuilderFactory) {
- jobBuilderFactory.get("itemsAndTaskletJob")
- .flow(itemStep)
- .next(taskletStep)
- .end()
+ Job itemsAndTaskletJob() {
+ jobs.get("itemsAndTaskletJob")
+ .start(itemStep())
+ .next(taskletStep())
.build()
}
@Bean
- Step taskletStep(StepBuilderFactory stepBuilderFactory) {
- stepBuilderFactory.get("taskletStep")
+ Step taskletStep() {
+ steps.get("taskletStep")
.tasklet(new TestTasklet())
.build()
}
@Bean
- Step itemStep(ItemReader<String> itemReader,
- ItemProcessor<String, Integer> itemProcessor,
- ItemWriter<Integer> itemWriter,
- StepBuilderFactory stepBuilderFactory) {
- stepBuilderFactory.get("itemStep")
+ Step itemStep() {
+ steps.get("itemStep")
.chunk(5)
- .reader(itemReader)
- .processor(itemProcessor)
- .writer(itemWriter)
+ .reader(itemReader())
+ .processor(itemProcessor())
+ .writer(itemWriter())
.build()
}
@@ -81,4 +86,36 @@ class SpringBatchApplication {
ItemWriter<Integer> itemWriter() {
new TestItemWriter()
}
+
+ // job using a flow
+ @Bean
+ Job flowJob() {
+ jobs.get("flowJob")
+ .start(flow())
+ .build()
+ .build()
+ }
+
+ @Bean
+ Flow flow() {
+ new FlowBuilder<SimpleFlow>("flow")
+ .start(flowStep1())
+ .on("*")
+ .to(flowStep2())
+ .build()
+ }
+
+ @Bean
+ Step flowStep1() {
+ steps.get("flowStep1")
+ .tasklet(new TestTasklet())
+ .build()
+ }
+
+ @Bean
+ Step flowStep2() {
+ steps.get("flowStep2")
+ .tasklet(new TestTasklet())
+ .build()
+ }
}

MODIFY
instrumentation/spring/spring-batch-3.0/javaagent/src/test/groovy/SpringBatchTest.groovy
instrumentation/spring/spring-batch-3.0/javaagent/src/test/groovy/SpringBatchTest.groovy
@@ -115,6 +115,41 @@ abstract class SpringBatchTest extends AgentTestRunner {
}
}
}
+
+ def "should trace flow job"() {
+ when:
+ runJob("flowJob")
+
+ then:
+ assertTraces(1) {
+ trace(0, 5) {
+ span(0) {
+ name "BatchJob flowJob"
+ kind INTERNAL
+ }
+ span(1) {
+ name "BatchJob flowJob.flowStep1"
+ kind INTERNAL
+ childOf span(0)
+ }
+ span(2) {
+ name "BatchJob flowJob.flowStep1.Chunk"
+ kind INTERNAL
+ childOf span(1)
+ }
+ span(3) {
+ name "BatchJob flowJob.flowStep2"
+ kind INTERNAL
+ childOf span(0)
+ }
+ span(4) {
+ name "BatchJob flowJob.flowStep2.Chunk"
+ kind INTERNAL
+ childOf span(3)
+ }
+ }
+ }
+ }
}
class JavaConfigBatchJobTest extends SpringBatchTest implements ApplicationConfigTrait {

ADD
instrumentation/spring/spring-batch-3.0/javaagent/src/test/resources/META-INF/batch-jobs/flowJob.xml
+<?xml version="1.0" encoding="UTF-8"?>
+<job id="flowJob" xmlns="http://xmlns.jcp.org/xml/ns/javaee" version="1.0">
+ <flow id="flow">
+ <step id="flowStep1" next="flowStep2">
+ <batchlet ref="jsr.TestBatchlet"/>
+ </step>
+ <step id="flowStep2">
+ <batchlet ref="jsr.TestBatchlet"/>
+ </step>
+ </flow>
+</job>

MODIFY
instrumentation/spring/spring-batch-3.0/javaagent/src/test/resources/spring-batch.xml
instrumentation/spring/spring-batch-3.0/javaagent/src/test/resources/spring-batch.xml
</b:step>
</b:job>
+ <b:job id="flowJob">
+ <b:flow id="flow" parent="parentFlow"/>
+ </b:job>
+ <b:flow id="parentFlow">
+ <b:step id="flowStep1" next="flowStep2">
+ <b:tasklet ref="testTasklet"/>
+ </b:step>
+ <b:step id="flowStep2">
+ <b:tasklet ref="testTasklet"/>
+ </b:step>
+ </b:flow>
+
<bean id="itemReader" class="springbatch.TestItemReader"/>
<bean id="itemProcessor" class="springbatch.TestItemProcessor"/>
<bean id="itemWriter" class="springbatch.TestItemWriter"/>

Spring Batch instrumentation tests: flows (#1944)