|
18 | 18 | import java.time.Duration; |
19 | 19 | import java.time.LocalDateTime; |
20 | 20 | import java.time.temporal.ChronoUnit; |
21 | | -import java.util.Arrays; |
22 | | -import java.util.List; |
23 | 21 |
|
24 | | -import javax.sql.DataSource; |
25 | | - |
26 | | -import io.micrometer.core.instrument.Meter; |
27 | | -import io.micrometer.core.instrument.MeterRegistry; |
28 | | -import io.micrometer.core.instrument.observation.DefaultMeterObservationHandler; |
29 | | -import io.micrometer.core.instrument.simple.SimpleMeterRegistry; |
30 | | -import io.micrometer.observation.ObservationRegistry; |
31 | 22 | import org.junit.jupiter.api.Test; |
32 | 23 |
|
33 | | -import org.springframework.batch.core.ExitStatus; |
34 | | -import org.springframework.batch.core.configuration.annotation.EnableJdbcJobRepository; |
35 | | -import org.springframework.batch.core.job.Job; |
36 | | -import org.springframework.batch.core.job.JobExecution; |
37 | | -import org.springframework.batch.core.job.parameters.JobParameters; |
38 | | -import org.springframework.batch.core.step.Step; |
39 | | -import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; |
40 | | -import org.springframework.batch.core.job.builder.JobBuilder; |
41 | | -import org.springframework.batch.core.launch.JobOperator; |
42 | | -import org.springframework.batch.core.repository.JobRepository; |
43 | | -import org.springframework.batch.core.step.builder.StepBuilder; |
44 | | -import org.springframework.batch.item.support.ListItemReader; |
45 | | -import org.springframework.context.ApplicationContext; |
46 | | -import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
47 | | -import org.springframework.context.annotation.Bean; |
48 | | -import org.springframework.context.annotation.Configuration; |
49 | | -import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; |
50 | | -import org.springframework.jdbc.support.JdbcTransactionManager; |
51 | | -import org.springframework.transaction.PlatformTransactionManager; |
52 | | - |
53 | | -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
54 | 24 | import static org.junit.jupiter.api.Assertions.assertEquals; |
55 | 25 | import static org.junit.jupiter.api.Assertions.assertNull; |
56 | 26 | import static org.junit.jupiter.api.Assertions.assertTrue; |
@@ -132,118 +102,4 @@ void testFormatNullDuration() { |
132 | 102 | assertTrue(formattedDuration.isEmpty()); |
133 | 103 | } |
134 | 104 |
|
135 | | - @Test |
136 | | - void testBatchMetrics() throws Exception { |
137 | | - // given |
138 | | - ApplicationContext context = new AnnotationConfigApplicationContext(MyJobConfiguration.class); |
139 | | - JobOperator jobOperator = context.getBean(JobOperator.class); |
140 | | - Job job = context.getBean(Job.class); |
141 | | - MeterRegistry meterRegistry = context.getBean(MeterRegistry.class); |
142 | | - int expectedBatchMetricsCount = 6; |
143 | | - |
144 | | - // when |
145 | | - JobExecution jobExecution = jobOperator.start(job, new JobParameters()); |
146 | | - |
147 | | - // then |
148 | | - assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus()); |
149 | | - List<Meter> meters = meterRegistry.getMeters(); |
150 | | - assertTrue(meters.size() >= expectedBatchMetricsCount); |
151 | | - |
152 | | - // Job metrics |
153 | | - |
154 | | - assertDoesNotThrow(() -> meterRegistry.get(BatchMetrics.METRICS_PREFIX + "job.launch.count").timer(), |
155 | | - "There should be a meter of type TIMER named spring.batch.job.launch.count registered in the meter registry"); |
156 | | - assertEquals(1, meterRegistry.get(BatchMetrics.METRICS_PREFIX + "job.launch.count").timer().count()); |
157 | | - |
158 | | - assertDoesNotThrow( |
159 | | - () -> meterRegistry.get(BatchMetrics.METRICS_PREFIX + "job") |
160 | | - .tag(BatchMetrics.METRICS_PREFIX + "job.name", "job") |
161 | | - .tag(BatchMetrics.METRICS_PREFIX + "job.status", "COMPLETED") |
162 | | - .timer(), |
163 | | - "There should be a meter of type TIMER named spring.batch.job registered in the meter registry"); |
164 | | - |
165 | | - // Step metrics |
166 | | - |
167 | | - assertDoesNotThrow( |
168 | | - () -> meterRegistry.get(BatchMetrics.METRICS_PREFIX + "step") |
169 | | - .tag(BatchMetrics.METRICS_PREFIX + "step.name", "step") |
170 | | - .tag(BatchMetrics.METRICS_PREFIX + "step.job.name", "job") |
171 | | - .tag(BatchMetrics.METRICS_PREFIX + "step.status", "COMPLETED") |
172 | | - .timer(), |
173 | | - "There should be a meter of type TIMER named spring.batch.step registered in the meter registry"); |
174 | | - |
175 | | - assertDoesNotThrow( |
176 | | - () -> meterRegistry.get(BatchMetrics.METRICS_PREFIX + ".item.read") |
177 | | - .tag(BatchMetrics.METRICS_PREFIX + ".item.read.job.name", "job") |
178 | | - .tag(BatchMetrics.METRICS_PREFIX + ".item.read.step.name", "step") |
179 | | - .tag(BatchMetrics.METRICS_PREFIX + ".item.read.status", "SUCCESS") |
180 | | - .timer(), |
181 | | - "There should be a meter of type TIMER named spring.batch.item.read registered in the meter registry"); |
182 | | - |
183 | | - assertDoesNotThrow( |
184 | | - () -> meterRegistry.get(BatchMetrics.METRICS_PREFIX + "item.process") |
185 | | - .tag(BatchMetrics.METRICS_PREFIX + "item.process.job.name", "job") |
186 | | - .tag(BatchMetrics.METRICS_PREFIX + "item.process.step.name", "step") |
187 | | - .tag(BatchMetrics.METRICS_PREFIX + "item.process.status", "SUCCESS") |
188 | | - .timer(), |
189 | | - "There should be a meter of type TIMER named spring.batch.item.process registered in the meter registry"); |
190 | | - |
191 | | - assertDoesNotThrow( |
192 | | - () -> meterRegistry.get(BatchMetrics.METRICS_PREFIX + "chunk.write") |
193 | | - .tag(BatchMetrics.METRICS_PREFIX + "chunk.write.job.name", "job") |
194 | | - .tag(BatchMetrics.METRICS_PREFIX + "chunk.write.step.name", "step") |
195 | | - .tag(BatchMetrics.METRICS_PREFIX + "chunk.write.status", "SUCCESS") |
196 | | - .timer(), |
197 | | - "There should be a meter of type TIMER named spring.batch.chunk.write registered in the meter registry"); |
198 | | - |
199 | | - } |
200 | | - |
201 | | - @Configuration |
202 | | - @EnableBatchProcessing |
203 | | - @EnableJdbcJobRepository |
204 | | - static class MyJobConfiguration { |
205 | | - |
206 | | - @Bean |
207 | | - public Step step(JobRepository jobRepository, PlatformTransactionManager transactionManager) { |
208 | | - return new StepBuilder(jobRepository).<Integer, Integer>chunk(2) |
209 | | - .transactionManager(transactionManager) |
210 | | - .reader(new ListItemReader<>(Arrays.asList(1, 2, 3, 4, 5))) |
211 | | - .writer(items -> { |
212 | | - }) |
213 | | - .build(); |
214 | | - } |
215 | | - |
216 | | - @Bean |
217 | | - public Job job(JobRepository jobRepository, Step step) { |
218 | | - return new JobBuilder("job", jobRepository).start(step).build(); |
219 | | - } |
220 | | - |
221 | | - @Bean |
222 | | - public MeterRegistry meterRegistry() { |
223 | | - return new SimpleMeterRegistry(); |
224 | | - } |
225 | | - |
226 | | - @Bean |
227 | | - public ObservationRegistry observationRegistry(MeterRegistry meterRegistry) { |
228 | | - ObservationRegistry observationRegistry = ObservationRegistry.create(); |
229 | | - observationRegistry.observationConfig() |
230 | | - .observationHandler(new DefaultMeterObservationHandler(meterRegistry)); |
231 | | - return observationRegistry; |
232 | | - } |
233 | | - |
234 | | - @Bean |
235 | | - public DataSource dataSource() { |
236 | | - return new EmbeddedDatabaseBuilder().addScript("/org/springframework/batch/core/schema-drop-hsqldb.sql") |
237 | | - .addScript("/org/springframework/batch/core/schema-hsqldb.sql") |
238 | | - .generateUniqueName(true) |
239 | | - .build(); |
240 | | - } |
241 | | - |
242 | | - @Bean |
243 | | - public JdbcTransactionManager transactionManager(DataSource dataSource) { |
244 | | - return new JdbcTransactionManager(dataSource); |
245 | | - } |
246 | | - |
247 | | - } |
248 | | - |
249 | 105 | } |
0 commit comments