1
+ /*
2
+ * Copyright 2018-2023 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
+ * https://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 .sample .file .json ;
17
+
18
+ import javax .sql .DataSource ;
19
+
20
+ import org .springframework .batch .core .Job ;
21
+ import org .springframework .batch .core .Step ;
22
+ import org .springframework .batch .core .configuration .annotation .EnableBatchProcessing ;
23
+ import org .springframework .batch .core .configuration .annotation .StepScope ;
24
+ import org .springframework .batch .core .job .builder .JobBuilder ;
25
+ import org .springframework .batch .core .repository .JobRepository ;
26
+ import org .springframework .batch .core .step .builder .StepBuilder ;
27
+ import org .springframework .batch .item .json .GsonJsonObjectReader ;
28
+ import org .springframework .batch .item .json .JacksonJsonObjectMarshaller ;
29
+ import org .springframework .batch .item .json .JsonFileItemWriter ;
30
+ import org .springframework .batch .item .json .JsonItemReader ;
31
+ import org .springframework .batch .item .json .builder .JsonFileItemWriterBuilder ;
32
+ import org .springframework .batch .item .json .builder .JsonItemReaderBuilder ;
33
+ import org .springframework .batch .sample .domain .trade .Trade ;
34
+ import org .springframework .beans .factory .annotation .Value ;
35
+ import org .springframework .context .annotation .Bean ;
36
+ import org .springframework .context .annotation .Configuration ;
37
+ import org .springframework .core .io .Resource ;
38
+ import org .springframework .core .io .WritableResource ;
39
+ import org .springframework .jdbc .datasource .embedded .EmbeddedDatabaseBuilder ;
40
+ import org .springframework .jdbc .support .JdbcTransactionManager ;
41
+
42
+ /**
43
+ * @author Mahmoud Ben Hassine
44
+ */
45
+ @ Configuration
46
+ @ EnableBatchProcessing
47
+ public class JsonJobConfiguration {
48
+
49
+ @ Bean
50
+ @ StepScope
51
+ public JsonItemReader <Trade > itemReader (@ Value ("#{jobParameters[inputFile]}" ) Resource resource ) {
52
+ return new JsonItemReaderBuilder <Trade >().name ("tradesJsonItemReader" )
53
+ .resource (resource )
54
+ .jsonObjectReader (new GsonJsonObjectReader <>(Trade .class ))
55
+ .build ();
56
+ }
57
+
58
+ @ Bean
59
+ @ StepScope
60
+ public JsonFileItemWriter <Trade > itemWriter (@ Value ("#{jobParameters[outputFile]}" ) WritableResource resource ) {
61
+ return new JsonFileItemWriterBuilder <Trade >().resource (resource )
62
+ .lineSeparator ("\n " )
63
+ .jsonObjectMarshaller (new JacksonJsonObjectMarshaller <>())
64
+ .name ("tradesJsonFileItemWriter" )
65
+ .shouldDeleteIfExists (true )
66
+ .build ();
67
+ }
68
+
69
+ @ Bean
70
+ public Step step (JobRepository jobRepository , JdbcTransactionManager transactionManager ,
71
+ JsonItemReader <Trade > itemReader , JsonFileItemWriter <Trade > itemWriter ) {
72
+ return new StepBuilder ("step" , jobRepository ).<Trade , Trade >chunk (2 , transactionManager )
73
+ .reader (itemReader )
74
+ .writer (itemWriter )
75
+ .build ();
76
+ }
77
+
78
+ @ Bean
79
+ public Job job (JobRepository jobRepository , Step step ) {
80
+ return new JobBuilder ("job" , jobRepository ).start (step ).build ();
81
+ }
82
+
83
+ // Infrastructure beans
84
+
85
+ @ Bean
86
+ public DataSource dataSource () {
87
+ return new EmbeddedDatabaseBuilder ().addScript ("/org/springframework/batch/core/schema-drop-hsqldb.sql" )
88
+ .addScript ("/org/springframework/batch/core/schema-hsqldb.sql" )
89
+ .generateUniqueName (true )
90
+ .build ();
91
+ }
92
+
93
+ @ Bean
94
+ public JdbcTransactionManager transactionManager (DataSource dataSource ) {
95
+ return new JdbcTransactionManager (dataSource );
96
+ }
97
+
98
+ }
0 commit comments