관리 메뉴

나만의공간

SpringBatch 강의 #1 본문

IT/Spring Batch

SpringBatch 강의 #1

밥알이 2023. 7. 21. 15:16

대용량에 데이타 처리시나, 규칙적인 데이타를 처리하기 위해 여러 배치 프로그램을 사용합니다.
요즘 대부분 기업에서는 SpringBatch를 이용하여 배치처리를 많이 하고있고, 젠킨스와 결합하면 매우 좋은 배치 시스템이 됩니다.
SpringBatch를 처음부터 구성하는 방법을 정리하면서 학습을 해보고자 합니다.

SpringBatch F/W 초기 구성 다운로드

SpringBatch 기본구성은 Spring initializr 사이트에서 원하는 개발 스펙을 선택해서 다운로드 받으면 됩니다.
저희는 아래 구성 Spec을 기본으로 하여 구축 하고자 합니다.

SpringBatch 기본구성
언어: Java 17
빌드방식: Gradle 8.X
SpringBoot: 3.1.1

아래 사이트 방문 후 필요한 기능 선택 후 다운로드 받습니다.
https://start.spring.io/

오른쪽 Dependencies 항목들을 선택한 후 다운로드 받아야 SpringBoot에 Dependency가 같이 설정이 됩니다.

SpringBatch F/W 구성파일 설치

위 Spring initializr에서 다운로드 받은 파일을 원하는 폴더에서 압축을 해제 합니다.
압축을 해제 하면 아래 이미지 처럼 기본 파일들이 나타납니다.

압축을 해제하 mysqlBatch 폴더를 개발Tool을 이용하여 해당 폴더를 불러오면 기본 SpringBatch 구성이 완료 됩니다.

※ 개발툴에서 빌드시 아래와 같은 오류가 나오는 분들은 설정 정보 변경이 필요합니다.
SpringBoot 3.1.1을 사용할려면 Java17 이상에 버전이 필요한데 Gradle의 컴파일 설정이 Java 17하위 버전으로 설정되어 있으면 나타납니다.

A problem occurred configuring root project 'mysqlBatch'.
> Could not resolve all files for configuration ':classpath'.
   > Could not resolve org.springframework.boot:spring-boot-gradle-plugin:3.1.1.
     Required by:
         project : > org.springframework.boot:org.springframework.boot.gradle.plugin:3.1.1
      > No matching variant of org.springframework.boot:spring-boot-gradle-plugin:3.1.1 was found. The consumer was configured to find a library for use during runtime, compatible with Java 8, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '8.1.1' but:
          - Variant 'apiElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.1.1 declares a library, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares a component for use during compile-time, compatible with Java 17 and the consumer needed a component for use during runtime, compatible with Java 8
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.1.1')
          - Variant 'javadocElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.1.1 declares a component for use during runtime, and its dependencies declared externally:
              - Incompatible because this component declares documentation and the consumer needed a library
              - Other compatible attributes:
                  - Doesn't say anything about its target Java version (required compatibility with Java 8)
                  - Doesn't say anything about its elements (required them packaged as a jar)
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.1.1')
          - Variant 'mavenOptionalApiElements' capability org.springframework.boot:spring-boot-gradle-plugin-maven-optional:3.1.1 declares a library, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares a component for use during compile-time, compatible with Java 17 and the consumer needed a component for use during runtime, compatible with Java 8
              - Other compatible attribute:

인텔리J 환경설정에서 Java 버전을 17로 변경하면 오류가 해결됩니다.
Build,Execution,Deployment => Build Tools => Gradle 화면으로 이동
Gradle JVM 에 보이는 JAVA Version을 17로 선택합니다.

위와 같이 설정 후 Gradle Build를 다시 실행하면, 의존성 관련 파일 다운로드가 정상적으로 됩니다.

SpringBatch Application 실행하기

Gradle Build 완료 후 Batch Application을 이제 실행하기만 하면 되는데 바로 오류가 발생합니다.
오류 내용을 보면 DB연결정보가 없어 발생을 합니다.
MySql을 설치 했고, MySql과에 연결정보를 application.yml 파일을 신규로 만들어 설정 하면 아래 오류는 없어집니다.

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
	If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
	If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).


Process finished with exit code 1

application.yml 설정정보

아래 정보를 기준으로 application.yml 파일 정보를 설정하게 되면, SpringBatch도 실행이 됩니다.

spring:
  profiles:
    active: local
  batch:
    job:
      names: ${job.name:helloJob}

---
spring:
  config:
    activate:
      on-profile: local
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/test
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: test
  jpa:
    show-sql: true
    generate-ddl: false
#    hibernate:
#      ddl-auto: none
#    properties:
#      hibernate:
#        show_sql: true
  batch:
    jdbc:
      initialize-schema: ALWAYS

---
spring:
  config:
    activate:
      on-profile: test
  jpa:
    database: h2

DB연결도 정상적으로 되고, SpringBatch에서 기본적으로 사용하게 되는 테이블도 자동으로 만들어져 있습니다.
다음 강의 에서는 SpringBatch가 만든 기본 테이블에 대한 설명 후 간단한 배치 프로그램을 만들 겠습니다.

Comments