-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
94 additions
and
90 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
2 changes: 1 addition & 1 deletion
2
...batch/BatchRestRepositoryApplication.java → .../rest/BatchRestRepositoryApplication.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
28 changes: 14 additions & 14 deletions
28
...ava/zin/rashidi/boot/batch/user/User.java → ...in/rashidi/boot/batch/rest/user/User.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 |
---|---|---|
@@ -1,14 +1,14 @@ | ||
package zin.rashidi.boot.batch.user; | ||
|
||
import org.springframework.data.mongodb.core.mapping.Document; | ||
import org.springframework.data.mongodb.core.mapping.MongoId; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
|
||
/** | ||
* @author Rashidi Zin | ||
*/ | ||
@Document | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
record User(@MongoId Long id, String username) { | ||
} | ||
package zin.rashidi.boot.batch.rest.user; | ||
|
||
import org.springframework.data.mongodb.core.mapping.Document; | ||
import org.springframework.data.mongodb.core.mapping.MongoId; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
|
||
/** | ||
* @author Rashidi Zin | ||
*/ | ||
@Document | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
record User(@MongoId Long id, String username) { | ||
} |
140 changes: 70 additions & 70 deletions
140
...boot/batch/user/UserJobConfiguration.java → ...batch/rest/user/UserJobConfiguration.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 |
---|---|---|
@@ -1,70 +1,70 @@ | ||
package zin.rashidi.boot.batch.user; | ||
|
||
import java.net.MalformedURLException; | ||
|
||
import org.springframework.batch.core.Job; | ||
import org.springframework.batch.core.Step; | ||
import org.springframework.batch.core.job.builder.JobBuilder; | ||
import org.springframework.batch.core.repository.JobRepository; | ||
import org.springframework.batch.core.step.builder.StepBuilder; | ||
import org.springframework.batch.item.data.MongoItemWriter; | ||
import org.springframework.batch.item.data.builder.MongoItemWriterBuilder; | ||
import org.springframework.batch.item.json.JacksonJsonObjectReader; | ||
import org.springframework.batch.item.json.JsonItemReader; | ||
import org.springframework.batch.item.json.builder.JsonItemReaderBuilder; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.io.UrlResource; | ||
import org.springframework.data.mongodb.core.MongoOperations; | ||
import org.springframework.transaction.PlatformTransactionManager; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
/** | ||
* @author Rashidi Zin | ||
*/ | ||
@Configuration | ||
class UserJobConfiguration { | ||
|
||
private final JobRepository jobRepository; | ||
private final PlatformTransactionManager transactionManager; | ||
private final MongoOperations mongo; | ||
|
||
UserJobConfiguration(JobRepository jobRepository, PlatformTransactionManager transactionManager, MongoOperations mongo) { | ||
this.jobRepository = jobRepository; | ||
this.transactionManager = transactionManager; | ||
this.mongo = mongo; | ||
} | ||
|
||
@Bean | ||
public Job userJob() throws MalformedURLException { | ||
return new JobBuilder("userJob", jobRepository).start(step()).build(); | ||
} | ||
|
||
private Step step() throws MalformedURLException { | ||
return new StepBuilder("userStep", jobRepository) | ||
.<User, User>chunk(10, transactionManager) | ||
.reader(reader()) | ||
.writer(writer()) | ||
.build(); | ||
} | ||
|
||
private JsonItemReader<User> reader() throws MalformedURLException { | ||
JacksonJsonObjectReader<User> jsonObjectReader = new JacksonJsonObjectReader<>(User.class); | ||
|
||
jsonObjectReader.setMapper(new ObjectMapper()); | ||
|
||
return new JsonItemReaderBuilder<User>() | ||
.name("userReader") | ||
.jsonObjectReader(jsonObjectReader) | ||
.resource(new UrlResource("https://jsonplaceholder.typicode.com/users")) | ||
.build(); | ||
} | ||
|
||
private MongoItemWriter<User> writer() { | ||
return new MongoItemWriterBuilder<User>() | ||
.template(mongo) | ||
.build(); | ||
} | ||
|
||
} | ||
package zin.rashidi.boot.batch.rest.user; | ||
|
||
import java.net.MalformedURLException; | ||
|
||
import org.springframework.batch.core.Job; | ||
import org.springframework.batch.core.Step; | ||
import org.springframework.batch.core.job.builder.JobBuilder; | ||
import org.springframework.batch.core.repository.JobRepository; | ||
import org.springframework.batch.core.step.builder.StepBuilder; | ||
import org.springframework.batch.item.data.MongoItemWriter; | ||
import org.springframework.batch.item.data.builder.MongoItemWriterBuilder; | ||
import org.springframework.batch.item.json.JacksonJsonObjectReader; | ||
import org.springframework.batch.item.json.JsonItemReader; | ||
import org.springframework.batch.item.json.builder.JsonItemReaderBuilder; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.io.UrlResource; | ||
import org.springframework.data.mongodb.core.MongoOperations; | ||
import org.springframework.transaction.PlatformTransactionManager; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
/** | ||
* @author Rashidi Zin | ||
*/ | ||
@Configuration | ||
class UserJobConfiguration { | ||
|
||
private final JobRepository jobRepository; | ||
private final PlatformTransactionManager transactionManager; | ||
private final MongoOperations mongo; | ||
|
||
UserJobConfiguration(JobRepository jobRepository, PlatformTransactionManager transactionManager, MongoOperations mongo) { | ||
this.jobRepository = jobRepository; | ||
this.transactionManager = transactionManager; | ||
this.mongo = mongo; | ||
} | ||
|
||
@Bean | ||
public Job userJob() throws MalformedURLException { | ||
return new JobBuilder("userJob", jobRepository).start(step()).build(); | ||
} | ||
|
||
private Step step() throws MalformedURLException { | ||
return new StepBuilder("userStep", jobRepository) | ||
.<User, User>chunk(10, transactionManager) | ||
.reader(reader()) | ||
.writer(writer()) | ||
.build(); | ||
} | ||
|
||
private JsonItemReader<User> reader() throws MalformedURLException { | ||
JacksonJsonObjectReader<User> jsonObjectReader = new JacksonJsonObjectReader<>(User.class); | ||
|
||
jsonObjectReader.setMapper(new ObjectMapper()); | ||
|
||
return new JsonItemReaderBuilder<User>() | ||
.name("userReader") | ||
.jsonObjectReader(jsonObjectReader) | ||
.resource(new UrlResource("https://jsonplaceholder.typicode.com/users")) | ||
.build(); | ||
} | ||
|
||
private MongoItemWriter<User> writer() { | ||
return new MongoItemWriterBuilder<User>() | ||
.template(mongo) | ||
.build(); | ||
} | ||
|
||
} |
6 changes: 3 additions & 3 deletions
6
...di/boot/batch/user/UserBatchJobTests.java → ...ot/batch/rest/user/UserBatchJobTests.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
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