1
+ package org .example ;
2
+
3
+ import lombok .RequiredArgsConstructor ;
4
+ import org .springframework .batch .core .*;
5
+ import org .springframework .batch .core .configuration .annotation .EnableBatchProcessing ;
6
+ import org .springframework .batch .core .configuration .annotation .JobBuilderFactory ;
7
+ import org .springframework .batch .core .configuration .annotation .StepBuilderFactory ;
8
+ import org .springframework .batch .core .launch .JobLauncher ;
9
+ import org .springframework .batch .core .launch .support .RunIdIncrementer ;
10
+ import org .springframework .batch .core .repository .JobExecutionAlreadyRunningException ;
11
+ import org .springframework .batch .core .repository .JobInstanceAlreadyCompleteException ;
12
+ import org .springframework .batch .core .repository .JobRestartException ;
13
+ import org .springframework .batch .extensions .excel .mapping .BeanWrapperRowMapper ;
14
+ import org .springframework .batch .extensions .excel .streaming .StreamingXlsxItemReader ;
15
+ import org .springframework .boot .SpringApplication ;
16
+ import org .springframework .boot .autoconfigure .SpringBootApplication ;
17
+ import org .springframework .context .annotation .Bean ;
18
+ import org .springframework .context .annotation .Configuration ;
19
+ import org .springframework .core .io .FileSystemResource ;
20
+
21
+ import java .net .URL ;
22
+ import java .util .Objects ;
23
+
24
+ @ Configuration
25
+ @ SpringBootApplication
26
+ @ EnableBatchProcessing
27
+ @ RequiredArgsConstructor
28
+ public class RowByIndexNotSupportedReprod
29
+ {
30
+ private final JobBuilderFactory jobBuilderFactory ;
31
+ private final StepBuilderFactory stepBuilderFactory ;
32
+ private final JobLauncher jobLauncher ;
33
+ public static void main (String [] args )
34
+ {
35
+ SpringApplication .run (RowByIndexNotSupportedReprod .class , args );
36
+ }
37
+
38
+ @ Bean
39
+ public JobExecution createJob () throws JobInstanceAlreadyCompleteException , JobExecutionAlreadyRunningException , JobParametersInvalidException , JobRestartException
40
+ {
41
+ Job job = jobBuilderFactory .get ("a_job" )
42
+ .incrementer (new RunIdIncrementer ())
43
+ .flow (createStep ())
44
+ .end ().build ();
45
+ return jobLauncher .run (job , new JobParameters ());
46
+ }
47
+
48
+ public Step createStep ()
49
+ {
50
+ BeanWrapperRowMapper <MyIn > mapper = new BeanWrapperRowMapper <>();
51
+ mapper .setTargetType (MyIn .class );
52
+ StreamingXlsxItemReader <MyIn > reader = new StreamingXlsxItemReader <>();
53
+
54
+ FileSystemResource resource = resource ("input.xlsx" );
55
+
56
+ reader .setResource (resource );
57
+ reader .setRowMapper (mapper );
58
+ reader .setLinesToSkip (1 );
59
+
60
+ return stepBuilderFactory .get ("a_step" )
61
+ .<MyIn , MyOut >chunk (10 )
62
+ .reader (reader )
63
+ .writer (System .out ::println )
64
+ .build ();
65
+ }
66
+
67
+ private static FileSystemResource resource (String name )
68
+ {
69
+ URL url = RowByIndexNotSupportedReprod .class
70
+ .getClassLoader ()
71
+ .getResource (name );
72
+
73
+ String file = Objects .requireNonNull (url ).getFile ();
74
+
75
+ return new FileSystemResource (file );
76
+ }
77
+ }
78
+
79
+ class MyIn {}
80
+ class MyOut {}
0 commit comments