Skip to content

Commit

Permalink
YEL-214 [setting] spring batch setting
Browse files Browse the repository at this point in the history
  • Loading branch information
hyeonjeongs committed Feb 26, 2024
1 parent 4447168 commit b025439
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 0 deletions.
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);
}
}
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();
}
}
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 -> {

});
}
}
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();
}


}
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();
}
}

0 comments on commit b025439

Please sign in to comment.