11/*
2- * Copyright 2006-2025 the original author or authors.
2+ * Copyright 2006-present 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.
1616
1717package org .springframework .batch .samples .restart .stop ;
1818
19+ import java .util .List ;
20+
21+ import javax .sql .DataSource ;
22+
1923import org .apache .commons .logging .Log ;
2024import org .apache .commons .logging .LogFactory ;
21- import org .junit .jupiter .api .Disabled ;
2225import org .junit .jupiter .api .Test ;
26+ import org .junit .jupiter .api .extension .ExtendWith ;
2327
2428import org .springframework .batch .core .BatchStatus ;
29+ import org .springframework .batch .core .configuration .annotation .EnableBatchProcessing ;
30+ import org .springframework .batch .core .configuration .annotation .EnableJdbcJobRepository ;
31+ import org .springframework .batch .core .job .Job ;
2532import org .springframework .batch .core .job .JobExecution ;
33+ import org .springframework .batch .core .job .builder .JobBuilder ;
2634import org .springframework .batch .core .job .parameters .JobParameters ;
27- import org .springframework .batch .core .job .parameters .JobParametersBuilder ;
2835import org .springframework .batch .core .launch .JobOperator ;
29- import org .springframework .batch .test .JobOperatorTestUtils ;
36+ import org .springframework .batch .core .repository .JobRepository ;
37+ import org .springframework .batch .core .step .Step ;
38+ import org .springframework .batch .core .step .builder .ChunkOrientedStepBuilder ;
39+ import org .springframework .batch .infrastructure .item .support .ListItemReader ;
40+ import org .springframework .batch .infrastructure .item .support .ListItemWriter ;
3041import org .springframework .beans .factory .annotation .Autowired ;
31- import org .springframework .test .context .junit .jupiter .SpringJUnitConfig ;
42+ import org .springframework .context .annotation .Bean ;
43+ import org .springframework .context .annotation .Configuration ;
44+ import org .springframework .core .task .AsyncTaskExecutor ;
45+ import org .springframework .core .task .SimpleAsyncTaskExecutor ;
46+ import org .springframework .jdbc .datasource .embedded .EmbeddedDatabaseBuilder ;
47+ import org .springframework .jdbc .datasource .embedded .EmbeddedDatabaseType ;
48+ import org .springframework .jdbc .support .JdbcTransactionManager ;
49+ import org .springframework .test .context .junit .jupiter .SpringExtension ;
3250
3351import static org .junit .jupiter .api .Assertions .assertEquals ;
3452import static org .junit .jupiter .api .Assertions .assertFalse ;
4462 * @author Mahmoud Ben Hassine
4563 *
4664 */
47- @ SpringJUnitConfig (locations = { "/org/springframework/batch/samples/restart/stop/stopRestartSample.xml" })
48- @ Disabled
49- // FIXME passes in the IDE but not on the CLI - needs investigation
65+ @ ExtendWith (SpringExtension .class )
5066class GracefulShutdownFunctionalTests {
5167
5268 /** Logger */
5369 private final Log logger = LogFactory .getLog (getClass ());
5470
55- @ Autowired
56- private JobOperatorTestUtils jobOperatorTestUtils ;
57-
58- @ Autowired
59- private JobOperator jobOperator ;
60-
6171 @ Test
62- void testLaunchJob () throws Exception {
63- final JobParameters jobParameters = new JobParametersBuilder ().addLong ("timestamp" , System .currentTimeMillis ())
64- .toJobParameters ();
65-
66- JobExecution jobExecution = jobOperatorTestUtils .startJob (jobParameters );
72+ void testStopJob (@ Autowired Job job , @ Autowired JobOperator jobOperator ) throws Exception {
73+ JobExecution jobExecution = jobOperator .start (job , new JobParameters ());
6774
6875 Thread .sleep (1000 );
6976
@@ -83,4 +90,51 @@ void testLaunchJob() throws Exception {
8390 assertEquals (BatchStatus .STOPPED , jobExecution .getStatus ());
8491 }
8592
93+ @ Configuration
94+ @ EnableBatchProcessing (taskExecutorRef = "batchTaskExecutor" )
95+ @ EnableJdbcJobRepository
96+ static class JobConfiguration {
97+
98+ @ Bean
99+ public Job job (JobRepository jobRepository , Step step ) {
100+ return new JobBuilder (jobRepository ).start (step ).build ();
101+ }
102+
103+ @ Bean
104+ public Step chunkOrientedStep (JobRepository jobRepository , JdbcTransactionManager transactionManager ) {
105+ return new ChunkOrientedStepBuilder <String , String >(jobRepository , 2 )
106+ .reader (new ListItemReader <>(List .of ("item1" , "item2" , "item3" , "item4" , "item5" )))
107+ .processor (item -> {
108+ // Simulate processing time
109+ Thread .sleep (500 );
110+ return item .toUpperCase ();
111+ })
112+ .writer (new ListItemWriter <>())
113+ .transactionManager (transactionManager )
114+ .build ();
115+ }
116+
117+ @ Bean
118+ public DataSource dataSource () {
119+ return new EmbeddedDatabaseBuilder ().setType (EmbeddedDatabaseType .HSQL )
120+ .addScript ("/org/springframework/batch/core/schema-drop-hsqldb.sql" )
121+ .addScript ("/org/springframework/batch/core/schema-hsqldb.sql" )
122+ .generateUniqueName (true )
123+ .build ();
124+ }
125+
126+ @ Bean
127+ public JdbcTransactionManager transactionManager (DataSource dataSource ) {
128+ return new JdbcTransactionManager (dataSource );
129+ }
130+
131+ @ Bean
132+ public AsyncTaskExecutor batchTaskExecutor () {
133+ SimpleAsyncTaskExecutor asyncTaskExecutor = new SimpleAsyncTaskExecutor ("batch-executor" );
134+ asyncTaskExecutor .setConcurrencyLimit (1 );
135+ return asyncTaskExecutor ;
136+ }
137+
138+ }
139+
86140}
0 commit comments