11/*
2- * Copyright 2018 the original author or authors.
2+ * Copyright 2021 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
2121import org .springframework .batch .core .JobExecution ;
2222import org .springframework .batch .core .JobParameters ;
2323import org .springframework .batch .core .StepExecution ;
24+ import org .springframework .batch .core .configuration .annotation .BatchConfigurer ;
25+ import org .springframework .batch .core .configuration .annotation .DefaultBatchConfigurer ;
2426import org .springframework .batch .core .configuration .annotation .EnableBatchProcessing ;
2527import org .springframework .batch .core .configuration .annotation .JobBuilderFactory ;
2628import org .springframework .batch .core .configuration .annotation .StepBuilderFactory ;
2729import org .springframework .batch .core .launch .JobLauncher ;
2830import org .springframework .batch .core .listener .JobExecutionListenerSupport ;
2931import org .springframework .batch .core .step .tasklet .TaskletStep ;
3032import org .springframework .batch .item .ItemReader ;
31- import org .springframework .batch .item .ItemWriter ;
3233import org .springframework .beans .factory .annotation .Autowired ;
3334import org .springframework .context .ApplicationContext ;
3435import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
@@ -58,13 +59,14 @@ public class MultiThreadedTaskletStepIntegrationTests {
5859 @ Test
5960 public void testMultiThreadedTaskletExecutionWhenNoErrors () throws Exception {
6061 // given
61- Class [] configurationClasses = {JobConfiguration .class , TransactionManagerConfiguration .class };
62+ Class <?> [] configurationClasses = {JobConfiguration .class , TransactionManagerConfiguration .class };
6263 ApplicationContext context = new AnnotationConfigApplicationContext (configurationClasses );
6364 JobLauncher jobLauncher = context .getBean (JobLauncher .class );
6465 Job job = context .getBean (Job .class );
66+ JobParameters jobParameters = new JobParameters ();
6567
6668 // when
67- JobExecution jobExecution = jobLauncher .run (job , new JobParameters () );
69+ JobExecution jobExecution = jobLauncher .run (job , jobParameters );
6870
6971 // then
7072 assertNotNull (jobExecution );
@@ -77,13 +79,14 @@ public void testMultiThreadedTaskletExecutionWhenNoErrors() throws Exception {
7779 @ Test
7880 public void testMultiThreadedTaskletExecutionWhenCommitFails () throws Exception {
7981 // given
80- Class [] configurationClasses = {JobConfiguration .class , CommitFailingTransactionManagerConfiguration .class };
82+ Class <?> [] configurationClasses = {JobConfiguration .class , CommitFailingTransactionManagerConfiguration .class };
8183 ApplicationContext context = new AnnotationConfigApplicationContext (configurationClasses );
8284 JobLauncher jobLauncher = context .getBean (JobLauncher .class );
8385 Job job = context .getBean (Job .class );
86+ JobParameters jobParameters = new JobParameters ();
8487
8588 // when
86- JobExecution jobExecution = jobLauncher .run (job , new JobParameters () );
89+ JobExecution jobExecution = jobLauncher .run (job , jobParameters );
8790
8891 // then
8992 assertNotNull (jobExecution );
@@ -98,13 +101,14 @@ public void testMultiThreadedTaskletExecutionWhenCommitFails() throws Exception
98101 @ Test
99102 public void testMultiThreadedTaskletExecutionWhenRollbackFails () throws Exception {
100103 // given
101- Class [] configurationClasses = {JobConfiguration .class , RollbackFailingTransactionManagerConfiguration .class };
104+ Class <?> [] configurationClasses = {JobConfiguration .class , RollbackFailingTransactionManagerConfiguration .class };
102105 ApplicationContext context = new AnnotationConfigApplicationContext (configurationClasses );
103106 JobLauncher jobLauncher = context .getBean (JobLauncher .class );
104107 Job job = context .getBean (Job .class );
108+ JobParameters jobParameters = new JobParameters ();
105109
106110 // when
107- JobExecution jobExecution = jobLauncher .run (job , new JobParameters () );
111+ JobExecution jobExecution = jobLauncher .run (job , jobParameters );
108112
109113 // then
110114 assertNotNull (jobExecution );
@@ -130,7 +134,7 @@ public TaskletStep step() {
130134 return stepBuilderFactory .get ("step" )
131135 .<Integer , Integer >chunk (3 )
132136 .reader (itemReader ())
133- .writer (itemWriter () )
137+ .writer (items -> {} )
134138 .taskExecutor (taskExecutor ())
135139 .build ();
136140 }
@@ -160,8 +164,7 @@ public ThreadPoolTaskExecutor taskExecutor() {
160164 @ Bean
161165 public ItemReader <Integer > itemReader () {
162166 return new ItemReader <Integer >() {
163- private AtomicInteger atomicInteger = new AtomicInteger ();
164-
167+ private final AtomicInteger atomicInteger = new AtomicInteger ();
165168 @ Override
166169 public synchronized Integer read () {
167170 int value = atomicInteger .incrementAndGet ();
@@ -170,11 +173,6 @@ public synchronized Integer read() {
170173 };
171174 }
172175
173- @ Bean
174- public ItemWriter <Integer > itemWriter () {
175- return items -> {
176- };
177- }
178176 }
179177
180178 @ Configuration
@@ -196,8 +194,13 @@ public DataSource dataSource() {
196194 public static class TransactionManagerConfiguration {
197195
198196 @ Bean
199- public PlatformTransactionManager transactionManager (DataSource dataSource ) {
200- return new DataSourceTransactionManager (dataSource );
197+ public BatchConfigurer batchConfigurer (DataSource dataSource ) {
198+ return new DefaultBatchConfigurer (dataSource ) {
199+ @ Override
200+ public PlatformTransactionManager getTransactionManager () {
201+ return new DataSourceTransactionManager (dataSource );
202+ }
203+ };
201204 }
202205
203206 }
@@ -207,14 +210,19 @@ public PlatformTransactionManager transactionManager(DataSource dataSource) {
207210 public static class CommitFailingTransactionManagerConfiguration {
208211
209212 @ Bean
210- public PlatformTransactionManager transactionManager (DataSource dataSource ) {
211- return new DataSourceTransactionManager (dataSource ) {
213+ public BatchConfigurer batchConfigurer (DataSource dataSource ) {
214+ return new DefaultBatchConfigurer (dataSource ) {
212215 @ Override
213- protected void doCommit (DefaultTransactionStatus status ) {
214- super .doCommit (status );
215- if (Thread .currentThread ().getName ().equals ("spring-batch-worker-thread-2" )) {
216- throw new RuntimeException ("Planned commit exception!" );
217- }
216+ public PlatformTransactionManager getTransactionManager () {
217+ return new DataSourceTransactionManager (dataSource ) {
218+ @ Override
219+ protected void doCommit (DefaultTransactionStatus status ) {
220+ super .doCommit (status );
221+ if (Thread .currentThread ().getName ().equals ("spring-batch-worker-thread-2" )) {
222+ throw new RuntimeException ("Planned commit exception!" );
223+ }
224+ }
225+ };
218226 }
219227 };
220228 }
@@ -226,22 +234,27 @@ protected void doCommit(DefaultTransactionStatus status) {
226234 public static class RollbackFailingTransactionManagerConfiguration {
227235
228236 @ Bean
229- public PlatformTransactionManager transactionManager (DataSource dataSource ) {
230- return new DataSourceTransactionManager (dataSource ) {
237+ public BatchConfigurer batchConfigurer (DataSource dataSource ) {
238+ return new DefaultBatchConfigurer (dataSource ) {
231239 @ Override
232- protected void doCommit (DefaultTransactionStatus status ) {
233- super .doCommit (status );
234- if (Thread .currentThread ().getName ().equals ("spring-batch-worker-thread-2" )) {
235- throw new RuntimeException ("Planned commit exception!" );
236- }
237- }
240+ public PlatformTransactionManager getTransactionManager () {
241+ return new DataSourceTransactionManager (dataSource ) {
242+ @ Override
243+ protected void doCommit (DefaultTransactionStatus status ) {
244+ super .doCommit (status );
245+ if (Thread .currentThread ().getName ().equals ("spring-batch-worker-thread-2" )) {
246+ throw new RuntimeException ("Planned commit exception!" );
247+ }
248+ }
238249
239- @ Override
240- protected void doRollback (DefaultTransactionStatus status ) {
241- super .doRollback (status );
242- if (Thread .currentThread ().getName ().equals ("spring-batch-worker-thread-2" )) {
243- throw new RuntimeException ("Planned rollback exception!" );
244- }
250+ @ Override
251+ protected void doRollback (DefaultTransactionStatus status ) {
252+ super .doRollback (status );
253+ if (Thread .currentThread ().getName ().equals ("spring-batch-worker-thread-2" )) {
254+ throw new RuntimeException ("Planned rollback exception!" );
255+ }
256+ }
257+ };
245258 }
246259 };
247260 }
0 commit comments