11package zin .rashidi .boot .batch .rest .user ;
22
33import java .net .MalformedURLException ;
4-
5- import org . springframework . batch . core . Job ;
6- import org .springframework .batch .core .Step ;
4+ import tools . jackson . databind . DeserializationFeature ;
5+ import tools . jackson . databind . json . JsonMapper ;
6+ import org .springframework .batch .core .job . Job ;
77import org .springframework .batch .core .job .builder .JobBuilder ;
88import org .springframework .batch .core .repository .JobRepository ;
9+ import org .springframework .batch .core .step .Step ;
910import org .springframework .batch .core .step .builder .StepBuilder ;
10- import org .springframework .batch .item .data .MongoItemWriter ;
11- import org .springframework .batch .item .data .builder .MongoItemWriterBuilder ;
12- import org .springframework .batch .item .json .JacksonJsonObjectReader ;
13- import org .springframework .batch .item .json .JsonItemReader ;
14- import org .springframework .batch .item .json .builder .JsonItemReaderBuilder ;
11+ import org .springframework .batch .infrastructure .item .data .MongoItemWriter ;
12+ import org .springframework .batch .infrastructure .item .data .builder .MongoItemWriterBuilder ;
13+ import org .springframework .batch .infrastructure .item .json .JacksonJsonObjectReader ;
14+ import org .springframework .batch .infrastructure .item .json .JsonItemReader ;
15+ import org .springframework .batch .infrastructure .item .json .builder .JsonItemReaderBuilder ;
16+ import org .springframework .beans .factory .annotation .Value ;
1517import org .springframework .context .annotation .Bean ;
1618import org .springframework .context .annotation .Configuration ;
17- import org .springframework .core .io .UrlResource ;
19+ import org .springframework .core .io .Resource ;
1820import org .springframework .data .mongodb .core .MongoOperations ;
1921import org .springframework .transaction .PlatformTransactionManager ;
2022
21- import com .fasterxml .jackson .databind .ObjectMapper ;
22-
2323/**
2424 * @author Rashidi Zin
2525 */
2626@ Configuration
2727class UserJobConfiguration {
2828
29- private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper ();
29+ private static final JsonMapper OBJECT_MAPPER = JsonMapper .builder ()
30+ .disable (DeserializationFeature .FAIL_ON_TRAILING_TOKENS )
31+ .build ();
3032
3133 private final JobRepository jobRepository ;
3234 private final PlatformTransactionManager transactionManager ;
3335 private final MongoOperations mongo ;
36+ private final Resource usersResource ;
3437
35- UserJobConfiguration (JobRepository jobRepository , PlatformTransactionManager transactionManager , MongoOperations mongo ) {
38+ UserJobConfiguration (JobRepository jobRepository , PlatformTransactionManager transactionManager , MongoOperations mongo ,
39+ @ Value ("${batch.users.resource:https://jsonplaceholder.typicode.com/users}" ) Resource usersResource ) {
3640 this .jobRepository = jobRepository ;
3741 this .transactionManager = transactionManager ;
3842 this .mongo = mongo ;
43+ this .usersResource = usersResource ;
3944 }
4045
4146 @ Bean
@@ -45,21 +50,22 @@ public Job userJob() throws MalformedURLException {
4550
4651 private Step step () throws MalformedURLException {
4752 return new StepBuilder ("userStep" , jobRepository )
48- .<User , User >chunk (10 , transactionManager )
53+ .<User , User >chunk (10 )
54+ .transactionManager (transactionManager )
4955 .reader (reader ())
5056 .writer (writer ())
5157 .build ();
5258 }
5359
54- private JsonItemReader <User > reader () throws MalformedURLException {
60+ private JsonItemReader <User > reader () {
5561 JacksonJsonObjectReader <User > jsonObjectReader = new JacksonJsonObjectReader <>(User .class );
5662
5763 jsonObjectReader .setMapper (OBJECT_MAPPER );
5864
5965 return new JsonItemReaderBuilder <User >()
6066 .name ("userReader" )
6167 .jsonObjectReader (jsonObjectReader )
62- .resource (new UrlResource ( "https://jsonplaceholder.typicode.com/users" ) )
68+ .resource (usersResource )
6369 .build ();
6470 }
6571
0 commit comments