Skip to content

Commit 99c67cd

Browse files
first commit
0 parents  commit 99c67cd

28 files changed

+931
-0
lines changed

.gitattributes

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/mvnw text eol=lf
2+
*.cmd text eol=crlf

.gitignore

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
README.md
2+
target/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
!**/src/main/**/target/
5+
!**/src/test/**/target/
6+
7+
### STS ###
8+
.apt_generated
9+
.classpath
10+
.factorypath
11+
.project
12+
.settings
13+
.springBeans
14+
.sts4-cache
15+
16+
### IntelliJ IDEA ###
17+
.idea
18+
*.iws
19+
*.iml
20+
*.ipr
21+
22+
### NetBeans ###
23+
/nbproject/private/
24+
/nbbuild/
25+
/dist/
26+
/nbdist/
27+
/.nb-gradle/
28+
build/
29+
!**/src/main/**/build/
30+
!**/src/test/**/build/
31+
32+
### VS Code ###
33+
.vscode/
34+
35+
.gradle/

dbAndCsvBatch/build.gradle

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
plugins {
2+
id 'org.springframework.boot' version '3.4.0'
3+
id 'io.spring.dependency-management' version '1.1.6'
4+
id 'com.diffplug.spotless' version '6.22.0'
5+
id 'nu.studer.jooq' version '9.0'
6+
id 'java'
7+
id 'project-report'
8+
}
9+
10+
group = 'com.example.batch'
11+
version = '0.0.1-SNAPSHOT'
12+
description = 'SpringBatchDbAndCsv'
13+
java {
14+
sourceCompatibility = JavaVersion.VERSION_21
15+
targetCompatibility = JavaVersion.VERSION_21
16+
}
17+
18+
repositories {
19+
mavenCentral()
20+
}
21+
22+
// Spring Bootプラグインが管理するライブラリに対してバージョン番号を明示的に指定しない。
23+
// Do not explicitly specify version numbers for libraries managed by the Spring Boot plugin.
24+
// https://docs.spring.io/spring-boot/appendix/dependency-versions/coordinates.html
25+
dependencies {
26+
implementation 'org.springframework.boot:spring-boot-starter-batch'
27+
implementation 'org.springframework.boot:spring-boot-starter-jooq'
28+
implementation 'org.apache.commons:commons-lang3'
29+
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-csv'
30+
implementation 'com.mysql:mysql-connector-j'
31+
32+
// jOOQ dependencies
33+
implementation 'org.jooq:jooq'
34+
jooqGenerator 'org.jooq:jooq-meta'
35+
jooqGenerator 'org.jooq:jooq-codegen'
36+
jooqGenerator 'com.mysql:mysql-connector-j'
37+
38+
runtimeOnly 'com.h2database:h2'
39+
annotationProcessor 'org.projectlombok:lombok'
40+
compileOnly 'org.projectlombok:lombok'
41+
42+
testImplementation 'org.springframework.boot:spring-boot-starter-test'
43+
testImplementation 'org.springframework.batch:spring-batch-test'
44+
}
45+
46+
tasks.withType(JavaCompile) {
47+
options.encoding = 'UTF-8'
48+
}
49+
50+
tasks.named('test') {
51+
useJUnitPlatform()
52+
}
53+
54+
// https://github.com/diffplug/spotless
55+
spotless {
56+
java {
57+
target project.fileTree(project.projectDir) {
58+
include '**/*.java'
59+
exclude '**/generated-src/**'
60+
exclude '**/build/**'
61+
}
62+
googleJavaFormat()
63+
trimTrailingWhitespace()
64+
endWithNewline()
65+
removeUnusedImports()
66+
}
67+
}
68+
69+
jooq {
70+
configurations {
71+
main { // 生成コードの設定
72+
generationTool {
73+
jdbc {
74+
driver = 'com.mysql.cj.jdbc.Driver'
75+
url = 'jdbc:mysql://localhost:3306/sampledb'
76+
user = 'sampleuser'
77+
password = 'samplepassword'
78+
}
79+
generator {
80+
name = 'org.jooq.codegen.DefaultGenerator'
81+
strategy {
82+
name = 'org.jooq.codegen.DefaultGeneratorStrategy'
83+
}
84+
database {
85+
name = 'org.jooq.meta.mysql.MySQLDatabase'
86+
inputSchema = 'sampledb' // スキーマ名を指定
87+
}
88+
target {
89+
packageName = 'com.example.batch.jooq' // 生成コードのパッケージ
90+
directory = "build/generated-src/jooq/main"// 生成先ディレクトリ
91+
}
92+
}
93+
}
94+
}
95+
}
96+
}
97+
98+
tasks.named('build') {
99+
dependsOn 'generateJooq'
100+
}
101+
102+
defaultTasks 'tasks', 'spotlessApply', 'clean', 'check', 'projectReport', 'bootJar'

