๐Ÿ‘จ‍๐Ÿ’ป๊ฐœ๋ฐœ/โ˜•Java

[Spring Property / Spring Scheduler] ์Šคํ”„๋ง ํ”„๋กœํผํ‹ฐ ์ ์šฉ / ์Šค์ผ€์ค„๋Ÿฌ / Spring Scheduler

kimtongting ์ž…๋‹ˆ๋‹ค 2022. 1. 20. 12:23

 

 

์„œ์‹

ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•  ๋•Œ, ๊ฐ€๋ณ€์˜ ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๋Š” ์ •๋ณด๋Š” ๋ชจ๋‘ properties์— ๋„ฃ๋Š”๋‹ค.

 

ํ•„์ž์˜ ๊ฒฝ์šฐ ํ”„๋กœ์ ํŠธ ๋ฐฐํฌ ์ˆœ์„œ์ธ 'DEV' -> 'STAGE' -> 'PROD' ์—์„œ

'STAGE' -> 'DEV' ๋กœ ๋‚ด๋ฆฌ๊ฑฐ๋‚˜, 'PROD' ๋‹จ๊ณ„์—์„œ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ์— ์ฃผ๋กœ ์“ฐ๊ณ  ์žˆ๋‹ค.

 

config.flag.type ์— 'D', 'S', 'P' ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ config.flag.type์ด 'D' ์ผ ๊ฒฝ์šฐ,

DEV ์„œ๋ฒ„๋กœ ๋“ฑ๋กํ•œ config๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๊ณ  ๋ญ ๊ทธ๋Ÿฐ์‹์ด๋‹ค.

 

์„œ๋ฒ„๋ฅผ ์ค‘๋‹จํ•˜์ง€ ์•Š๊ณ  ์ด ํŒŒ์ผ๋งŒ์„ ์ˆ˜์ •ํ•จ์œผ๋กœ์จ ํ”„๋กœ์ ํŠธ์˜ ์„ค์ • ๊ฐ’๋“ค์„ ํ•ธ๋“ค๋ง ํ•œ๋‹ค๋Š” ์ ์—์„œ

๊ต‰์žฅํžˆ ์œ ์šฉํ•˜๋‹ค.

 

 

 

ํ…Œ์ŠคํŠธ ์‹œ๋‚˜๋ฆฌ์˜ค

Spring ๊ตฌ์กฐ๋กœ ํ”„๋กœ์ ํŠธ๋ฅผ ๊ตฌ์„ฑํ–ˆ๋‹ค๋ฉด ๊ต‰์žฅํžˆ ์‹ฌํ”Œํ•˜๋‹ค.

 

์ด๋ฏธ classpath:resources/application.properties ํŒŒ์ผ์ด ์ƒ์„ฑ๋˜์—ˆ์„ ํ…๋ฐ, 

์ž„์˜๋กœ ๊ฐ’์„ ๋„ฃ์–ด์„œ ์˜ˆ๋ฅผ ๋“ค์–ด๋ดค๋‹ค.

 

์‹œ๋‚˜๋ฆฌ์˜ค๋Š” ์Šคํ”„๋ง ์Šค์ผ€์ค„๋Ÿฌ๋ฅผ ์ด์šฉํ•ด ํŠน์ • ์‹œ๊ฐ„๋Œ€๋งˆ๋‹ค properties์˜ ๊ฐ’์„ ํ™•์ธ ํ›„,

๊ทธ ๊ฐ’์— ๋”ฐ๋ผ ์ถฉ์ „์ด ๋˜๋Š” ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•ด ๋ณผ ์˜ˆ์ •์ด๋‹ค.

 

application.properties >charge.use.flag=n

 

๋จผ์ € properties๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„  Bean ์ž‘์—…์ด ํ•„์š”ํ•˜๋‹ค.

properties๋ฅผ java์—์„œ ์ฝ์„ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์€ ๊ต‰์žฅํžˆ ๋งŽ์€๋ฐ ๊ทธ ์ค‘ ์ œ์ผ ์‰ฌ์šด ๋ฐฉ๋ฒ•์œผ๋กœ ์ง„ํ–‰ํ•  ์˜ˆ์ •์ด๋‹ค.

 

@Value

 

์ด ๊ฐ’์œผ๋กœ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ•„์ˆ˜์ ์œผ๋กœ @Bean ์— ๋“ฑ๋ก์„ ํ•ด์•ผํ•œ๋‹ค.

ํ•„์ž๋Š” servlet-context.xml ๋กœ ๊ตฌํ˜„ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด ๊ณณ์— bean์„ ๋“ฑ๋กํ–ˆ๋‹ค.

    <?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:beans="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
            http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd">


...
    <!-- ์Šค์ผ€์ฅด๋Ÿฌ ์„ค์ • scheduler-->
    <task:scheduler id="jobScheduler" pool-size="10"/>
    <task:annotation-driven scheduler="jobScheduler"/>
    
    <!-- Property-->
    <beans:bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <beans:property name="location" value="classpath:application.properties"/>
        <beans:property name="fileEncoding" value="UTF-8" />
        <!-- ์—ฌ๋Ÿฌ ๊ฐœ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ, -->
        <!--<beans:property name="locations">
            <beans:list>
                <beans:value>classpath:application.properties</beans:value>
                <beans:value>classpath:useState.properties</beans:value>
            </beans:list>
        </beans:property>-->
    </beans:bean>

 

์‹œ๋‚˜๋ฆฌ์˜ค ๊ตฌํ˜„

	@Value("${charge.use.flag}")
	private String chargeFlag;
    
    @Scheduled(cron="0/5 * * * * ?") // ๊ฐœ๋ฐœ (5์ดˆ๋งˆ๋‹ค)
	@Transactional(propagation = Propagation.REQUIRED)
	public void workCarryOverBatch() {
		log.info("์ถฉ์ „๊ตฌ๋ถ„::{}",chargeFlag);

		if(chargeFlag.equalsIgnoreCase("Y")) {
			log.info("{} ์ผ ๊ธˆ์•ก ์ถฉ์ „ ์ž‘์—… ์ง„ํ–‰ ์ค‘.(Charging JOB Start)",Util.getCurrentDateTime());
			/*// ํ•ด๋‹น์ผ ์ถฉ์ „ ๋Œ€์ƒ ์œ ๋ฌด ํŒ๋‹จ.
			Optional<List<AutoBatch>> optional = this.todayBatchList(dateConverter.dateFormat());
			//์ถฉ์ „ ์ง„ํ–‰.
			this.chargeVM(optional);
			log.info("{}์ผ ๊ธˆ์•ก ์ถฉ์ „ ์ž‘์—… ์ง„ํ–‰ ์™„๋ฃŒ.(Charging JOB End)",dateConverter.dateFormat());*/
		}else{
			log.info("์ถฉ์ „๊ตฌ๋ถ„::{} ๋กœ ์ธํ•œ ์ค‘์ง€",chargeFlag);
		}
	}

 

๊ฒฐ๊ณผ

 

 

๋ฐ˜์‘ํ˜•