batch delete
This commit is contained in:
@@ -26,6 +26,8 @@ dependencies {
|
|||||||
implementation 'org.springframework.boot:spring-boot-starter-actuator'
|
implementation 'org.springframework.boot:spring-boot-starter-actuator'
|
||||||
implementation 'org.springframework.boot:spring-boot-starter-security'
|
implementation 'org.springframework.boot:spring-boot-starter-security'
|
||||||
|
|
||||||
|
implementation 'org.springframework.boot:spring-boot-starter-batch'
|
||||||
|
|
||||||
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
||||||
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
|
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +1,17 @@
|
|||||||
package com.example.oneul;
|
package com.example.oneul;
|
||||||
|
|
||||||
|
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
|
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
|
||||||
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||||
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
|
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
|
||||||
|
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
@EnableJpaAuditing
|
@EnableJpaAuditing
|
||||||
|
@EnableScheduling
|
||||||
|
@EnableBatchProcessing
|
||||||
@EnableRedisHttpSession
|
@EnableRedisHttpSession
|
||||||
public class OneulApplication {
|
public class OneulApplication {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package com.example.oneul.domain.post.dao;
|
package com.example.oneul.domain.post.dao;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import com.example.oneul.domain.post.domain.Post;
|
import com.example.oneul.domain.post.domain.Post;
|
||||||
@@ -13,4 +15,6 @@ public interface PostCommandRepository extends CrudRepository<Post, Long> {
|
|||||||
void deleteByIdAndWriter(Long id, UserEntity writer);
|
void deleteByIdAndWriter(Long id, UserEntity writer);
|
||||||
void deleteById(Long id);
|
void deleteById(Long id);
|
||||||
void delete(Post post);
|
void delete(Post post);
|
||||||
|
|
||||||
|
List<Post> findAllByExpiredAt(LocalDateTime expiredAt);
|
||||||
}
|
}
|
||||||
@@ -31,6 +31,9 @@ public class Post implements Serializable {
|
|||||||
@CreatedDate
|
@CreatedDate
|
||||||
private LocalDateTime createdAt;
|
private LocalDateTime createdAt;
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
|
private LocalDateTime expiredAt;
|
||||||
|
private LocalDateTime deletedAt;
|
||||||
|
@Column(nullable = false)
|
||||||
private String content;
|
private String content;
|
||||||
@Access(AccessType.PROPERTY)
|
@Access(AccessType.PROPERTY)
|
||||||
@ManyToOne(fetch = FetchType.EAGER, optional = false)
|
@ManyToOne(fetch = FetchType.EAGER, optional = false)
|
||||||
@@ -38,9 +41,11 @@ public class Post implements Serializable {
|
|||||||
|
|
||||||
public Post() {}
|
public Post() {}
|
||||||
|
|
||||||
public Post(Long id, LocalDateTime createdAt, String content, UserEntity writer){
|
public Post(Long id, LocalDateTime createdAt, LocalDateTime expiredAt, String content, UserEntity writer){
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.createdAt = createdAt;
|
this.createdAt = createdAt;
|
||||||
|
this.expiredAt = expiredAt.plusHours(24);
|
||||||
|
this.deletedAt = null;
|
||||||
this.content = content;
|
this.content = content;
|
||||||
this.writer = writer;
|
this.writer = writer;
|
||||||
}
|
}
|
||||||
@@ -61,6 +66,22 @@ public class Post implements Serializable {
|
|||||||
this.createdAt = createdAt;
|
this.createdAt = createdAt;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public LocalDateTime getExpiredAt() {
|
||||||
|
return this.expiredAt;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setExpiredAt(LocalDateTime expiredAt){
|
||||||
|
this.expiredAt = expiredAt;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LocalDateTime getDeletedAt(){
|
||||||
|
return this.deletedAt;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDeletedAt(LocalDateTime deletedAt){
|
||||||
|
this.deletedAt = deletedAt;
|
||||||
|
}
|
||||||
|
|
||||||
public String getContent(){
|
public String getContent(){
|
||||||
return this.content;
|
return this.content;
|
||||||
}
|
}
|
||||||
@@ -99,6 +120,8 @@ public class Post implements Serializable {
|
|||||||
return "Post["
|
return "Post["
|
||||||
+ "id: " + this.id
|
+ "id: " + this.id
|
||||||
+ ", createdAt: " + this.createdAt
|
+ ", createdAt: " + this.createdAt
|
||||||
|
+ ", expiredAt: " + this.expiredAt
|
||||||
|
+ ", deletedAt; " + this.deletedAt
|
||||||
+ ", content: " + this.content
|
+ ", content: " + this.content
|
||||||
+ "writer: " + this.writer.getId()
|
+ "writer: " + this.writer.getId()
|
||||||
+ "]";
|
+ "]";
|
||||||
@@ -112,12 +135,14 @@ public class Post implements Serializable {
|
|||||||
private Long id;
|
private Long id;
|
||||||
private String content;
|
private String content;
|
||||||
private LocalDateTime createdAt;
|
private LocalDateTime createdAt;
|
||||||
|
private LocalDateTime expiredAt;
|
||||||
private UserEntity writer;
|
private UserEntity writer;
|
||||||
|
|
||||||
public Post build(){
|
public Post build(){
|
||||||
return new Post(
|
return new Post(
|
||||||
id,
|
id,
|
||||||
createdAt,
|
createdAt,
|
||||||
|
expiredAt,
|
||||||
content,
|
content,
|
||||||
writer);
|
writer);
|
||||||
}
|
}
|
||||||
@@ -131,6 +156,11 @@ public class Post implements Serializable {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Builder expiredAt(LocalDateTime expiredAt){
|
||||||
|
this.expiredAt = expiredAt;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public Builder content(String content){
|
public Builder content(String content){
|
||||||
this.content = content;
|
this.content = content;
|
||||||
return this;
|
return this;
|
||||||
|
|||||||
@@ -0,0 +1,60 @@
|
|||||||
|
package com.example.oneul.global.config;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.example.oneul.domain.post.dao.PostCommandRepository;
|
||||||
|
import com.example.oneul.domain.post.domain.Post;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
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.repeat.RepeatStatus;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableBatchProcessing
|
||||||
|
public class BatchConfig {
|
||||||
|
public JobBuilderFactory jobBuilderFactory;
|
||||||
|
public StepBuilderFactory stepBuilderFactory;
|
||||||
|
|
||||||
|
private final Logger log = LoggerFactory.getLogger(BatchConfig.class);
|
||||||
|
|
||||||
|
private PostCommandRepository postCommandRepository;
|
||||||
|
|
||||||
|
public BatchConfig(JobBuilderFactory jobBuilderFactory, StepBuilderFactory stepBuilderFactory, PostCommandRepository postCommandRepository){
|
||||||
|
this.jobBuilderFactory = jobBuilderFactory;
|
||||||
|
this.stepBuilderFactory = stepBuilderFactory;
|
||||||
|
this.postCommandRepository = postCommandRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Job job() {
|
||||||
|
Job job = jobBuilderFactory.get("job")
|
||||||
|
.start(step())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
return job;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Step step() {
|
||||||
|
return stepBuilderFactory.get("step")
|
||||||
|
.tasklet((contribution, chunkContext) -> {
|
||||||
|
LocalDateTime expiredAt = LocalDateTime.now();
|
||||||
|
List<Post> posts = postCommandRepository.findAllByExpiredAt(expiredAt);
|
||||||
|
// posts.stream().forEach(post -> {
|
||||||
|
// post.setDeletedAt(expiredAt);
|
||||||
|
// });
|
||||||
|
// // TODO: Query DB 한테 알려줘야함
|
||||||
|
// postCommandRepository.saveAll(posts);
|
||||||
|
log.info(expiredAt + " posts(" + posts.size() + ") are deleted.");
|
||||||
|
return RepeatStatus.FINISHED;
|
||||||
|
}).build();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
package com.example.oneul.global.util;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import com.example.oneul.global.config.BatchConfig;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
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.batch.core.repository.JobRestartException;
|
||||||
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class BatchScheduler {
|
||||||
|
private final Logger log = LoggerFactory.getLogger(BatchScheduler.class);
|
||||||
|
|
||||||
|
private JobLauncher jobLauncher;
|
||||||
|
private BatchConfig batchConfig;
|
||||||
|
|
||||||
|
public BatchScheduler(JobLauncher jobLauncher, BatchConfig batchConfig){
|
||||||
|
this.jobLauncher = jobLauncher;
|
||||||
|
this.batchConfig = batchConfig;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 1 분마다 실행
|
||||||
|
@Scheduled(cron = "0 * * * * *")
|
||||||
|
public void runjob(){
|
||||||
|
Map<String, JobParameter> configMap = new HashMap<>();
|
||||||
|
configMap.put("time", new JobParameter(System.currentTimeMillis()));
|
||||||
|
JobParameters jobParameters = new JobParameters(configMap);
|
||||||
|
|
||||||
|
try {
|
||||||
|
jobLauncher.run(batchConfig.job(), jobParameters);
|
||||||
|
} catch (JobExecutionAlreadyRunningException | JobInstanceAlreadyCompleteException | JobParametersInvalidException | JobRestartException e) {
|
||||||
|
log.error(e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,3 +1,6 @@
|
|||||||
spring:
|
spring:
|
||||||
profiles:
|
profiles:
|
||||||
active: local
|
active: local
|
||||||
|
batch:
|
||||||
|
job:
|
||||||
|
enabled: false
|
||||||
Reference in New Issue
Block a user