dbAndCsvBatch/compose.yaml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
services:
2+
mysql:
3+
image: mysql:latest
4+
container_name: mysql-container
5+
ports:
6+
- "3306:3306"
7+
environment:
8+
MYSQL_ROOT_PASSWORD: rootpassword
9+
MYSQL_DATABASE: sampledb
10+
MYSQL_USER: sampleuser
11+
MYSQL_PASSWORD: samplepassword
12+
volumes:
13+
- ./init-scripts:/docker-entrypoint-initdb.d
14+
restart: always
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CREATE TABLE member (
2+
id INT AUTO_INCREMENT PRIMARY KEY,
3+
name VARCHAR(100) NOT NULL,
4+
email VARCHAR(100) NOT NULL UNIQUE,
5+
phone VARCHAR(15),
6+
address VARCHAR(255),
7+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
8+
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
9+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
INSERT INTO member (name, email, phone, address) VALUES
2+
('Alice Johnson', '[email protected]', '123-456-7890', '123 Main St, Springfield'),
3+
('Bob Smith', '[email protected]', '987-654-3210', '456 Elm St, Metropolis'),
4+
('Charlie Brown', '[email protected]', NULL, '789 Oak St, Smallville'),
5+
('Diana Prince', '[email protected]', '555-123-4567', NULL);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.example.batch;
2+
3+
import org.apache.commons.lang3.StringUtils;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
import org.springframework.boot.SpringApplication;
7+
import org.springframework.boot.autoconfigure.SpringBootApplication;
8+
9+
@SpringBootApplication
10+
public class DbAndCsvBatchApp {
11+
private static final Logger log = LoggerFactory.getLogger(DbAndCsvBatchApp.class);
12+
13+
public static void main(String[] args) {
14+
logOnlyBatchDetails();
15+
16+
System.exit(SpringApplication.exit(SpringApplication.run(DbAndCsvBatchApp.class, args)));
17+
}
18+
19+
/** バッチの起動引数をログ出力して確認できるようにする Logs the batch startup arguments for verification. */
20+
private static void logOnlyBatchDetails() {
21+
String commandProp = System.getProperty("sun.java.command");
22+
log.info(
23+
"##### KEY:\"sun.java.command\", VALUE:\"{}\"", System.getProperty("sun.java.command"));
24+
String jobName = StringUtils.substringAfterLast(commandProp, "--spring.batch.job.name=");
25+
jobName = StringUtils.substringBefore(jobName, " ");
26+
String profile = StringUtils.substringAfterLast(commandProp, "--spring.profiles.active=");
27+
profile = StringUtils.substringBefore(profile, " ");
28+
log.info("##### Spring Batch ##### - Job: {}, Profile: {}", jobName, profile);
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.example.batch.job;
2+
3+
import com.example.batch.logic.CsvToDbLogic;
4+
import lombok.RequiredArgsConstructor;
5+
import lombok.extern.slf4j.Slf4j;
6+
import org.springframework.batch.core.Job;
7+
import org.springframework.batch.core.Step;
8+
import org.springframework.batch.core.job.builder.JobBuilder;
9+
import org.springframework.batch.core.launch.support.RunIdIncrementer;
10+
import org.springframework.batch.core.repository.JobRepository;
11+
import org.springframework.batch.core.step.builder.StepBuilder;
12+
import org.springframework.context.annotation.Bean;
13+
import org.springframework.context.annotation.Configuration;
14+
import org.springframework.transaction.PlatformTransactionManager;
15+
16+
@Slf4j
17+
@RequiredArgsConstructor
18+
@Configuration
19+
public class CsvToDbJob {
20+
21+
private final CsvToDbLogic logic;
22+
23+
/** 実行時引数で入力するジョブ名は、このstrJobNameと一致する必要がある */
24+
private static final String BATCH_JOB_NAME = "CSV_TO_DB";
25+
26+
@Bean("CSV_TO_DB")
27+
public Job sample(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
28+
log.info("----------- START ----------- DbToCsvJob ----------- START -----------");
29+
Step myStep =
30+
new StepBuilder(BATCH_JOB_NAME + "-step", jobRepository)
31+
.tasklet(logic, transactionManager)
32+
.build();
33+
34+
Job myJob =
35+
new JobBuilder(BATCH_JOB_NAME, jobRepository)
36+
.incrementer(new RunIdIncrementer())
37+
.listener(BATCH_JOB_NAME)
38+
.start(myStep)
39+
.build();
40+
log.info("----------- END ----------- DbToCsvJob ----------- END -----------");
41+
42+
return myJob;
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.example.batch.job;
2+
3+
import com.example.batch.logic.DbToCsvLogic;
4+
import lombok.RequiredArgsConstructor;
5+
import lombok.extern.slf4j.Slf4j;
6+
import org.springframework.batch.core.Job;
7+
import org.springframework.batch.core.Step;
8+
import org.springframework.batch.core.job.builder.JobBuilder;
9+
import org.springframework.batch.core.launch.support.RunIdIncrementer;
10+
import org.springframework.batch.core.repository.JobRepository;
11+
import org.springframework.batch.core.step.builder.StepBuilder;
12+
import org.springframework.context.annotation.Bean;
13+
import org.springframework.context.annotation.Configuration;
14+
import org.springframework.transaction.PlatformTransactionManager;
15+
16+
@Slf4j
17+
@RequiredArgsConstructor
18+
@Configuration
19+
public class DbToCsvJob {
20+
21+
private final DbToCsvLogic logic;
22+
23+
/** 実行時引数で入力するジョブ名は、このstrJobNameと一致する必要がある */
24+
private static final String BATCH_JOB_NAME = "DB_TO_CSV";
25+
26+
@Bean("DB_TO_CSV")
27+
public Job sample(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
28+
log.info("----------- START ----------- DbToCsvJob ----------- START -----------");
29+
Step myStep =
30+
new StepBuilder(BATCH_JOB_NAME + "-step", jobRepository)
31+
.tasklet(logic, transactionManager)
32+
.build();
33+
34+
Job myJob =
35+
new JobBuilder(BATCH_JOB_NAME, jobRepository)
36+
.incrementer(new RunIdIncrementer())
37+
.listener(BATCH_JOB_NAME)
38+
.start(myStep)
39+
.build();
40+
log.info("----------- END ----------- DbToCsvJob ----------- END -----------");
41+
42+
return myJob;
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.example.batch.logic;
2+
3+
import com.example.batch.service.CsvToDbService;
4+
import lombok.RequiredArgsConstructor;
5+
import lombok.extern.slf4j.Slf4j;
6+
import org.springframework.batch.core.ExitStatus;
7+
import org.springframework.batch.core.StepContribution;
8+
import org.springframework.batch.core.scope.context.ChunkContext;
9+
import org.springframework.batch.core.step.tasklet.Tasklet;
10+
import org.springframework.batch.repeat.RepeatStatus;
11+
import org.springframework.stereotype.Component;
12+
13+
@Slf4j
14+
@RequiredArgsConstructor
15+
@Component
16+
public class CsvToDbLogic implements Tasklet {
17+
private final CsvToDbService csvToDbService;
18+
19+
@Override
20+
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext)
21+
throws Exception {
22+
23+
log.info("----------- START ----------- CsvToDbLogic ----------- START -----------");
24+
try {
25+
csvToDbService.execute();
26+
} catch (Exception e) {
27+
contribution.setExitStatus(ExitStatus.FAILED);
28+
log.error("job failure", e);
29+
}
30+
log.info("----------- END ----------- CsvToDbLogic ----------- END -----------");
31+
32+
return RepeatStatus.FINISHED;
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.example.batch.logic;
2+
3+
import com.example.batch.service.DbToCsvService;
4+
import lombok.RequiredArgsConstructor;
5+
import lombok.extern.slf4j.Slf4j;
6+
import org.springframework.batch.core.ExitStatus;
7+
import org.springframework.batch.core.StepContribution;
8+
import org.springframework.batch.core.scope.context.ChunkContext;
9+
import org.springframework.batch.core.step.tasklet.Tasklet;
10+
import org.springframework.batch.repeat.RepeatStatus;
11+
import org.springframework.stereotype.Component;
12+
13+
@Slf4j
14+
@RequiredArgsConstructor
15+
@Component
16+
public class DbToCsvLogic implements Tasklet {
17+
private final DbToCsvService dbToCsvService;
18+
19+
@Override
20+
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext)
21+
throws Exception {
22+
23+
log.info("----------- START ----------- DbToCsvLogic ----------- START -----------");
24+
try {
25+
dbToCsvService.execute();
26+
} catch (Exception e) {
27+
contribution.setExitStatus(ExitStatus.FAILED);
28+
log.error("job failure", e);
29+
}
30+
log.info("----------- END ----------- DbToCsvLogic ----------- END -----------");
31+
32+
return RepeatStatus.FINISHED;
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.example.batch.service;
2+
3+
import lombok.RequiredArgsConstructor;
4+
import lombok.extern.slf4j.Slf4j;
5+
import org.springframework.stereotype.Service;
6+
7+
@Slf4j
8+
@RequiredArgsConstructor
9+
@Service
10+
public class CsvToDbService {
11+
12+
public void execute() throws Exception {
13+
log.info("----------- START ----------- CsvToDbService ----------- START -----------");
14+
15+
log.info(
16+
System.lineSeparator().repeat(3)
17+
+ "***************************************************************************************"
18+
+ System.lineSeparator()
19+
+ "*** ここにバッチの業務ロジックを記述する !!! ***"
20+
+ System.lineSeparator()
21+
+ "*** Business logic for the batch process goes here! ***"
22+
+ System.lineSeparator()
23+
+ "***************************************************************************************"
24+
+ System.lineSeparator().repeat(3));
25+
26+
log.info("----------- END ----------- CsvToDbService ----------- END -----------");
27+
}
28+
}

0 commit comments

Comments
 (0)