forked from jojoldu/blog-code
-
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
12 changed files
with
535 additions
and
0 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
springboot-batch-2/src/main/java/com/jojoldu/blogcode/springbootbatch2/Application.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,12 @@ | ||
package com.jojoldu.blogcode.springbootbatch2; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class Application { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(Application.class, args); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
springboot-batch-2/src/main/java/com/jojoldu/blogcode/springbootbatch2/readupdate/Pay.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,38 @@ | ||
package com.jojoldu.blogcode.springbootbatch2.readupdate; | ||
|
||
/** | ||
* Created by [email protected] on 2018. 9. 15. | ||
* Blog : http://jojoldu.tistory.com | ||
* Github : https://github.com/jojoldu | ||
*/ | ||
|
||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.GenerationType; | ||
import javax.persistence.Id; | ||
|
||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@Entity | ||
public class Pay { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
private Long amount; | ||
private boolean successStatus; | ||
|
||
public Pay(Long amount, boolean successStatus) { | ||
this.amount = amount; | ||
this.successStatus = successStatus; | ||
} | ||
|
||
public void success() { | ||
this.successStatus = true; | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
...main/java/com/jojoldu/blogcode/springbootbatch2/readupdate/PayCursorJobConfiguration.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,94 @@ | ||
package com.jojoldu.blogcode.springbootbatch2.readupdate; | ||
|
||
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.JobBuilderFactory; | ||
import org.springframework.batch.core.configuration.annotation.JobScope; | ||
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.database.JdbcCursorItemReader; | ||
import org.springframework.batch.item.database.JpaItemWriter; | ||
import org.springframework.batch.item.database.builder.JdbcCursorItemReaderBuilder; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.jdbc.core.BeanPropertyRowMapper; | ||
|
||
import javax.persistence.EntityManagerFactory; | ||
import javax.sql.DataSource; | ||
|
||
import static com.jojoldu.blogcode.springbootbatch2.readupdate.PayCursorJobConfiguration.JOB_NAME; | ||
|
||
|
||
/** | ||
* Created by [email protected] on 2018. 9. 15. | ||
* Blog : http://jojoldu.tistory.com | ||
* Github : https://github.com/jojoldu | ||
*/ | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
@Configuration | ||
@ConditionalOnProperty(name = "job.name", havingValue = JOB_NAME) | ||
public class PayCursorJobConfiguration { | ||
|
||
public static final String JOB_NAME = "payCursorJob"; | ||
|
||
private final EntityManagerFactory entityManagerFactory; | ||
private final StepBuilderFactory stepBuilderFactory; | ||
private final JobBuilderFactory jobBuilderFactory; | ||
private final DataSource dataSource; | ||
|
||
private final int chunkSize = 10; | ||
|
||
@Bean | ||
public Job payPagingJob() { | ||
return jobBuilderFactory.get(JOB_NAME) | ||
.start(payPagingStep()) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
@JobScope | ||
public Step payPagingStep() { | ||
return stepBuilderFactory.get("payPagingStep") | ||
.<Pay, Pay>chunk(chunkSize) | ||
.reader(payPagingReader()) | ||
.processor(payPagingProcessor()) | ||
.writer(writer()) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
@StepScope | ||
public JdbcCursorItemReader<Pay> payPagingReader() { | ||
return new JdbcCursorItemReaderBuilder<Pay>() | ||
.sql("SELECT * FROM pay p WHERE p.success_status = false") | ||
.rowMapper(new BeanPropertyRowMapper<>(Pay.class)) | ||
.fetchSize(chunkSize) | ||
.dataSource(dataSource) | ||
.name("payPagingReader") | ||
.build(); | ||
} | ||
|
||
@Bean | ||
@StepScope | ||
public ItemProcessor<Pay, Pay> payPagingProcessor() { | ||
return item -> { | ||
item.success(); | ||
return item; | ||
}; | ||
} | ||
|
||
@Bean | ||
@StepScope | ||
public JpaItemWriter<Pay> writer() { | ||
JpaItemWriter<Pay> writer = new JpaItemWriter<>(); | ||
writer.setEntityManagerFactory(entityManagerFactory); | ||
return writer; | ||
} | ||
|
||
} |
91 changes: 91 additions & 0 deletions
91
.../java/com/jojoldu/blogcode/springbootbatch2/readupdate/PayPagingFailJobConfiguration.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,91 @@ | ||
package com.jojoldu.blogcode.springbootbatch2.readupdate; | ||
|
||
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.JobBuilderFactory; | ||
import org.springframework.batch.core.configuration.annotation.JobScope; | ||
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.database.JpaItemWriter; | ||
import org.springframework.batch.item.database.JpaPagingItemReader; | ||
import org.springframework.batch.item.database.builder.JpaPagingItemReaderBuilder; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import javax.persistence.EntityManagerFactory; | ||
|
||
import static com.jojoldu.blogcode.springbootbatch2.readupdate.PayPagingFailJobConfiguration.JOB_NAME; | ||
|
||
|
||
/** | ||
* Created by [email protected] on 2018. 9. 15. | ||
* Blog : http://jojoldu.tistory.com | ||
* Github : https://github.com/jojoldu | ||
*/ | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
@Configuration | ||
@ConditionalOnProperty(name = "job.name", havingValue = JOB_NAME) | ||
public class PayPagingFailJobConfiguration { | ||
|
||
public static final String JOB_NAME = "payPagingFailJob"; | ||
|
||
private final EntityManagerFactory entityManagerFactory; | ||
private final StepBuilderFactory stepBuilderFactory; | ||
private final JobBuilderFactory jobBuilderFactory; | ||
|
||
private final int chunkSize = 10; | ||
|
||
@Bean | ||
public Job payPagingJob() { | ||
return jobBuilderFactory.get(JOB_NAME) | ||
.start(payPagingStep()) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
@JobScope | ||
public Step payPagingStep() { | ||
return stepBuilderFactory.get("payPagingStep") | ||
.<Pay, Pay>chunk(chunkSize) | ||
.reader(payPagingReader()) | ||
.processor(payPagingProcessor()) | ||
.writer(writer()) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
@StepScope | ||
public JpaPagingItemReader<Pay> payPagingReader() { | ||
return new JpaPagingItemReaderBuilder<Pay>() | ||
.queryString("SELECT p FROM Pay p WHERE p.successStatus = false") | ||
.pageSize(chunkSize) | ||
.entityManagerFactory(entityManagerFactory) | ||
.name("payPagingReader") | ||
.build(); | ||
} | ||
|
||
|
||
@Bean | ||
@StepScope | ||
public ItemProcessor<Pay, Pay> payPagingProcessor() { | ||
return item -> { | ||
item.success(); | ||
return item; | ||
}; | ||
} | ||
|
||
@Bean | ||
@StepScope | ||
public JpaItemWriter<Pay> writer() { | ||
JpaItemWriter<Pay> writer = new JpaItemWriter<>(); | ||
writer.setEntityManagerFactory(entityManagerFactory); | ||
return writer; | ||
} | ||
|
||
} |
98 changes: 98 additions & 0 deletions
98
...main/java/com/jojoldu/blogcode/springbootbatch2/readupdate/PayPagingJobConfiguration.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,98 @@ | ||
package com.jojoldu.blogcode.springbootbatch2.readupdate; | ||
|
||
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.JobBuilderFactory; | ||
import org.springframework.batch.core.configuration.annotation.JobScope; | ||
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.database.JpaItemWriter; | ||
import org.springframework.batch.item.database.JpaPagingItemReader; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import javax.persistence.EntityManagerFactory; | ||
|
||
import static com.jojoldu.blogcode.springbootbatch2.readupdate.PayPagingJobConfiguration.JOB_NAME; | ||
|
||
|
||
/** | ||
* Created by [email protected] on 2018. 9. 15. | ||
* Blog : http://jojoldu.tistory.com | ||
* Github : https://github.com/jojoldu | ||
*/ | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
@Configuration | ||
@ConditionalOnProperty(name = "job.name", havingValue = JOB_NAME) | ||
public class PayPagingJobConfiguration { | ||
|
||
public static final String JOB_NAME = "payPagingJob"; | ||
|
||
private final EntityManagerFactory entityManagerFactory; | ||
private final StepBuilderFactory stepBuilderFactory; | ||
private final JobBuilderFactory jobBuilderFactory; | ||
|
||
private final int chunkSize = 10; | ||
|
||
@Bean | ||
public Job payPagingJob() { | ||
return jobBuilderFactory.get(JOB_NAME) | ||
.start(payPagingStep()) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
@JobScope | ||
public Step payPagingStep() { | ||
return stepBuilderFactory.get("payPagingStep") | ||
.<Pay, Pay>chunk(chunkSize) | ||
.reader(payPagingReader()) | ||
.processor(payPagingProcessor()) | ||
.writer(writer()) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
@StepScope | ||
public JpaPagingItemReader<Pay> payPagingReader() { | ||
|
||
JpaPagingItemReader<Pay> reader = new JpaPagingItemReader<Pay>() { | ||
@Override | ||
public int getPage() { | ||
return 0; | ||
} | ||
}; | ||
|
||
reader.setQueryString("SELECT p FROM Pay p WHERE p.successStatus = false"); | ||
reader.setPageSize(chunkSize); | ||
reader.setEntityManagerFactory(entityManagerFactory); | ||
reader.setName("payPagingReader"); | ||
|
||
return reader; | ||
} | ||
|
||
|
||
@Bean | ||
@StepScope | ||
public ItemProcessor<Pay, Pay> payPagingProcessor() { | ||
return item -> { | ||
item.success(); | ||
return item; | ||
}; | ||
} | ||
|
||
@Bean | ||
@StepScope | ||
public JpaItemWriter<Pay> writer() { | ||
JpaItemWriter<Pay> writer = new JpaItemWriter<>(); | ||
writer.setEntityManagerFactory(entityManagerFactory); | ||
return writer; | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
...batch-2/src/main/java/com/jojoldu/blogcode/springbootbatch2/readupdate/PayRepository.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,18 @@ | ||
package com.jojoldu.blogcode.springbootbatch2.readupdate; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Created by [email protected] on 2018. 9. 15. | ||
* Blog : http://jojoldu.tistory.com | ||
* Github : https://github.com/jojoldu | ||
*/ | ||
|
||
public interface PayRepository extends JpaRepository<Pay, Long> { | ||
|
||
@Query("SELECT p FROM Pay p WHERE p.successStatus = true") | ||
List<Pay> findAllSuccess(); | ||
} |
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,14 @@ | ||
spring: | ||
h2: | ||
console: | ||
enabled: true | ||
jpa: | ||
show-sql: true | ||
hibernate: | ||
ddl-auto: update | ||
|
||
logging: | ||
level: | ||
org.hibernate.type: trace | ||
|
||
spring.batch.job.names: ${job.name:NONE} |
16 changes: 16 additions & 0 deletions
16
...oot-batch-2/src/test/java/com/jojoldu/blogcode/springbootbatch2/TestJobConfiguration.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,16 @@ | ||
package com.jojoldu.blogcode.springbootbatch2; | ||
|
||
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; | ||
import org.springframework.batch.test.JobLauncherTestUtils; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@EnableBatchProcessing | ||
@Configuration | ||
public class TestJobConfiguration { | ||
|
||
@Bean | ||
public JobLauncherTestUtils jobLauncherTestUtils() { | ||
return new JobLauncherTestUtils(); | ||
} | ||
} |
Oops, something went wrong.