-
Notifications
You must be signed in to change notification settings - Fork 0
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
184 additions
and
1 deletion.
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
4 changes: 4 additions & 0 deletions
4
src/main/java/zipdabang/server/ZipdabangServerApplication.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
127 changes: 127 additions & 0 deletions
127
src/main/java/zipdabang/server/batch/config/WeeklyBestRecipeBatchConfig.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,127 @@ | ||
package zipdabang.server.batch.config; | ||
|
||
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.configuration.annotation.EnableBatchProcessing; | ||
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; | ||
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; | ||
import org.springframework.batch.core.configuration.annotation.StepScope; | ||
import org.springframework.batch.item.ItemProcessor; | ||
import org.springframework.batch.item.ItemWriter; | ||
import org.springframework.batch.item.data.RepositoryItemReader; | ||
import org.springframework.batch.item.data.builder.RepositoryItemReaderBuilder; | ||
import org.springframework.batch.item.support.ListItemReader; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.domain.Sort; | ||
import zipdabang.server.domain.recipe.Recipe; | ||
import zipdabang.server.domain.recipe.WeeklyBestRecipe; | ||
import zipdabang.server.repository.recipeRepositories.RecipeRepository; | ||
import zipdabang.server.repository.recipeRepositories.WeeklyBestRecipeRepository; | ||
|
||
import java.util.Collections; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
@Slf4j | ||
@Configuration | ||
@EnableBatchProcessing | ||
@RequiredArgsConstructor | ||
public class WeeklyBestRecipeBatchConfig { | ||
|
||
private final JobBuilderFactory jobBuilderFactory; | ||
private final StepBuilderFactory stepBuilderFactory; | ||
private final RecipeRepository recipeRepository; | ||
private final WeeklyBestRecipeRepository weeklyBestRecipeRepository; | ||
|
||
@Value("10") | ||
private Integer chunkSize; | ||
|
||
@Bean | ||
public Job job() { | ||
Job job = jobBuilderFactory.get("WeeklyBestRecipeJob") | ||
.start(step1()) | ||
.next(step2()) | ||
.next(step3()) | ||
.build(); | ||
|
||
return job; | ||
} | ||
|
||
@Bean | ||
public Step step1() { | ||
return stepBuilderFactory.get("WeeklyBestRecipeStep1") | ||
.<WeeklyBestRecipe, WeeklyBestRecipe>chunk(5) | ||
.reader(step1ItemReader()) | ||
.writer(step1ItemWriter()) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public Step step2() { | ||
return stepBuilderFactory.get("WeeklyBestRecipeStep2") | ||
.<Recipe, WeeklyBestRecipe>chunk(chunkSize) | ||
.reader(step2ItemReader()) | ||
.processor(step2ItemProcessor()) | ||
.writer(step2ItemWriter()) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public Step step3() { | ||
return stepBuilderFactory.get("WeeklyBestRecipeStep3") | ||
.<Recipe, Recipe>chunk(chunkSize) | ||
.reader(step3ItemReader()) | ||
.writer(step3ItemWriter()) | ||
.build(); | ||
} | ||
|
||
@StepScope | ||
@Bean | ||
public ListItemReader<WeeklyBestRecipe> step1ItemReader() { | ||
return new ListItemReader<WeeklyBestRecipe>(weeklyBestRecipeRepository.findAll()); | ||
} | ||
|
||
@StepScope | ||
@Bean | ||
public ItemWriter<WeeklyBestRecipe> step1ItemWriter() { | ||
return bestRecipes -> bestRecipes.forEach(bestRecipe -> weeklyBestRecipeRepository.delete(bestRecipe)); | ||
} | ||
|
||
@StepScope | ||
@Bean | ||
public ListItemReader<Recipe> step2ItemReader() { | ||
return new ListItemReader<Recipe>(recipeRepository.findTop5ByOrderByWeekLikeDescWeekScrapDescTotalLikeDescTotalScrapDesc()); | ||
} | ||
|
||
@StepScope | ||
@Bean | ||
public ItemProcessor<Recipe, WeeklyBestRecipe> step2ItemProcessor() { | ||
AtomicInteger index = new AtomicInteger(1); | ||
|
||
return recipe -> WeeklyBestRecipe.builder() | ||
.ranking(index.getAndIncrement()) | ||
.recipe(recipe) | ||
.build(); | ||
} | ||
|
||
@StepScope | ||
@Bean | ||
public ItemWriter<WeeklyBestRecipe> step2ItemWriter() { | ||
return bestRecipes -> bestRecipes.forEach(bestRecipe -> weeklyBestRecipeRepository.save(bestRecipe)); | ||
} | ||
|
||
@StepScope | ||
@Bean | ||
public ListItemReader<Recipe> step3ItemReader() { | ||
return new ListItemReader<Recipe>(recipeRepository.findAll()); | ||
} | ||
|
||
@StepScope | ||
@Bean | ||
public ItemWriter<Recipe> step3ItemWriter() { | ||
return recipes -> recipes.forEach(recipe -> recipeRepository.updateWeeklyData(recipe)); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/main/java/zipdabang/server/batch/schedular/BatchScheduler.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 zipdabang.server.batch.schedular; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.batch.core.JobParameter; | ||
import org.springframework.batch.core.JobParameters; | ||
import org.springframework.batch.core.JobParametersInvalidException; | ||
import org.springframework.batch.core.launch.JobLauncher; | ||
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException; | ||
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Component; | ||
import zipdabang.server.batch.config.WeeklyBestRecipeBatchConfig; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class BatchScheduler { | ||
|
||
private final JobLauncher jobLauncher; | ||
private final WeeklyBestRecipeBatchConfig weeklyBestRecipeBatchConfig; | ||
|
||
// @Scheduled(cron = "0 0 0 ? ? 1") | ||
@Scheduled(cron = "0 0 0 * * 1") | ||
public void runWeeklyBestRecipeJob() { | ||
Map<String, JobParameter> confMap = new HashMap<>(); | ||
confMap.put("time", new JobParameter(System.currentTimeMillis())); | ||
JobParameters jobParameters = new JobParameters(confMap); | ||
|
||
try { | ||
jobLauncher.run(weeklyBestRecipeBatchConfig.job(), jobParameters); | ||
} catch (JobExecutionAlreadyRunningException | JobInstanceAlreadyCompleteException | ||
| JobParametersInvalidException | org.springframework.batch.core.repository.JobRestartException e) { | ||
log.error(e.getMessage()); | ||
} | ||
} | ||
} |
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