forked from spring-projects/spring-batch
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make the meaning of running status consistent
- A running status is STARTING, STARTED, or STOPPING. Resolves spring-projects#1483
- Loading branch information
Showing
9 changed files
with
130 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
...rc/test/java/org/springframework/batch/core/launch/SimpleJobLauncherIntegrationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* Copyright 2006-2022 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.batch.core.launch; | ||
|
||
import java.util.Date; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import javax.sql.DataSource; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import org.springframework.batch.core.Job; | ||
import org.springframework.batch.core.JobParameters; | ||
import org.springframework.batch.core.JobParametersBuilder; | ||
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; | ||
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; | ||
import org.springframework.batch.core.repository.JobRepository; | ||
import org.springframework.batch.core.step.tasklet.TaskletStep; | ||
import org.springframework.batch.core.test.AbstractIntegrationTests; | ||
import org.springframework.batch.repeat.RepeatStatus; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||
import org.springframework.transaction.PlatformTransactionManager; | ||
|
||
/** | ||
* @author Marvin Deng | ||
*/ | ||
@RunWith(SpringJUnit4ClassRunner.class) | ||
@ContextConfiguration(locations = {"/simple-job-launcher-context.xml"}) | ||
public class SimpleJobLauncherIntegrationTests extends AbstractIntegrationTests { | ||
|
||
@Autowired | ||
private JobLauncher jobLauncher; | ||
|
||
@Autowired | ||
private JobRepository jobRepository; | ||
|
||
@Autowired | ||
private PlatformTransactionManager transactionManager; | ||
|
||
@Autowired | ||
public void setDataSource(DataSource dataSource) { | ||
this.dataSource = dataSource; | ||
} | ||
|
||
@Test | ||
public void testLaunchWithSameParametersInMultiThreads() throws Exception { | ||
JobBuilderFactory jobBuilderFactory = new JobBuilderFactory(jobRepository); | ||
StepBuilderFactory stepBuilderFactory = new StepBuilderFactory(jobRepository, transactionManager); | ||
|
||
TaskletStep step = stepBuilderFactory.get("testStep") | ||
.tasklet((contribution, chunkContext) -> RepeatStatus.FINISHED) | ||
.build(); | ||
Job job = jobBuilderFactory.get("testJob").start(step).build(); | ||
JobParameters jobParameters = new JobParametersBuilder() | ||
.addDate("now", new Date()) | ||
.toJobParameters(); | ||
|
||
AtomicInteger succeedCount = new AtomicInteger(0); | ||
AtomicInteger failedCount = new AtomicInteger(0); | ||
final int count = 10; | ||
CountDownLatch latch = new CountDownLatch(count); | ||
ExecutorService executorService = Executors.newFixedThreadPool(count); | ||
for (int i = 0; i < count; i++) { | ||
executorService.submit(() -> { | ||
try { | ||
jobLauncher.run(job, jobParameters); | ||
succeedCount.incrementAndGet(); | ||
} | ||
catch (Exception e) { | ||
failedCount.incrementAndGet(); | ||
} | ||
finally { | ||
latch.countDown(); | ||
} | ||
}); | ||
} | ||
executorService.shutdown(); | ||
latch.await(); | ||
Assert.assertEquals(1, succeedCount.intValue()); | ||
Assert.assertEquals(count - 1, failedCount.intValue()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters