-
Notifications
You must be signed in to change notification settings - Fork 1
spring batch #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Bael
wants to merge
3
commits into
master
Choose a base branch
from
spring_batch
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
spring batch #6
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
51 changes: 51 additions & 0 deletions
51
kanban-server/src/main/java/ru/otus/spring/hw/kanban/conversion/BoardItemProcessor.java
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package ru.otus.spring.hw.kanban.conversion; | ||
|
|
||
| import org.springframework.batch.item.ItemProcessor; | ||
| import org.springframework.stereotype.Service; | ||
| import ru.otus.spring.hw.kanban.domain.Board; | ||
| import ru.otus.spring.hw.kanban.domain.Stage; | ||
| import ru.otus.spring.hw.kanban.domain.Task; | ||
|
|
||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
| import java.util.function.Function; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| @Service | ||
| public class BoardItemProcessor implements ItemProcessor<Board, ru.otus.spring.hw.kanban.domain.mongo.Board> { | ||
|
|
||
| @Override | ||
| public ru.otus.spring.hw.kanban.domain.mongo.Board process(Board board) throws Exception { | ||
| ru.otus.spring.hw.kanban.domain.mongo.Board mongoBoard = new ru.otus.spring.hw.kanban.domain.mongo.Board(); | ||
| mongoBoard.setName(board.getName()); | ||
|
|
||
| Set<ru.otus.spring.hw.kanban.domain.mongo.Stage> mongoStages = new HashSet<>(); | ||
|
|
||
| Function<Task, ru.otus.spring.hw.kanban.domain.mongo.Task> convert = (task) -> { | ||
| ru.otus.spring.hw.kanban.domain.mongo.Task mongoTask = new ru.otus.spring.hw.kanban.domain.mongo.Task(); | ||
| mongoTask.setDescription(task.getDescription()); | ||
| mongoTask.setExecutor(task.getExecutor()); | ||
| mongoTask.setName(task.getName()); | ||
| mongoTask.setPriority(task.getPriority()); | ||
| return mongoTask; | ||
| }; | ||
|
|
||
| for (Stage stage : board.getStages()) { | ||
| ru.otus.spring.hw.kanban.domain.mongo.Stage mongoStage = new ru.otus.spring.hw.kanban.domain.mongo.Stage(); | ||
| mongoStage.setDescription(stage.getDescription()); | ||
| mongoStage.setName(stage.getName()); | ||
| mongoStage.setTasks( | ||
| stage.getTasks() | ||
| .stream() | ||
| .map(task -> convert.apply(task)) | ||
| .collect(Collectors.toSet()) | ||
| ); | ||
| mongoStages.add(mongoStage); | ||
|
|
||
| } | ||
| mongoBoard.setStages(mongoStages); | ||
| return mongoBoard; | ||
|
|
||
|
|
||
| } | ||
| } |
16 changes: 16 additions & 0 deletions
16
...erver/src/main/java/ru/otus/spring/hw/kanban/conversion/NoPersistenceBatchConfigurer.java
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package ru.otus.spring.hw.kanban.conversion; | ||
|
|
||
| //public class NoPersistenceBatchConfigurer { | ||
| //} | ||
|
|
||
| import org.springframework.batch.core.configuration.annotation.DefaultBatchConfigurer; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import javax.sql.DataSource; | ||
|
|
||
| @Component | ||
| public class NoPersistenceBatchConfigurer extends DefaultBatchConfigurer { | ||
| @Override | ||
| public void setDataSource(DataSource dataSource) { | ||
| } | ||
| } |
178 changes: 178 additions & 0 deletions
178
kanban-server/src/main/java/ru/otus/spring/hw/kanban/conversion/ToMongoBatchConfig.java
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| package ru.otus.spring.hw.kanban.conversion; | ||
|
|
||
| import com.mongodb.MongoClient; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.batch.core.*; | ||
| import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; | ||
| import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; | ||
| import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; | ||
| import org.springframework.batch.core.scope.context.ChunkContext; | ||
| import org.springframework.batch.item.*; | ||
| import org.springframework.batch.item.data.MongoItemWriter; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.data.mongodb.MongoDbFactory; | ||
| import org.springframework.data.mongodb.core.MongoTemplate; | ||
| import org.springframework.data.mongodb.core.SimpleMongoDbFactory; | ||
| import ru.otus.spring.hw.kanban.domain.Board; | ||
| import ru.otus.spring.hw.kanban.domain.Stage; | ||
| import ru.otus.spring.hw.kanban.repository.BoardRepository; | ||
| import ru.otus.spring.hw.kanban.repository.StageRepository; | ||
| import ru.otus.spring.hw.kanban.repository.TaskRepository; | ||
| import ru.otus.spring.hw.kanban.repository.mongo.BoardMongoRepository; | ||
|
|
||
| import java.util.*; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| @EnableBatchProcessing | ||
| public class ToMongoBatchConfig { | ||
|
|
||
| private final Logger logger = LoggerFactory.getLogger("Batch"); | ||
|
|
||
| @Autowired | ||
| public JobBuilderFactory jobBuilderFactory; | ||
|
|
||
| @Autowired | ||
| public StepBuilderFactory stepBuilderFactory; | ||
|
|
||
|
|
||
| @Autowired | ||
| BoardItemProcessor boardItemProcessor; | ||
|
|
||
| @Autowired | ||
| BoardRepository boardRepository; | ||
|
|
||
|
|
||
| @Autowired | ||
| BoardMongoRepository boardMongoRepository; | ||
|
|
||
| @Autowired | ||
| StageRepository stageRepository; | ||
|
|
||
| @Autowired | ||
| TaskRepository taskRepository; | ||
|
|
||
| @Bean | ||
| public ItemReader<Board> reader() { | ||
| return new ItemReader<Board>() { | ||
|
|
||
| private Integer currentId = 0; | ||
|
|
||
| @Override | ||
| public Board read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException { | ||
| Board board = boardRepository.findFirstByIdGreaterThan(currentId).orElse(null); | ||
| if (board != null) { | ||
| Set<Stage> stagesSet = new HashSet<>(); | ||
| List<Stage> stages = stageRepository.findStagesByBoard(board); | ||
|
|
||
| for (Stage stage : stages) { | ||
| stage.setTasks(taskRepository.findAllByStage(stage) | ||
| .stream() | ||
| .collect(Collectors.toSet())); | ||
| stagesSet.add(stage); | ||
| } | ||
| board.setStages(stagesSet); | ||
| currentId = board.getId(); | ||
| } | ||
|
|
||
| return board; | ||
| } | ||
| }; | ||
| } | ||
|
|
||
|
|
||
| @Bean | ||
| public ItemWriter<ru.otus.spring.hw.kanban.domain.mongo.Board> writer() { | ||
| MongoItemWriter<ru.otus.spring.hw.kanban.domain.mongo.Board> writer = new MongoItemWriter<>(); | ||
| try { | ||
| writer.setTemplate(mongoTemplate()); | ||
| } catch (Exception e) { | ||
| logger.error(e.toString()); | ||
| } | ||
| writer.setCollection("board"); | ||
| return writer; | ||
| } | ||
|
|
||
|
|
||
| @Bean | ||
| public MongoDbFactory mongoDbFactory() throws Exception { | ||
| return new SimpleMongoDbFactory(new MongoClient(), "kanban"); | ||
| } | ||
|
|
||
| @Bean | ||
| public MongoTemplate mongoTemplate() throws Exception { | ||
| MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory()); | ||
| return mongoTemplate; | ||
| } | ||
|
|
||
|
|
||
| @Bean | ||
| public Step step1() { | ||
| return stepBuilderFactory.get("step1") | ||
| .<Board, ru.otus.spring.hw.kanban.domain.mongo.Board>chunk(5) | ||
| .reader(reader()) | ||
| .processor(boardItemProcessor) | ||
| .writer(writer()) | ||
| .listener(new ItemReadListener<Board>() { | ||
| public void beforeRead() { | ||
| logger.info("Начало чтения"); | ||
| } | ||
|
|
||
| public void afterRead(Board o) { | ||
| logger.info("Конец чтения " + o.toString()); | ||
| } | ||
|
|
||
| public void onReadError(Exception e) { | ||
| logger.info("Ошибка чтения"); | ||
| } | ||
| }) | ||
| .listener(new ItemWriteListener() { | ||
| public void beforeWrite(List list) { | ||
| logger.info("Начало записи"); | ||
| } | ||
|
|
||
| public void afterWrite(List list) { | ||
| logger.info("Конец записи"); | ||
| } | ||
|
|
||
| public void onWriteError(Exception e, List list) { | ||
| logger.info("Ошибка записи"); | ||
| } | ||
| }) | ||
| .listener(new ItemProcessListener() { | ||
| public void beforeProcess(Object o) { | ||
| logger.info("Начало обработки"); | ||
| } | ||
|
|
||
| public void afterProcess(Object o, Object o2) { | ||
| logger.info("Конец обработки"); | ||
| } | ||
|
|
||
| public void onProcessError(Object o, Exception e) { | ||
| logger.info("Ошбка обработки"); | ||
| } | ||
| }) | ||
| .listener(new ChunkListener() { | ||
| public void beforeChunk(ChunkContext chunkContext) { | ||
| logger.info("Начало пачки"); | ||
| } | ||
|
|
||
| public void afterChunk(ChunkContext chunkContext) { | ||
| logger.info("Конец пачки"); | ||
| } | ||
|
|
||
| public void afterChunkError(ChunkContext chunkContext) { | ||
| logger.info("Ошибка пачки"); | ||
| } | ||
| }) | ||
| .build(); | ||
| } | ||
|
|
||
| @Bean | ||
| public Job convertBoardsJob() { | ||
| return jobBuilderFactory.get("convertBoardsJob") | ||
| .start(step1()) | ||
| .build(); | ||
| } | ||
| } | ||
12 changes: 12 additions & 0 deletions
12
kanban-server/src/main/java/ru/otus/spring/hw/kanban/domain/Board.java
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Кстати кастомный ридер здесь совсем не обязательно, spring batch очень крут тем что имеет много готовых компонентов для большинства задач.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
применил RepositoryItemReader