init
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
package com.example.springbatch;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringBatchApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringBatchApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
103
src/main/java/com/example/springbatch/batch/FirstBatch.java
Normal file
103
src/main/java/com/example/springbatch/batch/FirstBatch.java
Normal file
@@ -0,0 +1,103 @@
|
||||
package com.example.springbatch.batch;
|
||||
|
||||
import com.example.springbatch.entity.AfterEntity;
|
||||
import com.example.springbatch.entity.BeforeEntity;
|
||||
import com.example.springbatch.repository.AfterRepository;
|
||||
import com.example.springbatch.repository.BeforeRepository;
|
||||
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.launch.support.RunIdIncrementer;
|
||||
import org.springframework.batch.core.repository.JobRepository;
|
||||
import org.springframework.batch.core.step.builder.StepBuilder;
|
||||
import org.springframework.batch.item.ItemProcessor;
|
||||
import org.springframework.batch.item.data.RepositoryItemReader;
|
||||
import org.springframework.batch.item.data.RepositoryItemWriter;
|
||||
import org.springframework.batch.item.data.builder.RepositoryItemReaderBuilder;
|
||||
import org.springframework.batch.item.data.builder.RepositoryItemWriterBuilder;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Configuration
|
||||
public class FirstBatch {
|
||||
|
||||
private final JobRepository jobRepository;
|
||||
private final PlatformTransactionManager platformTransactionManager;
|
||||
|
||||
private final BeforeRepository beforeRepository;
|
||||
private final AfterRepository afterRepository;
|
||||
|
||||
public FirstBatch(JobRepository jobRepository, PlatformTransactionManager platformTransactionManager, BeforeRepository beforeRepository, AfterRepository afterRepository) {
|
||||
|
||||
this.jobRepository = jobRepository;
|
||||
this.platformTransactionManager = platformTransactionManager;
|
||||
this.beforeRepository = beforeRepository;
|
||||
this.afterRepository = afterRepository;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Job firstJob() {
|
||||
|
||||
System.out.println("first job");
|
||||
|
||||
return new JobBuilder("firstJob", jobRepository)
|
||||
.incrementer(new RunIdIncrementer())
|
||||
.start(firstStep())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Step firstStep() {
|
||||
|
||||
System.out.println("first step");
|
||||
|
||||
return new StepBuilder("firstStep", jobRepository)
|
||||
.<BeforeEntity, AfterEntity> chunk(10, platformTransactionManager)
|
||||
.reader(beforeReader())
|
||||
.processor(middleProcessor())
|
||||
.writer(afterWriter())
|
||||
.build();
|
||||
}
|
||||
|
||||
//https://github.com/spring-projects/spring-batch/blob/main/spring-batch-samples/src/main/java/org/springframework/batch/samples/jpa/JpaRepositoryJobConfiguration.java
|
||||
@Bean
|
||||
public RepositoryItemReader<BeforeEntity> beforeReader() {
|
||||
|
||||
return new RepositoryItemReaderBuilder<BeforeEntity>()
|
||||
.name("beforeReader")
|
||||
.pageSize(10)
|
||||
.methodName("findAll")
|
||||
.repository(beforeRepository)
|
||||
.sorts(Map.of("id", Sort.Direction.ASC))
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ItemProcessor<BeforeEntity, AfterEntity> middleProcessor() {
|
||||
|
||||
return new ItemProcessor<BeforeEntity, AfterEntity>() {
|
||||
|
||||
@Override
|
||||
public AfterEntity process(BeforeEntity item) throws Exception {
|
||||
|
||||
AfterEntity afterEntity = new AfterEntity();
|
||||
afterEntity.setUsername(item.getUsername());
|
||||
|
||||
return afterEntity;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RepositoryItemWriter<AfterEntity> afterWriter() {
|
||||
|
||||
return new RepositoryItemWriterBuilder<AfterEntity>()
|
||||
.repository(afterRepository)
|
||||
.methodName("save")
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.example.springbatch.batch;
|
||||
|
||||
public class SecondBatch {
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.example.springbatch.config;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.jdbc.DataSourceBuilder;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.orm.jpa.JpaTransactionManager;
|
||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.util.HashMap;
|
||||
|
||||
@Configuration
|
||||
@EnableJpaRepositories(
|
||||
basePackages = "com.example.springbatch.repository",
|
||||
entityManagerFactoryRef = "dataEntityManager",
|
||||
transactionManagerRef = "dataTransactionManager"
|
||||
)
|
||||
public class DataDBConfig {
|
||||
|
||||
@Bean
|
||||
@ConfigurationProperties(prefix = "spring.datasource-data")
|
||||
public DataSource dataDBSource() {
|
||||
|
||||
return DataSourceBuilder.create().build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public LocalContainerEntityManagerFactoryBean dataEntityManager() {
|
||||
|
||||
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
||||
|
||||
em.setDataSource(dataDBSource());
|
||||
em.setPackagesToScan(new String[]{"com.example.springbatch.entity"});
|
||||
em. setJpaVendorAdapter(new HibernateJpaVendorAdapter());
|
||||
|
||||
HashMap<String, Object> properties = new HashMap<>();
|
||||
properties.put("hibernate.hbm2ddl.auto", "update");
|
||||
properties.put("hibernate.show_sql", "true");
|
||||
em.setJpaPropertyMap(properties);
|
||||
|
||||
return em;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PlatformTransactionManager dataTransactionManager() {
|
||||
|
||||
JpaTransactionManager transactionManager = new JpaTransactionManager();
|
||||
|
||||
transactionManager.setEntityManagerFactory(dataEntityManager().getObject());
|
||||
|
||||
return transactionManager;
|
||||
}
|
||||
|
||||
// @Bean
|
||||
// public JdbcTemplate jdbcTemplate() {
|
||||
//
|
||||
// return new JdbcTemplate(dataDBSource());
|
||||
// }
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.example.springbatch.config;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.jdbc.DataSourceBuilder;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
@Configuration
|
||||
public class MetaDBConfig {
|
||||
|
||||
@Primary
|
||||
@Bean
|
||||
@ConfigurationProperties(prefix = "spring.datasource-meta")
|
||||
public DataSource metaDBSource() {
|
||||
|
||||
return DataSourceBuilder.create().build();
|
||||
}
|
||||
|
||||
@Primary
|
||||
@Bean
|
||||
public PlatformTransactionManager metaTransactionManager() {
|
||||
|
||||
return new DataSourceTransactionManager(metaDBSource());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.example.springbatch.controller;
|
||||
|
||||
public class MainController {
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.example.springbatch.entity;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Entity
|
||||
@Getter
|
||||
@Setter
|
||||
public class AfterEntity {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
private String username;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.example.springbatch.entity;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Entity
|
||||
@Getter
|
||||
@Setter
|
||||
public class BeforeEntity {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
private String username;
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.example.springbatch.repository;
|
||||
|
||||
import com.example.springbatch.entity.AfterEntity;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface AfterRepository extends JpaRepository<AfterEntity, Long> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.example.springbatch.repository;
|
||||
|
||||
import com.example.springbatch.entity.BeforeEntity;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface BeforeRepository extends JpaRepository<BeforeEntity, Long> {
|
||||
|
||||
}
|
||||
19
src/main/resources/application.properties
Normal file
19
src/main/resources/application.properties
Normal file
@@ -0,0 +1,19 @@
|
||||
spring.application.name=SpringBatch
|
||||
|
||||
server.port=8080
|
||||
|
||||
spring.datasource-meta.driver-class-name=com.mysql.cj.jdbc.Driver
|
||||
spring.datasource-meta.jdbc-url=jdbc:mysql://144.24.71.211:3306/meta_db?useSSL=false&useUnicode=true&serverTimezone=Asia/Seoul&allowPublicKeyRetrieval=true
|
||||
spring.datasource-meta.username=user1
|
||||
spring.datasource-meta.password=vmfhaltmskdls123
|
||||
|
||||
spring.datasource-data.driver-class-name=com.mysql.cj.jdbc.Driver
|
||||
spring.datasource-data.jdbc-url=jdbc:mysql://144.24.71.211:3306/data_db?useSSL=false&useUnicode=true&serverTimezone=Asia/Seoul&allowPublicKeyRetrieval=true
|
||||
spring.datasource-data.username=user1
|
||||
spring.datasource-data.password=vmfhaltmskdls123
|
||||
|
||||
|
||||
spring.batch.job.enabled=true
|
||||
spring.batch.job.name=firstJob
|
||||
spring.batch.jdbc.initialize-schema=always
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.example.springbatch;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class SpringBatchApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user