티스토리 뷰

개요

개발을 하다보면 당연하지만 properties 파일을 사용합니다.

간단하게 스프링 부트에서 사용하는 방식을 살펴보겠습니다.

마찬가지로 이전 글과 이어집니다.

2016/09/07 - [개발/JAVA] - 스프링 부트(Spring boot)로 scheduling 개발하기


프로퍼티 생성

new-Source Folder를 선택하여 src/main/resources폴더를 생성합니다.

그리고 생성된 폴더에 application.properties만들고 다음과 같이 작성합니다.

spring.task.fixedDelay=2000

spring.task.name=donnert



Timer 수정

package net.donnert.spring.boot;

import java.util.concurrent.atomic.AtomicInteger;
import javax.annotation.PostConstruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

@Component
public class Timer {
	Logger logger = LoggerFactory.getLogger(this.getClass());
	private AtomicInteger loopCounter = new AtomicInteger();
	
	@Autowired
	private StopWatch watch;
	
	@Value("${spring.task.name}")
	private String taskNamePrefix;
	
	@PostConstruct
	public void init() {
		watch.start();
	}

	@Scheduled(fixedDelayString = "${spring.task.fixedDelay}")
	public void tick() throws InterruptedException{
		watch.stop();
		logger.info(watch.prettyPrint());
		String taskName = taskNamePrefix + "-" + String.valueOf(loopCounter.getAndIncrement());
		watch.start(taskName);
	}

	@Bean
	public StopWatch watch() {
		return new StopWatch();
	}
}


마무리

Application.java를 돌려보면 아래처럼 설정에서 읽어돠 2초마다 수행이 된 것을 볼 수 있습니다.

application.properties는 스프링 부트에서 기본으로 읽어오는 프로퍼티입니다.

-----------------------------------------

ms     %     Task name

-----------------------------------------

00157  002%  

02000  020%  donnert-0

02002  020%  donnert-1

02002  020%  donnert-2

02001  020%  donnert-3

02000  020%  donnert-4


댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/03   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함