1. Multi Job

 - 한개의 Application 에서 여러개의 Job을 만들기


2. Job을 2개 만든다.

 1) 이전 글에서 만든 Job

/**
* Basic Configuration
*
* @author akageun
*/
@Configuration
public class BasicConfiguration {
private static final Logger LOG = LoggerFactory.getLogger(BasicConfiguration.class);

private static final String BASIC_JOB_NM = "BASIC_JOB";
private static final String BASIC_STEP_NM = "BASIC_TASKLET_STEP";

@Autowired
public JobBuilderFactory jobBuilderFactory;

@Autowired
public StepBuilderFactory stepBuilderFactory;

/**
* Basic Job Configuration
*
* @return
*/
@Bean(name = BASIC_JOB_NM)
public Job basicJob() {
//@formatter:off
return jobBuilderFactory
.get(BASIC_JOB_NM)
.incrementer(new RunIdIncrementer())
.start(basicTaskletStep())
.build();
//@formatter:on
}

/**
* Basic Step Configuration
*
* @return
*/
@Bean(name = BASIC_STEP_NM)
public Step basicTaskletStep() {
//@formatter:off
return stepBuilderFactory
.get(BASIC_STEP_NM)
.tasklet((stepContribution, chunkContext) -> {
LOG.info("Tasklet Run!!");

return RepeatStatus.FINISHED;
})
.build();
//@formatter:on
}
}


 2) 새로 만든 Multi Job


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.launch.support.RunIdIncrementer;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* MULTI BASIC Configuration
*
* @author akageun
*/
@Configuration
public class MultiBasicTestConfiguration {
private static final Logger LOG = LoggerFactory.getLogger(MultiBasicTestConfiguration.class);

private static final String MULTI_BASIC_JOB_NM = "MULTI_BASIC_JOB";
private static final String MULTI_BASIC_STEP_NM = "MULTI_BASIC_TASKLET_STEP";

@Autowired
public JobBuilderFactory jobBuilderFactory;

@Autowired
public StepBuilderFactory stepBuilderFactory;

/**
* MULTI_BASIC Job Configuration
*
* @return
*/
@Bean(name = MULTI_BASIC_JOB_NM)
public Job multiBasicJob() {
//@formatter:off
return jobBuilderFactory
.get(MULTI_BASIC_JOB_NM)
.incrementer(new RunIdIncrementer())
.start(multiBasicTaskletStep())
.build();
//@formatter:on
}

/**
* MULTI_BASIC Step Configuration
*
* @return
*/
@Bean(name = MULTI_BASIC_STEP_NM)
public Step multiBasicTaskletStep() {
//@formatter:off
return stepBuilderFactory
.get(MULTI_BASIC_STEP_NM)
.tasklet((stepContribution, chunkContext) -> {
LOG.info("Tasklet Multi Job Run!!");

return RepeatStatus.FINISHED;
})
.build();
//@formatter:on
}
}


3. 실행

 1) application.yml 내 옵션 설정

spring:
batch:
job:
enabled: true

 - 위와 같이 설정할 경우 해당 App내에 모든 Job이 실행된다.


 2) application 내 옵션

spring:
batch:
job:
enabled: false
names: BASIC_JOB,MULTI_BASIC_JOB

- 위와같이 설정할 경우 names에 들어있는 Job들만 실행된다. (Job들은 콤마(,)로 구분된다)


 3) jar파일 실행

java -jar ./BATCH_STUDY.jar --spring.batch.job.names=BASIC_JOB,MULTI_BASIC_JOB


 4) intellij 실행


4. 결과


5. 전체소스보기 : 바로가기


1. embedded-redis

 - 개발 버전, 프로토 타이핑 등에서 간단하게 사용하기 편함.


2. pom.xml

 - https://github.com/kstyrc/embedded-redis 로 사용할 예정

<!-- embedded-redis -->
<dependency>
<groupId>com.github.kstyrc</groupId>
<artifactId>embedded-redis</artifactId>
<version>0.6</version>
</dependency>


3. Redis Configuration

 - application.yml

spring:
redis:
host: localhost
port: 6379
database: 0

 - Start 및 stop 설정 필요

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import redis.embedded.RedisServer;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.io.IOException;

/**
* Embedded Redis Configration
*
* @author akageun
*/
@Component
public class EmbeddedRedisConfiguration {

@Value("${spring.redis.port}")
private int redisPort;

private RedisServer redisServer;

@PostConstruct
public void startRedis() throws IOException {
redisServer = new RedisServer(redisPort);
redisServer.start(); //Redis 시작
}

@PreDestroy
public void stopRedis() {
redisServer.stop(); //Redis 종료
}

}


 - Template 설정


import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
* Redis Configuration
*
* @author akageun
*/
@Configuration
public class RedisConfig {

@Value("${spring.redis.host}")
private String redisHost;

@Value("${spring.redis.port}")
private int redisPort;

@Value("${spring.redis.database}")
private int redisDatabase;

/**
* Factory
*
* @return
*/
@Bean
public JedisConnectionFactory jedisConnectionFactory() {
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
jedisConnectionFactory.setHostName(redisHost);
jedisConnectionFactory.setPort(redisPort);
jedisConnectionFactory.setDatabase(redisDatabase);
jedisConnectionFactory.setUsePool(true);
return jedisConnectionFactory;
}

/**
* redis Template
*
* @return
*/
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
redisTemplate.setConnectionFactory(jedisConnectionFactory());

return redisTemplate;
}
}


4. 사용

 - RedisTemplate 사용

@Autowired
private RedisTemplate redisTemplate;


- Code

redisTemplate.opsForValue().set("test", "test1111");
redisTemplate.opsForValue().get("test");


정말 간단하다.

+ Recent posts