-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
YEL-214 [setting] spring batch setting
- Loading branch information
1 parent
4447168
commit b025439
Showing
5 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
src/main/java/com/yello/server/infrastructure/batch/ChunkProcessor.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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.yello.server.infrastructure.batch; | ||
|
||
|
||
import com.yello.server.domain.user.entity.User; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.batch.core.configuration.annotation.StepScope; | ||
import org.springframework.batch.item.ItemProcessor; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@RequiredArgsConstructor | ||
@Configuration | ||
public class ChunkProcessor { | ||
|
||
@Bean | ||
@StepScope | ||
public ItemProcessor<User, User> lunchEventProcessor() { | ||
return user -> new User); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/com/yello/server/infrastructure/batch/ChunkReader.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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.yello.server.infrastructure.batch; | ||
|
||
import com.yello.server.domain.user.entity.User; | ||
import com.yello.server.domain.user.repository.UserJpaRepository; | ||
import com.yello.server.domain.user.repository.UserRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.batch.core.configuration.annotation.StepScope; | ||
import org.springframework.batch.item.data.RepositoryItemReader; | ||
import org.springframework.batch.item.data.builder.RepositoryItemReaderBuilder; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.domain.Sort; | ||
|
||
import java.util.Collections; | ||
|
||
@RequiredArgsConstructor | ||
@Configuration | ||
public class ChunkReader { | ||
private final UserJpaRepository userRepository; | ||
|
||
@Bean | ||
@StepScope | ||
public RepositoryItemReader<User> userDataReader() { | ||
|
||
return new RepositoryItemReaderBuilder<User>() | ||
.name("userDataReader") | ||
.repository(userRepository) | ||
.methodName("findAllByPageable") | ||
.pageSize(100) | ||
.sorts(Collections.singletonMap("id", Sort.Direction.ASC)) | ||
.build(); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/com/yello/server/infrastructure/batch/ChunkWriter.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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.yello.server.infrastructure.batch; | ||
|
||
import com.yello.server.domain.user.entity.User; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.batch.core.configuration.annotation.StepScope; | ||
import org.springframework.batch.item.ItemWriter; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
|
||
@RequiredArgsConstructor | ||
@Configuration | ||
public class ChunkWriter { | ||
|
||
@Bean | ||
@StepScope | ||
public ItemWriter<User> lunchEventWriter() { | ||
return items -> items.forEach(item -> { | ||
|
||
}); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/com/yello/server/infrastructure/batch/JobConfiguration.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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.yello.server.infrastructure.batch; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.batch.core.Job; | ||
import org.springframework.batch.core.job.builder.JobBuilder; | ||
import org.springframework.batch.core.repository.JobRepository; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.transaction.PlatformTransactionManager; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
@Configuration | ||
public class JobConfiguration { | ||
private final StepConfiguration stepConfiguration; | ||
|
||
@Bean | ||
public Job myJob(JobRepository jobRepository, PlatformTransactionManager transactionManager) { | ||
return new JobBuilder("myJob", jobRepository) | ||
.start(stepConfiguration.lunchEventAlarmStep(jobRepository, transactionManager)) | ||
.build(); | ||
} | ||
|
||
|
||
} |
40 changes: 40 additions & 0 deletions
40
src/main/java/com/yello/server/infrastructure/batch/StepConfiguration.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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.yello.server.infrastructure.batch; | ||
|
||
import com.yello.server.domain.user.entity.User; | ||
import com.yello.server.domain.user.entity.UserData; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
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.repeat.RepeatStatus; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.transaction.PlatformTransactionManager; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
@Configuration | ||
public class StepConfiguration { | ||
private final ChunkReader chunkReader; | ||
private final ChunkProcessor chunkProcessor; | ||
private final ChunkWriter chunkWriter; | ||
|
||
@Bean | ||
public Step myStep(JobRepository jobRepository, PlatformTransactionManager transactionManager) { | ||
return new StepBuilder("myStep", jobRepository) | ||
.tasklet(((contribution, chunkContext) -> RepeatStatus.FINISHED), transactionManager) // or .chunk(chunkSize, transactionManager) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public Step lunchEventAlarmStep(JobRepository jobRepository, PlatformTransactionManager transactionManager) { | ||
return new StepBuilder("lunchEventStep", jobRepository) | ||
.<User, User>chunk(100, transactionManager) | ||
.reader(chunkReader.userDataReader()) | ||
.writer(chunkWriter.lunchEventWriter()) | ||
.build(); | ||
} | ||
} |