change directory
This commit is contained in:
37
spring-batch/practice/.gitignore
vendored
Normal file
37
spring-batch/practice/.gitignore
vendored
Normal file
@@ -0,0 +1,37 @@
|
||||
HELP.md
|
||||
.gradle
|
||||
build/
|
||||
!gradle/wrapper/gradle-wrapper.jar
|
||||
!**/src/main/**/build/
|
||||
!**/src/test/**/build/
|
||||
|
||||
### STS ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
bin/
|
||||
!**/src/main/**/bin/
|
||||
!**/src/test/**/bin/
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
out/
|
||||
!**/src/main/**/out/
|
||||
!**/src/test/**/out/
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
|
||||
### VS Code ###
|
||||
.vscode/
|
||||
38
spring-batch/practice/build.gradle
Normal file
38
spring-batch/practice/build.gradle
Normal file
@@ -0,0 +1,38 @@
|
||||
plugins {
|
||||
id 'org.springframework.boot' version '2.4.5'
|
||||
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
|
||||
id 'java'
|
||||
}
|
||||
|
||||
group = 'com.example'
|
||||
version = '0.0.1-SNAPSHOT'
|
||||
sourceCompatibility = '11'
|
||||
|
||||
configurations {
|
||||
compileOnly {
|
||||
extendsFrom annotationProcessor
|
||||
}
|
||||
}
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation 'org.springframework.boot:spring-boot-starter-batch'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||
|
||||
compileOnly 'org.projectlombok:lombok'
|
||||
developmentOnly 'org.springframework.boot:spring-boot-devtools'
|
||||
runtimeOnly 'mysql:mysql-connector-java'
|
||||
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
|
||||
annotationProcessor 'org.projectlombok:lombok'
|
||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||
testImplementation 'org.springframework.batch:spring-batch-test'
|
||||
}
|
||||
|
||||
test {
|
||||
useJUnitPlatform()
|
||||
}
|
||||
BIN
spring-batch/practice/gradle/wrapper/gradle-wrapper.jar
vendored
Normal file
BIN
spring-batch/practice/gradle/wrapper/gradle-wrapper.jar
vendored
Normal file
Binary file not shown.
5
spring-batch/practice/gradle/wrapper/gradle-wrapper.properties
vendored
Normal file
5
spring-batch/practice/gradle/wrapper/gradle-wrapper.properties
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
184
spring-batch/practice/gradlew
vendored
Normal file
184
spring-batch/practice/gradlew
vendored
Normal file
@@ -0,0 +1,184 @@
|
||||
#!/usr/bin/env sh
|
||||
|
||||
#
|
||||
# Copyright 2015 the original author or authors.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# https://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
|
||||
##############################################################################
|
||||
##
|
||||
## Gradle start up script for UN*X
|
||||
##
|
||||
##############################################################################
|
||||
|
||||
# Attempt to set APP_HOME
|
||||
# Resolve links: $0 may be a link
|
||||
PRG="$0"
|
||||
# Need this for relative symlinks.
|
||||
while [ -h "$PRG" ]; do
|
||||
ls=$(ls -ld "$PRG")
|
||||
link=$(expr "$ls" : '.*-> \(.*\)$')
|
||||
if expr "$link" : '/.*' >/dev/null; then
|
||||
PRG="$link"
|
||||
else
|
||||
PRG=$(dirname "$PRG")"/$link"
|
||||
fi
|
||||
done
|
||||
SAVED="$(pwd)"
|
||||
cd "$(dirname \"$PRG\")/" >/dev/null
|
||||
APP_HOME="$(pwd -P)"
|
||||
cd "$SAVED" >/dev/null
|
||||
|
||||
APP_NAME="Gradle"
|
||||
APP_BASE_NAME=$(basename "$0")
|
||||
|
||||
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
|
||||
|
||||
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
||||
MAX_FD="maximum"
|
||||
|
||||
warn() {
|
||||
echo "$*"
|
||||
}
|
||||
|
||||
die() {
|
||||
echo
|
||||
echo "$*"
|
||||
echo
|
||||
exit 1
|
||||
}
|
||||
|
||||
# OS specific support (must be 'true' or 'false').
|
||||
cygwin=false
|
||||
msys=false
|
||||
darwin=false
|
||||
nonstop=false
|
||||
case "$(uname)" in
|
||||
CYGWIN*)
|
||||
cygwin=true
|
||||
;;
|
||||
Darwin*)
|
||||
darwin=true
|
||||
;;
|
||||
MINGW*)
|
||||
msys=true
|
||||
;;
|
||||
NONSTOP*)
|
||||
nonstop=true
|
||||
;;
|
||||
esac
|
||||
|
||||
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
|
||||
|
||||
# Determine the Java command to use to start the JVM.
|
||||
if [ -n "$JAVA_HOME" ]; then
|
||||
if [ -x "$JAVA_HOME/jre/sh/java" ]; then
|
||||
# IBM's JDK on AIX uses strange locations for the executables
|
||||
JAVACMD="$JAVA_HOME/jre/sh/java"
|
||||
else
|
||||
JAVACMD="$JAVA_HOME/bin/java"
|
||||
fi
|
||||
if [ ! -x "$JAVACMD" ]; then
|
||||
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
|
||||
|
||||
Please set the JAVA_HOME variable in your environment to match the
|
||||
location of your Java installation."
|
||||
fi
|
||||
else
|
||||
JAVACMD="java"
|
||||
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||
|
||||
Please set the JAVA_HOME variable in your environment to match the
|
||||
location of your Java installation."
|
||||
fi
|
||||
|
||||
# Increase the maximum file descriptors if we can.
|
||||
if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ]; then
|
||||
MAX_FD_LIMIT=$(ulimit -H -n)
|
||||
if [ $? -eq 0 ]; then
|
||||
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ]; then
|
||||
MAX_FD="$MAX_FD_LIMIT"
|
||||
fi
|
||||
ulimit -n $MAX_FD
|
||||
if [ $? -ne 0 ]; then
|
||||
warn "Could not set maximum file descriptor limit: $MAX_FD"
|
||||
fi
|
||||
else
|
||||
warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
|
||||
fi
|
||||
fi
|
||||
|
||||
# For Darwin, add options to specify how the application appears in the dock
|
||||
if $darwin; then
|
||||
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
|
||||
fi
|
||||
|
||||
# For Cygwin or MSYS, switch paths to Windows format before running java
|
||||
if [ "$cygwin" = "true" -o "$msys" = "true" ]; then
|
||||
APP_HOME=$(cygpath --path --mixed "$APP_HOME")
|
||||
CLASSPATH=$(cygpath --path --mixed "$CLASSPATH")
|
||||
|
||||
JAVACMD=$(cygpath --unix "$JAVACMD")
|
||||
|
||||
# We build the pattern for arguments to be converted via cygpath
|
||||
ROOTDIRSRAW=$(find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null)
|
||||
SEP=""
|
||||
for dir in $ROOTDIRSRAW; do
|
||||
ROOTDIRS="$ROOTDIRS$SEP$dir"
|
||||
SEP="|"
|
||||
done
|
||||
OURCYGPATTERN="(^($ROOTDIRS))"
|
||||
# Add a user-defined pattern to the cygpath arguments
|
||||
if [ "$GRADLE_CYGPATTERN" != "" ]; then
|
||||
OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
|
||||
fi
|
||||
# Now convert the arguments - kludge to limit ourselves to /bin/sh
|
||||
i=0
|
||||
for arg in "$@"; do
|
||||
CHECK=$(echo "$arg" | egrep -c "$OURCYGPATTERN" -)
|
||||
CHECK2=$(echo "$arg" | egrep -c "^-") ### Determine if an option
|
||||
|
||||
if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ]; then ### Added a condition
|
||||
eval $(echo args$i)=$(cygpath --path --ignore --mixed "$arg")
|
||||
else
|
||||
eval $(echo args$i)="\"$arg\""
|
||||
fi
|
||||
i=$(expr $i + 1)
|
||||
done
|
||||
case $i in
|
||||
0) set -- ;;
|
||||
1) set -- "$args0" ;;
|
||||
2) set -- "$args0" "$args1" ;;
|
||||
3) set -- "$args0" "$args1" "$args2" ;;
|
||||
4) set -- "$args0" "$args1" "$args2" "$args3" ;;
|
||||
5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
|
||||
6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
|
||||
7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
|
||||
8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
|
||||
9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
|
||||
esac
|
||||
fi
|
||||
|
||||
# Escape application args
|
||||
save() {
|
||||
for i; do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/"; done
|
||||
echo " "
|
||||
}
|
||||
APP_ARGS=$(save "$@")
|
||||
|
||||
# Collect all arguments for the java command, following the shell quoting and substitution rules
|
||||
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
|
||||
|
||||
exec "$JAVACMD" "$@"
|
||||
89
spring-batch/practice/gradlew.bat
vendored
Normal file
89
spring-batch/practice/gradlew.bat
vendored
Normal file
@@ -0,0 +1,89 @@
|
||||
@rem
|
||||
@rem Copyright 2015 the original author or authors.
|
||||
@rem
|
||||
@rem Licensed under the Apache License, Version 2.0 (the "License");
|
||||
@rem you may not use this file except in compliance with the License.
|
||||
@rem You may obtain a copy of the License at
|
||||
@rem
|
||||
@rem https://www.apache.org/licenses/LICENSE-2.0
|
||||
@rem
|
||||
@rem Unless required by applicable law or agreed to in writing, software
|
||||
@rem distributed under the License is distributed on an "AS IS" BASIS,
|
||||
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
@rem See the License for the specific language governing permissions and
|
||||
@rem limitations under the License.
|
||||
@rem
|
||||
|
||||
@if "%DEBUG%" == "" @echo off
|
||||
@rem ##########################################################################
|
||||
@rem
|
||||
@rem Gradle startup script for Windows
|
||||
@rem
|
||||
@rem ##########################################################################
|
||||
|
||||
@rem Set local scope for the variables with windows NT shell
|
||||
if "%OS%"=="Windows_NT" setlocal
|
||||
|
||||
set DIRNAME=%~dp0
|
||||
if "%DIRNAME%" == "" set DIRNAME=.
|
||||
set APP_BASE_NAME=%~n0
|
||||
set APP_HOME=%DIRNAME%
|
||||
|
||||
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
|
||||
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
|
||||
|
||||
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
|
||||
|
||||
@rem Find java.exe
|
||||
if defined JAVA_HOME goto findJavaFromJavaHome
|
||||
|
||||
set JAVA_EXE=java.exe
|
||||
%JAVA_EXE% -version >NUL 2>&1
|
||||
if "%ERRORLEVEL%" == "0" goto execute
|
||||
|
||||
echo.
|
||||
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||
echo.
|
||||
echo Please set the JAVA_HOME variable in your environment to match the
|
||||
echo location of your Java installation.
|
||||
|
||||
goto fail
|
||||
|
||||
:findJavaFromJavaHome
|
||||
set JAVA_HOME=%JAVA_HOME:"=%
|
||||
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
|
||||
|
||||
if exist "%JAVA_EXE%" goto execute
|
||||
|
||||
echo.
|
||||
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
|
||||
echo.
|
||||
echo Please set the JAVA_HOME variable in your environment to match the
|
||||
echo location of your Java installation.
|
||||
|
||||
goto fail
|
||||
|
||||
:execute
|
||||
@rem Setup the command line
|
||||
|
||||
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
|
||||
|
||||
|
||||
@rem Execute Gradle
|
||||
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
|
||||
|
||||
:end
|
||||
@rem End local scope for the variables with windows NT shell
|
||||
if "%ERRORLEVEL%"=="0" goto mainEnd
|
||||
|
||||
:fail
|
||||
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
|
||||
rem the _cmd.exe /c_ return code!
|
||||
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
|
||||
exit /b 1
|
||||
|
||||
:mainEnd
|
||||
if "%OS%"=="Windows_NT" endlocal
|
||||
|
||||
:omega
|
||||
1
spring-batch/practice/http/job.http
Normal file
1
spring-batch/practice/http/job.http
Normal file
@@ -0,0 +1 @@
|
||||
http://localhost:8080//jobs/create-articles
|
||||
1
spring-batch/practice/settings.gradle
Normal file
1
spring-batch/practice/settings.gradle
Normal file
@@ -0,0 +1 @@
|
||||
rootProject.name = 'spring-batch'
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.example.springbatch;
|
||||
|
||||
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableBatchProcessing
|
||||
@EnableScheduling
|
||||
public class SpringBatchApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringBatchApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.example.springbatch.application.controller;
|
||||
|
||||
import com.example.springbatch.application.service.JobService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/jobs")
|
||||
@RequiredArgsConstructor
|
||||
public class JobController {
|
||||
|
||||
private final JobService jobService;
|
||||
|
||||
@GetMapping("/create-articles")
|
||||
public void runCreateArticleJob() throws Exception {
|
||||
jobService.runCreateArticleJob();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
package com.example.springbatch.application.job;
|
||||
|
||||
import com.example.springbatch.application.job.param.CreateArticleJobParam;
|
||||
import com.example.springbatch.application.model.ArticleModel;
|
||||
import com.example.springbatch.domain.entity.Article;
|
||||
import com.example.springbatch.domain.repository.ArticleRepository;
|
||||
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.core.launch.support.RunIdIncrementer;
|
||||
import org.springframework.batch.item.ItemProcessor;
|
||||
import org.springframework.batch.item.ItemWriter;
|
||||
import org.springframework.batch.item.data.RepositoryItemWriter;
|
||||
import org.springframework.batch.item.data.builder.RepositoryItemWriterBuilder;
|
||||
import org.springframework.batch.item.database.builder.JdbcBatchItemWriterBuilder;
|
||||
import org.springframework.batch.item.database.builder.JpaItemWriterBuilder;
|
||||
import org.springframework.batch.item.file.FlatFileItemReader;
|
||||
import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder;
|
||||
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.sql.DataSource;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Configuration
|
||||
@Slf4j
|
||||
public class CreateArticleJobConfig {
|
||||
|
||||
private final JobBuilderFactory jobBuilderFactory;
|
||||
private final StepBuilderFactory stepBuilderFactory;
|
||||
private final ArticleRepository articleRepository;
|
||||
private final CreateArticleJobParam createArticleJobParam;
|
||||
private final JdbcTemplate demoJdbcTemplate;
|
||||
private final EntityManagerFactory demoEntityManagerFactory;
|
||||
private final DataSource demoDataSource;
|
||||
|
||||
public CreateArticleJobConfig(JobBuilderFactory jobBuilderFactory,
|
||||
StepBuilderFactory stepBuilderFactory,
|
||||
ArticleRepository articleRepository,
|
||||
CreateArticleJobParam createArticleJobParam,
|
||||
@Qualifier("demoJdbcTemplate") JdbcTemplate demoJdbcTemplate,
|
||||
@Qualifier("demoEntityManagerFactory") EntityManagerFactory demoEntityManagerFactory,
|
||||
DataSource demoDataSource) {
|
||||
this.jobBuilderFactory = jobBuilderFactory;
|
||||
this.stepBuilderFactory = stepBuilderFactory;
|
||||
this.articleRepository = articleRepository;
|
||||
this.createArticleJobParam = createArticleJobParam;
|
||||
this.demoJdbcTemplate = demoJdbcTemplate;
|
||||
this.demoEntityManagerFactory = demoEntityManagerFactory;
|
||||
this.demoDataSource = demoDataSource;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Job createArticleJob() {
|
||||
return jobBuilderFactory.get("createArticleJob")
|
||||
.incrementer(new RunIdIncrementer())
|
||||
.start(createArticleStep())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@JobScope
|
||||
public Step createArticleStep() {
|
||||
return stepBuilderFactory.get("createArticleStep")
|
||||
.<ArticleModel, Article>chunk(1000)
|
||||
.reader(createArticleReader())
|
||||
.processor(createArticleProcessor())
|
||||
.writer(createArticleWriterJPA())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@StepScope
|
||||
public FlatFileItemReader<ArticleModel> createArticleReader() {
|
||||
log.info("PARAM!!!!!! {}", createArticleJobParam.getName());
|
||||
return new FlatFileItemReaderBuilder<ArticleModel>()
|
||||
.name("createArticleReader")
|
||||
.resource(new ClassPathResource("Articles.csv"))
|
||||
.delimited()
|
||||
.names("title", "content")
|
||||
.fieldSetMapper(new BeanWrapperFieldSetMapper<>())
|
||||
.targetType(ArticleModel.class)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ItemProcessor<ArticleModel, Article> createArticleProcessor() {
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
return articleModel -> Article.builder()
|
||||
.title(articleModel.getTitle())
|
||||
.content(articleModel.getContent())
|
||||
.createdAt(now)
|
||||
.build();
|
||||
}
|
||||
|
||||
// JDBC
|
||||
@Bean
|
||||
public ItemWriter<Article> createArticleWriterJDBC() {
|
||||
return articles -> demoJdbcTemplate.batchUpdate("insert into Article (title, content, createdAt) values (?, ?, ?)",
|
||||
articles,
|
||||
1000,
|
||||
(ps, article) -> {
|
||||
ps.setObject(1, article.getTitle());
|
||||
ps.setObject(2, article.getContent());
|
||||
ps.setObject(3, article.getCreatedAt());
|
||||
});
|
||||
}
|
||||
|
||||
// JPA
|
||||
@Bean
|
||||
public RepositoryItemWriter<Article> createArticleWriterJPA() {
|
||||
return new RepositoryItemWriterBuilder<Article>()
|
||||
.repository(articleRepository)
|
||||
.build();
|
||||
}
|
||||
|
||||
// ======================
|
||||
|
||||
// // JDBC
|
||||
// @Bean
|
||||
// public ItemWriter<Article> writerJDBC() {
|
||||
// return new JdbcBatchItemWriterBuilder<Article>()
|
||||
// .dataSource(demoDataSource)
|
||||
// .build();
|
||||
// }
|
||||
|
||||
// JPA
|
||||
@Bean
|
||||
public ItemWriter<Article> writerJPA() {
|
||||
return new JpaItemWriterBuilder<Article>()
|
||||
.entityManagerFactory(this.demoEntityManagerFactory)
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
package com.example.springbatch.application.job;
|
||||
|
||||
import com.example.springbatch.application.model.BoardModel;
|
||||
import com.example.springbatch.domain.entity.Article;
|
||||
import com.example.springbatch.domain.entity.Board;
|
||||
import com.example.springbatch.utils.FileUtils;
|
||||
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.core.launch.support.RunIdIncrementer;
|
||||
import org.springframework.batch.core.partition.support.MultiResourcePartitioner;
|
||||
import org.springframework.batch.core.partition.support.Partitioner;
|
||||
import org.springframework.batch.core.partition.support.TaskExecutorPartitionHandler;
|
||||
import org.springframework.batch.item.ItemProcessor;
|
||||
import org.springframework.batch.item.ItemWriter;
|
||||
import org.springframework.batch.item.file.FlatFileItemReader;
|
||||
import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder;
|
||||
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.UrlResource;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.MalformedURLException;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Configuration
|
||||
@Slf4j
|
||||
public class CreateBoardJobConfig {
|
||||
|
||||
private static final int CHUNK_SIZE = 1000;
|
||||
private static final int GRID_SIZE = 10;
|
||||
private static final int POOL_SIZE = 5;
|
||||
|
||||
private final JobBuilderFactory jobBuilderFactory;
|
||||
private final StepBuilderFactory stepBuilderFactory;
|
||||
private final JdbcTemplate demoJdbcTemplate;
|
||||
|
||||
public CreateBoardJobConfig(JobBuilderFactory jobBuilderFactory,
|
||||
StepBuilderFactory stepBuilderFactory,
|
||||
@Qualifier("demoJdbcTemplate") JdbcTemplate demoJdbcTemplate) {
|
||||
this.jobBuilderFactory = jobBuilderFactory;
|
||||
this.stepBuilderFactory = stepBuilderFactory;
|
||||
this.demoJdbcTemplate = demoJdbcTemplate;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Job createBoardJob() throws MalformedURLException {
|
||||
return jobBuilderFactory.get("createBoardJob")
|
||||
.incrementer(new RunIdIncrementer())
|
||||
.start(createBoardManager())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Step createBoardManager() throws MalformedURLException {
|
||||
return stepBuilderFactory.get("createBoardManager")
|
||||
.partitioner("createBoardPartitioner", createBoardPartitioner())
|
||||
.partitionHandler(createBoardPartitionHandler())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@StepScope
|
||||
public Partitioner createBoardPartitioner() {
|
||||
MultiResourcePartitioner partitioner = new MultiResourcePartitioner();
|
||||
|
||||
Path path = Paths.get("/Users/bobby/Desktop/kim/study/data");
|
||||
Resource[] resources = FileUtils.stream(path)
|
||||
.filter(File::isFile)
|
||||
.filter(file -> "csv".equals(StringUtils.getFilenameExtension(file.getPath())))
|
||||
.map(FileSystemResource::new)
|
||||
.toArray(Resource[]::new);
|
||||
|
||||
partitioner.setResources(resources);
|
||||
partitioner.partition(GRID_SIZE);
|
||||
return partitioner;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ThreadPoolTaskExecutor createBoardTaskExecutor() {
|
||||
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
|
||||
taskExecutor.setCorePoolSize(POOL_SIZE);
|
||||
taskExecutor.setMaxPoolSize(POOL_SIZE);
|
||||
taskExecutor.setWaitForTasksToCompleteOnShutdown(true);
|
||||
taskExecutor.initialize();
|
||||
return taskExecutor;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public TaskExecutorPartitionHandler createBoardPartitionHandler() throws MalformedURLException {
|
||||
TaskExecutorPartitionHandler partitionHandler = new TaskExecutorPartitionHandler();
|
||||
partitionHandler.setStep(createBoardWorker());
|
||||
partitionHandler.setGridSize(GRID_SIZE);
|
||||
partitionHandler.setTaskExecutor(createBoardTaskExecutor());
|
||||
return partitionHandler;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Step createBoardWorker() throws MalformedURLException {
|
||||
return stepBuilderFactory.get("createBoardStep")
|
||||
.<BoardModel, Board>chunk(1000)
|
||||
.reader(createBoardReader(null))
|
||||
.processor(createBoardProcessor())
|
||||
.writer(createBoardWriterJDBC())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@StepScope
|
||||
public FlatFileItemReader<BoardModel> createBoardReader(@Value("#{stepExecutionContext[fileName]}") String fileName) throws MalformedURLException {
|
||||
return new FlatFileItemReaderBuilder<BoardModel>()
|
||||
.name("createBoardReader")
|
||||
.resource(new UrlResource(fileName))
|
||||
.delimited()
|
||||
.names("title", "content")
|
||||
.fieldSetMapper(new BeanWrapperFieldSetMapper<>())
|
||||
.targetType(BoardModel.class)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ItemProcessor<BoardModel, Board> createBoardProcessor() {
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
return boardModel -> Board.builder()
|
||||
.title(boardModel.getTitle())
|
||||
.content(boardModel.getContent())
|
||||
.createdAt(now)
|
||||
.build();
|
||||
}
|
||||
|
||||
// JDBC
|
||||
@Bean
|
||||
public ItemWriter<Board> createBoardWriterJDBC() {
|
||||
return boards -> demoJdbcTemplate.batchUpdate("insert into Board (title, content, createdAt) values (?, ?, ?)",
|
||||
boards,
|
||||
CHUNK_SIZE,
|
||||
(ps, board) -> {
|
||||
ps.setObject(1, board.getTitle());
|
||||
ps.setObject(2, board.getContent());
|
||||
ps.setObject(3, board.getCreatedAt());
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
package com.example.springbatch.application.job;
|
||||
|
||||
import com.example.springbatch.application.job.param.CreateOddBoardJobParam;
|
||||
import com.example.springbatch.application.model.BoardModel;
|
||||
import com.example.springbatch.domain.entity.Board;
|
||||
import com.example.springbatch.domain.entity.OddBoard;
|
||||
import com.example.springbatch.domain.repository.BoardRepository;
|
||||
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.StepBuilderFactory;
|
||||
import org.springframework.batch.core.configuration.annotation.StepScope;
|
||||
import org.springframework.batch.core.launch.support.RunIdIncrementer;
|
||||
import org.springframework.batch.core.partition.support.Partitioner;
|
||||
import org.springframework.batch.core.partition.support.TaskExecutorPartitionHandler;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
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.file.FlatFileItemReader;
|
||||
import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder;
|
||||
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.io.UrlResource;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Configuration
|
||||
@Slf4j
|
||||
public class CreateOddBoardJobConfig {
|
||||
|
||||
private static final int CHUNK_SIZE = 1000;
|
||||
private static final int GRID_SIZE = 10;
|
||||
private static final int POOL_SIZE = 5;
|
||||
|
||||
private final JobBuilderFactory jobBuilderFactory;
|
||||
private final StepBuilderFactory stepBuilderFactory;
|
||||
private final CreateOddBoardJobParam createOddBoardJobParam;
|
||||
private final BoardRepository boardRepository;
|
||||
private final JdbcTemplate demoJdbcTemplate;
|
||||
|
||||
public CreateOddBoardJobConfig(JobBuilderFactory jobBuilderFactory,
|
||||
StepBuilderFactory stepBuilderFactory,
|
||||
CreateOddBoardJobParam createOddBoardJobParam,
|
||||
BoardRepository boardRepository,
|
||||
@Qualifier("demoJdbcTemplate") JdbcTemplate demoJdbcTemplate) {
|
||||
this.jobBuilderFactory = jobBuilderFactory;
|
||||
this.stepBuilderFactory = stepBuilderFactory;
|
||||
this.createOddBoardJobParam = createOddBoardJobParam;
|
||||
this.boardRepository = boardRepository;
|
||||
this.demoJdbcTemplate = demoJdbcTemplate;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Job createOddBoardJob() throws MalformedURLException {
|
||||
return jobBuilderFactory.get("createOddBoardJob")
|
||||
.incrementer(new RunIdIncrementer())
|
||||
.start(createOddBoardManager())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Step createOddBoardManager() throws MalformedURLException {
|
||||
return stepBuilderFactory.get("createOddBoardManager")
|
||||
.partitioner("createOddBoardPartitioner", createOddBoardPartitioner())
|
||||
.partitionHandler(createOddBoardPartitionHandler())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@StepScope
|
||||
public Partitioner createOddBoardPartitioner() {
|
||||
return gridSize -> {
|
||||
long min = createOddBoardJobParam.getMinId();
|
||||
long max = createOddBoardJobParam.getMaxId();
|
||||
long targetSize = (max - min) / gridSize + 1;
|
||||
|
||||
Map<String, ExecutionContext> result = new HashMap<>();
|
||||
int number = 0;
|
||||
long start = min;
|
||||
long end = start + targetSize - 1;
|
||||
|
||||
while (start <= max) {
|
||||
ExecutionContext value = new ExecutionContext();
|
||||
result.put("partition" + number, value);
|
||||
|
||||
if (end >= max) {
|
||||
end = max;
|
||||
}
|
||||
value.putLong("minValue", start);
|
||||
value.putLong("maxValue", end);
|
||||
start += targetSize;
|
||||
end += targetSize;
|
||||
number++;
|
||||
}
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ThreadPoolTaskExecutor createOddBoardTaskExecutor() {
|
||||
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
|
||||
taskExecutor.setCorePoolSize(POOL_SIZE);
|
||||
taskExecutor.setMaxPoolSize(POOL_SIZE);
|
||||
taskExecutor.setWaitForTasksToCompleteOnShutdown(true);
|
||||
taskExecutor.initialize();
|
||||
return taskExecutor;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public TaskExecutorPartitionHandler createOddBoardPartitionHandler() {
|
||||
TaskExecutorPartitionHandler partitionHandler = new TaskExecutorPartitionHandler();
|
||||
partitionHandler.setStep(createOddBoardWorker());
|
||||
partitionHandler.setGridSize(GRID_SIZE);
|
||||
partitionHandler.setTaskExecutor(createOddBoardTaskExecutor());
|
||||
return partitionHandler;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Step createOddBoardWorker() {
|
||||
return stepBuilderFactory.get("createOddBoardWorker")
|
||||
.<Board, OddBoard>chunk(CHUNK_SIZE)
|
||||
.reader(createOddBoardReader(null, null))
|
||||
.processor(createOddBoardProcessor())
|
||||
.writer(createOddBoardWriter())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@StepScope
|
||||
public RepositoryItemReader<Board> createOddBoardReader(
|
||||
@Value("#{stepExecutionContext[minValue]}") Long minValue,
|
||||
@Value("#{stepExecutionContext[maxValue]}") Long maxValue) {
|
||||
return new RepositoryItemReaderBuilder<Board>()
|
||||
.name("createOddBoardReader")
|
||||
.repository(boardRepository)
|
||||
.methodName("findAllByIdBetween")
|
||||
.arguments(minValue, maxValue)
|
||||
.pageSize(CHUNK_SIZE)
|
||||
.sorts(Map.of("id", Sort.Direction.ASC))
|
||||
.build();
|
||||
}
|
||||
|
||||
public ItemProcessor<Board, OddBoard> createOddBoardProcessor() {
|
||||
return board -> {
|
||||
if (board.getId() % 2 == 1) {
|
||||
return OddBoard.builder()
|
||||
.id(board.getId())
|
||||
.title(board.getTitle())
|
||||
.content(board.getContent())
|
||||
.createdAt(LocalDateTime.now())
|
||||
.build();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ItemWriter<OddBoard> createOddBoardWriter() {
|
||||
return boards -> demoJdbcTemplate.batchUpdate("insert into OddBoard (id, title, content, createdAt) values (?, ?, ?, ?)",
|
||||
boards,
|
||||
CHUNK_SIZE,
|
||||
(ps, board) -> {
|
||||
ps.setObject(1, board.getId());
|
||||
ps.setObject(2, board.getTitle());
|
||||
ps.setObject(3, board.getContent());
|
||||
ps.setObject(4, board.getCreatedAt());
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.example.springbatch.application.job.listener;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.batch.core.BatchStatus;
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.core.explore.JobExplorer;
|
||||
import org.springframework.batch.core.repository.JobRepository;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.event.ContextRefreshedEvent;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Set;
|
||||
|
||||
@Configuration
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class ContextRefreshedEventListener implements ApplicationListener<ContextRefreshedEvent> {
|
||||
private final JobExplorer jobExplorer;
|
||||
private final JobRepository jobRepository;
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(ContextRefreshedEvent event) {
|
||||
log.info("Stop running jobs.");
|
||||
for (String jobName : jobExplorer.getJobNames()) {
|
||||
Set<JobExecution> runningJobExecutions = jobExplorer.findRunningJobExecutions(jobName);
|
||||
|
||||
for (JobExecution jobExecution : runningJobExecutions) {
|
||||
log.warn("!!!!!!!!!!: {}, {} ", jobExecution.getJobId(), jobExecution.getStatus());
|
||||
jobExecution.setStatus(BatchStatus.STOPPED);
|
||||
jobExecution.setEndTime(new Date());
|
||||
for(StepExecution stepExecution : jobExecution.getStepExecutions()) {
|
||||
if (stepExecution.getStatus().isRunning()) {
|
||||
stepExecution.setStatus(BatchStatus.STOPPED);
|
||||
stepExecution.setEndTime(new Date());
|
||||
jobRepository.update(stepExecution);
|
||||
}
|
||||
}
|
||||
jobRepository.update(jobExecution);
|
||||
log.info("Updated job execution status: {}", jobExecution.getJobId());
|
||||
}
|
||||
}
|
||||
log.info("Stopped running jobs.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.example.springbatch.application.job.param;
|
||||
|
||||
import lombok.Getter;
|
||||
import org.springframework.batch.core.configuration.annotation.JobScope;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@JobScope
|
||||
@Getter
|
||||
public class CreateArticleJobParam {
|
||||
|
||||
private String name;
|
||||
|
||||
@Value("#{jobParameters[name]}")
|
||||
private void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.example.springbatch.application.job.param;
|
||||
|
||||
import lombok.Getter;
|
||||
import org.springframework.batch.core.configuration.annotation.JobScope;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@JobScope
|
||||
@Getter
|
||||
public class CreateOddBoardJobParam {
|
||||
|
||||
private long minId;
|
||||
private long maxId;
|
||||
|
||||
@Value("#{jobParameters[minId]}")
|
||||
private void setMinId(long minId) {
|
||||
this.minId = minId;
|
||||
}
|
||||
|
||||
@Value("#{jobParameters[maxId]}")
|
||||
private void setMaxId(long maxId) {
|
||||
this.maxId = maxId;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.example.springbatch.application.model;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class ArticleModel {
|
||||
|
||||
private String title;
|
||||
|
||||
private String content;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.example.springbatch.application.model;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class BoardModel {
|
||||
|
||||
private String title;
|
||||
|
||||
private String content;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.example.springbatch.application.scheduler;
|
||||
|
||||
import com.example.springbatch.application.service.JobService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@RequiredArgsConstructor
|
||||
public class JobScheduler {
|
||||
|
||||
private final JobService jobService;
|
||||
|
||||
// @Scheduled(fixedDelay = 5000)
|
||||
public void runCreateArticleJob() throws Exception{
|
||||
jobService.runCreateArticleJob();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.example.springbatch.application.service;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.batch.core.Job;
|
||||
import org.springframework.batch.core.JobParametersBuilder;
|
||||
import org.springframework.batch.core.launch.JobLauncher;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class JobService {
|
||||
|
||||
private final Job createArticleJob;
|
||||
private final JobLauncher jobLauncher;
|
||||
|
||||
public void runCreateArticleJob() throws Exception{
|
||||
jobLauncher.run(createArticleJob, new JobParametersBuilder()
|
||||
.addDate("date", new Date())
|
||||
.toJobParameters());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.example.springbatch.domain.entity;
|
||||
|
||||
import lombok.*;
|
||||
import org.hibernate.annotations.CreationTimestamp;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Entity
|
||||
@Getter
|
||||
@Setter
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Article {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private long id;
|
||||
|
||||
private String title;
|
||||
private String content;
|
||||
|
||||
private LocalDateTime createdAt;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.example.springbatch.domain.entity;
|
||||
|
||||
import lombok.*;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Entity
|
||||
@Getter
|
||||
@Setter
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Board {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private long id;
|
||||
|
||||
private String title;
|
||||
private String content;
|
||||
|
||||
private LocalDateTime createdAt;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.example.springbatch.domain.entity;
|
||||
|
||||
import lombok.*;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Entity
|
||||
@Getter
|
||||
@Setter
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class OddBoard {
|
||||
|
||||
@Id
|
||||
private long id;
|
||||
|
||||
private String title;
|
||||
private String content;
|
||||
|
||||
private LocalDateTime createdAt;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.example.springbatch.domain.repository;
|
||||
|
||||
import com.example.springbatch.domain.entity.Article;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface ArticleRepository extends JpaRepository<Article, Long> {
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.example.springbatch.domain.repository;
|
||||
|
||||
import com.example.springbatch.domain.entity.Board;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface BoardRepository extends JpaRepository<Board, Long> {
|
||||
|
||||
Page<Board> findAllByIdBetween(long minId, long maxId, Pageable pageable);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.example.springbatch.domain.repository;
|
||||
|
||||
import com.example.springbatch.domain.entity.Board;
|
||||
import com.example.springbatch.domain.entity.OddBoard;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface OddBoardRepository extends JpaRepository<OddBoard, Long> {
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.example.springbatch.infrastructure;
|
||||
|
||||
import org.springframework.batch.core.configuration.annotation.DefaultBatchConfigurer;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
|
||||
@Configuration
|
||||
public class BatchDataManagerConfig extends DefaultBatchConfigurer {
|
||||
|
||||
private final PlatformTransactionManager batchTransactionManager;
|
||||
|
||||
public BatchDataManagerConfig(
|
||||
@Qualifier("batchTransactionManager") PlatformTransactionManager batchTransactionManager) {
|
||||
this.batchTransactionManager = batchTransactionManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlatformTransactionManager getTransactionManager() {
|
||||
return this.batchTransactionManager;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.example.springbatch.infrastructure;
|
||||
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
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 BatchDataSourceConfig {
|
||||
|
||||
@Bean
|
||||
@Primary
|
||||
@ConfigurationProperties(prefix = "spring.datasource.hikari.batch")
|
||||
public DataSource batchDataSource() {
|
||||
return DataSourceBuilder.create().type(HikariDataSource.class).build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PlatformTransactionManager batchTransactionManager (
|
||||
@Qualifier("batchDataSource") DataSource batchDataSource) {
|
||||
return new DataSourceTransactionManager(batchDataSource);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.example.springbatch.infrastructure;
|
||||
|
||||
import org.hibernate.cfg.AvailableSettings;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
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.LocalContainerEntityManagerFactoryBean;
|
||||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.util.Properties;
|
||||
|
||||
@Configuration
|
||||
@EnableTransactionManagement
|
||||
@EnableJpaRepositories(
|
||||
basePackages = {
|
||||
"com.example.springbatch.domain.repository"
|
||||
},
|
||||
entityManagerFactoryRef = "demoEntityManagerFactory",
|
||||
transactionManagerRef = "demoTransactionManager"
|
||||
)
|
||||
public class DemoDataManagerConfig {
|
||||
|
||||
private final DataSource dataSource;
|
||||
|
||||
public DemoDataManagerConfig(
|
||||
@Qualifier("demoDataSource") DataSource dataSource) {
|
||||
this.dataSource = dataSource;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public LocalContainerEntityManagerFactoryBean demoEntityManagerFactory() {
|
||||
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
||||
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
||||
vendorAdapter.setGenerateDdl(true);
|
||||
|
||||
em.setDataSource(this.dataSource);
|
||||
em.setPersistenceUnitName("demoEntityManager");
|
||||
em.setPackagesToScan(
|
||||
"com.example.springbatch.domain.entity"
|
||||
);
|
||||
em.setJpaVendorAdapter(vendorAdapter);
|
||||
em.setJpaProperties(demoJpaProperties());
|
||||
em.afterPropertiesSet();
|
||||
return em;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public JdbcTemplate demoJdbcTemplate(@Qualifier("demoDataSource") DataSource dataSource) {
|
||||
return new JdbcTemplate(dataSource);
|
||||
}
|
||||
|
||||
private Properties demoJpaProperties() {
|
||||
Properties properties = new Properties();
|
||||
properties.setProperty(AvailableSettings.HBM2DDL_AUTO, "update");
|
||||
properties.setProperty(AvailableSettings.SHOW_SQL, "true");
|
||||
properties.setProperty(AvailableSettings.ALLOW_UPDATE_OUTSIDE_TRANSACTION, "true");
|
||||
return properties;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.example.springbatch.infrastructure;
|
||||
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
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.orm.jpa.JpaTransactionManager;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
@Configuration
|
||||
public class DemoDataSourceConfig {
|
||||
|
||||
@Bean
|
||||
@ConfigurationProperties(prefix = "spring.datasource.hikari.demo")
|
||||
public DataSource demoDataSource() {
|
||||
return DataSourceBuilder.create().type(HikariDataSource.class).build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PlatformTransactionManager demoTransactionManager(
|
||||
@Qualifier("demoEntityManagerFactory") EntityManagerFactory demoEntityManagerFactory) {
|
||||
JpaTransactionManager demoTransactionManager = new JpaTransactionManager();
|
||||
demoTransactionManager.setEntityManagerFactory(demoEntityManagerFactory);
|
||||
return demoTransactionManager;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.example.springbatch.utils;
|
||||
|
||||
import lombok.SneakyThrows;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class FileUtils {
|
||||
|
||||
@SneakyThrows
|
||||
public static Stream<File> stream(Path path) {
|
||||
return Files.list(path).map(Path::toFile);
|
||||
}
|
||||
}
|
||||
50
spring-batch/practice/src/main/resources/Articles.csv
Normal file
50
spring-batch/practice/src/main/resources/Articles.csv
Normal file
@@ -0,0 +1,50 @@
|
||||
제목제목, 내용내용내용내용내용내용내용내용내용내용내용내용
|
||||
제목제목, 내용내용내용내용내용내용내용내용내용내용내용내용
|
||||
제목제목, 내용내용내용내용내용내용내용내용내용내용내용내용
|
||||
제목제목, 내용내용내용내용내용내용내용내용내용내용내용내용
|
||||
제목제목, 내용내용내용내용내용내용내용내용내용내용내용내용
|
||||
제목제목, 내용내용내용내용내용내용내용내용내용내용내용내용
|
||||
제목제목, 내용내용내용내용내용내용내용내용내용내용내용내용
|
||||
제목제목, 내용내용내용내용내용내용내용내용내용내용내용내용
|
||||
제목제목, 내용내용내용내용내용내용내용내용내용내용내용내용
|
||||
제목제목, 내용내용내용내용내용내용내용내용내용내용내용내용
|
||||
제목제목, 내용내용내용내용내용내용내용내용내용내용내용내용
|
||||
제목제목, 내용내용내용내용내용내용내용내용내용내용내용내용
|
||||
제목제목, 내용내용내용내용내용내용내용내용내용내용내용내용
|
||||
제목제목, 내용내용내용내용내용내용내용내용내용내용내용내용
|
||||
제목제목, 내용내용내용내용내용내용내용내용내용내용내용내용
|
||||
제목제목, 내용내용내용내용내용내용내용내용내용내용내용내용
|
||||
제목제목, 내용내용내용내용내용내용내용내용내용내용내용내용
|
||||
제목제목, 내용내용내용내용내용내용내용내용내용내용내용내용
|
||||
제목제목, 내용내용내용내용내용내용내용내용내용내용내용내용
|
||||
제목제목, 내용내용내용내용내용내용내용내용내용내용내용내용
|
||||
제목제목, 내용내용내용내용내용내용내용내용내용내용내용내용
|
||||
제목제목, 내용내용내용내용내용내용내용내용내용내용내용내용
|
||||
제목제목, 내용내용내용내용내용내용내용내용내용내용내용내용
|
||||
제목제목, 내용내용내용내용내용내용내용내용내용내용내용내용
|
||||
제목제목, 내용내용내용내용내용내용내용내용내용내용내용내용
|
||||
제목제목, 내용내용내용내용내용내용내용내용내용내용내용내용
|
||||
제목제목, 내용내용내용내용내용내용내용내용내용내용내용내용
|
||||
제목제목, 내용내용내용내용내용내용내용내용내용내용내용내용
|
||||
제목제목, 내용내용내용내용내용내용내용내용내용내용내용내용
|
||||
제목제목, 내용내용내용내용내용내용내용내용내용내용내용내용
|
||||
제목제목, 내용내용내용내용내용내용내용내용내용내용내용내용
|
||||
제목제목, 내용내용내용내용내용내용내용내용내용내용내용내용
|
||||
제목제목, 내용내용내용내용내용내용내용내용내용내용내용내용
|
||||
제목제목, 내용내용내용내용내용내용내용내용내용내용내용내용
|
||||
제목제목, 내용내용내용내용내용내용내용내용내용내용내용내용
|
||||
제목제목, 내용내용내용내용내용내용내용내용내용내용내용내용
|
||||
제목제목, 내용내용내용내용내용내용내용내용내용내용내용내용
|
||||
제목제목, 내용내용내용내용내용내용내용내용내용내용내용내용
|
||||
제목제목, 내용내용내용내용내용내용내용내용내용내용내용내용
|
||||
제목제목, 내용내용내용내용내용내용내용내용내용내용내용내용
|
||||
제목제목, 내용내용내용내용내용내용내용내용내용내용내용내용
|
||||
제목제목, 내용내용내용내용내용내용내용내용내용내용내용내용
|
||||
제목제목, 내용내용내용내용내용내용내용내용내용내용내용내용
|
||||
제목제목, 내용내용내용내용내용내용내용내용내용내용내용내용
|
||||
제목제목, 내용내용내용내용내용내용내용내용내용내용내용내용
|
||||
제목제목, 내용내용내용내용내용내용내용내용내용내용내용내용
|
||||
제목제목, 내용내용내용내용내용내용내용내용내용내용내용내용
|
||||
제목제목, 내용내용내용내용내용내용내용내용내용내용내용내용
|
||||
제목제목, 내용내용내용내용내용내용내용내용내용내용내용내용
|
||||
제목제목, 내용내용내용내용내용내용내용내용내용내용내용내용
|
||||
|
@@ -0,0 +1,32 @@
|
||||
spring:
|
||||
datasource:
|
||||
# 데이터베이스 분리 방법 (2)
|
||||
hikari:
|
||||
batch:
|
||||
jdbc-url: jdbc:mysql://localhost:3306/batch
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
username: batch
|
||||
password: 1234
|
||||
demo:
|
||||
jdbc-url: jdbc:mysql://localhost:3306/demo?rewriteBatchedStatements=true
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
username: batch
|
||||
password: 1234
|
||||
|
||||
batch:
|
||||
initialize-schema: always
|
||||
job:
|
||||
names: ${job.name:NONE}
|
||||
# # 데이터베이스 분리 방법 (1)
|
||||
# schema: schema-mysql.sql
|
||||
# table-prefix: batch.BATCH_
|
||||
jpa:
|
||||
generate-ddl: true
|
||||
hibernate:
|
||||
ddl-auto: update
|
||||
naming:
|
||||
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
|
||||
show-sql: true
|
||||
logging:
|
||||
level:
|
||||
root: info
|
||||
3
spring-batch/practice/src/main/resources/application.yml
Normal file
3
spring-batch/practice/src/main/resources/application.yml
Normal file
@@ -0,0 +1,3 @@
|
||||
spring:
|
||||
profiles:
|
||||
active: local
|
||||
@@ -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