|
| 1 | +/* |
| 2 | + * Copyright 2018 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.batch.core.step.item; |
| 17 | + |
| 18 | +import org.junit.Test; |
| 19 | +import org.springframework.batch.core.BatchStatus; |
| 20 | +import org.springframework.batch.core.Job; |
| 21 | +import org.springframework.batch.core.JobExecution; |
| 22 | +import org.springframework.batch.core.JobParameters; |
| 23 | +import org.springframework.batch.core.StepExecution; |
| 24 | +import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; |
| 25 | +import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; |
| 26 | +import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; |
| 27 | +import org.springframework.batch.core.launch.JobLauncher; |
| 28 | +import org.springframework.batch.core.listener.JobExecutionListenerSupport; |
| 29 | +import org.springframework.batch.core.step.tasklet.TaskletStep; |
| 30 | +import org.springframework.batch.item.ItemReader; |
| 31 | +import org.springframework.batch.item.ItemWriter; |
| 32 | +import org.springframework.beans.factory.annotation.Autowired; |
| 33 | +import org.springframework.context.ApplicationContext; |
| 34 | +import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
| 35 | +import org.springframework.context.annotation.Bean; |
| 36 | +import org.springframework.context.annotation.Configuration; |
| 37 | +import org.springframework.context.annotation.Import; |
| 38 | +import org.springframework.jdbc.datasource.DataSourceTransactionManager; |
| 39 | +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; |
| 40 | +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; |
| 41 | +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; |
| 42 | +import org.springframework.transaction.PlatformTransactionManager; |
| 43 | +import org.springframework.transaction.support.DefaultTransactionStatus; |
| 44 | + |
| 45 | +import javax.sql.DataSource; |
| 46 | +import java.util.concurrent.atomic.AtomicInteger; |
| 47 | + |
| 48 | +import static org.junit.Assert.assertEquals; |
| 49 | +import static org.junit.Assert.assertNotNull; |
| 50 | + |
| 51 | +/** |
| 52 | + * Tests for the behavior of a multi-threaded TaskletStep. |
| 53 | + * |
| 54 | + * @author Mahmoud Ben Hassine |
| 55 | + */ |
| 56 | +public class MultiThreadedTaskletStepIntegrationTests { |
| 57 | + |
| 58 | + @Test |
| 59 | + public void testMultiThreadedTaskletExecutionWhenNoErrors() throws Exception { |
| 60 | + // given |
| 61 | + Class[] configurationClasses = {JobConfiguration.class, TransactionManagerConfiguration.class}; |
| 62 | + ApplicationContext context = new AnnotationConfigApplicationContext(configurationClasses); |
| 63 | + JobLauncher jobLauncher = context.getBean(JobLauncher.class); |
| 64 | + Job job = context.getBean(Job.class); |
| 65 | + |
| 66 | + // when |
| 67 | + JobExecution jobExecution = jobLauncher.run(job, new JobParameters()); |
| 68 | + |
| 69 | + // then |
| 70 | + assertNotNull(jobExecution); |
| 71 | + assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus()); |
| 72 | + StepExecution stepExecution = jobExecution.getStepExecutions().iterator().next(); |
| 73 | + assertEquals(BatchStatus.COMPLETED, stepExecution.getStatus()); |
| 74 | + assertEquals(0, stepExecution.getFailureExceptions().size()); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testMultiThreadedTaskletExecutionWhenCommitFails() throws Exception { |
| 79 | + // given |
| 80 | + Class[] configurationClasses = {JobConfiguration.class, CommitFailingTransactionManagerConfiguration.class}; |
| 81 | + ApplicationContext context = new AnnotationConfigApplicationContext(configurationClasses); |
| 82 | + JobLauncher jobLauncher = context.getBean(JobLauncher.class); |
| 83 | + Job job = context.getBean(Job.class); |
| 84 | + |
| 85 | + // when |
| 86 | + JobExecution jobExecution = jobLauncher.run(job, new JobParameters()); |
| 87 | + |
| 88 | + // then |
| 89 | + assertNotNull(jobExecution); |
| 90 | + assertEquals(BatchStatus.FAILED, jobExecution.getStatus()); |
| 91 | + StepExecution stepExecution = jobExecution.getStepExecutions().iterator().next(); |
| 92 | + assertEquals(BatchStatus.FAILED, stepExecution.getStatus()); |
| 93 | + Throwable e = stepExecution.getFailureExceptions().get(0); |
| 94 | + assertEquals("Planned commit exception!", e.getMessage()); |
| 95 | + // No assertions on execution context because it is undefined in this case |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void testMultiThreadedTaskletExecutionWhenRollbackFails() throws Exception { |
| 100 | + // given |
| 101 | + Class[] configurationClasses = {JobConfiguration.class, RollbackFailingTransactionManagerConfiguration.class}; |
| 102 | + ApplicationContext context = new AnnotationConfigApplicationContext(configurationClasses); |
| 103 | + JobLauncher jobLauncher = context.getBean(JobLauncher.class); |
| 104 | + Job job = context.getBean(Job.class); |
| 105 | + |
| 106 | + // when |
| 107 | + JobExecution jobExecution = jobLauncher.run(job, new JobParameters()); |
| 108 | + |
| 109 | + // then |
| 110 | + assertNotNull(jobExecution); |
| 111 | + assertEquals(BatchStatus.UNKNOWN, jobExecution.getStatus()); |
| 112 | + StepExecution stepExecution = jobExecution.getStepExecutions().iterator().next(); |
| 113 | + assertEquals(BatchStatus.UNKNOWN, stepExecution.getStatus()); |
| 114 | + Throwable e = stepExecution.getFailureExceptions().get(0); |
| 115 | + assertEquals("Planned rollback exception!", e.getMessage()); |
| 116 | + // No assertions on execution context because it is undefined in this case |
| 117 | + } |
| 118 | + |
| 119 | + @Configuration |
| 120 | + @EnableBatchProcessing |
| 121 | + public static class JobConfiguration { |
| 122 | + |
| 123 | + @Autowired |
| 124 | + private JobBuilderFactory jobBuilderFactory; |
| 125 | + @Autowired |
| 126 | + private StepBuilderFactory stepBuilderFactory; |
| 127 | + |
| 128 | + @Bean |
| 129 | + public TaskletStep step() { |
| 130 | + return stepBuilderFactory.get("step") |
| 131 | + .<Integer, Integer>chunk(3) |
| 132 | + .reader(itemReader()) |
| 133 | + .writer(itemWriter()) |
| 134 | + .taskExecutor(taskExecutor()) |
| 135 | + .build(); |
| 136 | + } |
| 137 | + |
| 138 | + @Bean |
| 139 | + public Job job(ThreadPoolTaskExecutor taskExecutor) { |
| 140 | + return jobBuilderFactory.get("job") |
| 141 | + .start(step()) |
| 142 | + .listener(new JobExecutionListenerSupport() { |
| 143 | + @Override |
| 144 | + public void afterJob(JobExecution jobExecution) { |
| 145 | + taskExecutor.shutdown(); |
| 146 | + } |
| 147 | + }) |
| 148 | + .build(); |
| 149 | + } |
| 150 | + |
| 151 | + @Bean |
| 152 | + public ThreadPoolTaskExecutor taskExecutor() { |
| 153 | + ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor(); |
| 154 | + taskExecutor.setCorePoolSize(3); |
| 155 | + taskExecutor.setMaxPoolSize(3); |
| 156 | + taskExecutor.setThreadNamePrefix("spring-batch-worker-thread-"); |
| 157 | + return taskExecutor; |
| 158 | + } |
| 159 | + |
| 160 | + @Bean |
| 161 | + public ItemReader<Integer> itemReader() { |
| 162 | + return new ItemReader<Integer>() { |
| 163 | + private AtomicInteger atomicInteger = new AtomicInteger(); |
| 164 | + |
| 165 | + @Override |
| 166 | + public synchronized Integer read() { |
| 167 | + int value = atomicInteger.incrementAndGet(); |
| 168 | + return value <= 9 ? value : null; |
| 169 | + } |
| 170 | + }; |
| 171 | + } |
| 172 | + |
| 173 | + @Bean |
| 174 | + public ItemWriter<Integer> itemWriter() { |
| 175 | + return items -> { |
| 176 | + }; |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + @Configuration |
| 181 | + public static class DataSourceConfiguration { |
| 182 | + |
| 183 | + @Bean |
| 184 | + public DataSource dataSource() { |
| 185 | + return new EmbeddedDatabaseBuilder() |
| 186 | + .setType(EmbeddedDatabaseType.HSQL) |
| 187 | + .addScript("org/springframework/batch/core/schema-drop-hsqldb.sql") |
| 188 | + .addScript("org/springframework/batch/core/schema-hsqldb.sql") |
| 189 | + .build(); |
| 190 | + } |
| 191 | + |
| 192 | + } |
| 193 | + |
| 194 | + @Configuration |
| 195 | + @Import(DataSourceConfiguration.class) |
| 196 | + public static class TransactionManagerConfiguration { |
| 197 | + |
| 198 | + @Bean |
| 199 | + public PlatformTransactionManager transactionManager(DataSource dataSource) { |
| 200 | + return new DataSourceTransactionManager(dataSource); |
| 201 | + } |
| 202 | + |
| 203 | + } |
| 204 | + |
| 205 | + @Configuration |
| 206 | + @Import(DataSourceConfiguration.class) |
| 207 | + public static class CommitFailingTransactionManagerConfiguration { |
| 208 | + |
| 209 | + @Bean |
| 210 | + public PlatformTransactionManager transactionManager(DataSource dataSource) { |
| 211 | + return new DataSourceTransactionManager(dataSource) { |
| 212 | + @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 | + } |
| 218 | + } |
| 219 | + }; |
| 220 | + } |
| 221 | + |
| 222 | + } |
| 223 | + |
| 224 | + @Configuration |
| 225 | + @Import(DataSourceConfiguration.class) |
| 226 | + public static class RollbackFailingTransactionManagerConfiguration { |
| 227 | + |
| 228 | + @Bean |
| 229 | + public PlatformTransactionManager transactionManager(DataSource dataSource) { |
| 230 | + return new DataSourceTransactionManager(dataSource) { |
| 231 | + @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 | + } |
| 238 | + |
| 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 | + } |
| 245 | + } |
| 246 | + }; |
| 247 | + } |
| 248 | + |
| 249 | + } |
| 250 | + |
| 251 | +} |
0 commit comments