티스토리 뷰
spring boot TaskExecutor 예제
build.gradle
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 32 33 | buildscript { ext { springBootVersion = '1.4.3.RELEASE' } repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") } } apply plugin: 'java' apply plugin: 'idea' apply plugin: 'org.springframework.boot' jar { baseName = 'demo' version = '0.0.1-SNAPSHOT' } sourceCompatibility = 1.8 repositories { mavenCentral() } dependencies { compile('org.springframework.boot:spring-boot-starter') testCompile('org.springframework.boot:spring-boot-starter-test') } |
DemoApplication.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | package com.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableScheduling; @Configuration @ComponentScan @EnableScheduling public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } | cs |
ScheduledTasks.java
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 | package com.example.task; /** * Created by kyeongrok.kim on 2017-01-24. */ import java.text.SimpleDateFormat; import java.util.Date; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class ScheduledTasks { private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class); private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); @Scheduled(fixedRate = 5000) public void reportCurrentTime() { log.info("The time is now {}", dateFormat.format(new Date())); } } | cs |
이렇게 하면 5초에 한번씩 실행됨.
ClassPathXmlApplicationContext 사용하는 법
applicationContext.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"> <bean id="scheduledTasks" class="com.example.task.ScheduledTasks"/> <task:scheduled-tasks scheduler="myScheduler"> <task:scheduled ref="scheduledTasks" fixed-rate="2000" method="reportCurrentTime"/> </task:scheduled-tasks> <task:scheduler id="myScheduler" pool-size="10"/> </beans> | cs |
DemoApplication.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.scheduling.annotation.EnableScheduling; @Configuration @ComponentScan @EnableScheduling public class DemoApplication { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); //SpringApplication.run(DemoApplication.class, args); } } | cs |
end.
728x90
'Spring > Spring Boot(스프링 부트)' 카테고리의 다른 글
Spring 자주 쓰는 Annotation 정리 (2) | 2017.02.14 |
---|---|
SpringBoot profile local로 설정 (0) | 2017.02.03 |
Spring Boot gradle로 빌드하기 - @SpringBootApplication annotation 이란? (1) | 2017.01.13 |
Intellij에서 spring프로젝트 gradle로 생성하기 - 제7편 test method 코딩, 잘 실행 되는지 확인 (0) | 2016.04.30 |
Intellij에서 spring프로젝트 gradle로 생성하기 - 제6편 add method 코딩, test class생성 (0) | 2016.04.30 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- docker container tissue
- docker container whale
- 싱가폴
- docker container
- 도커티슈케이스
- docker container tissue box
- 도커각티슈케이스
- 개발자
- 도커각티슈박스
- docker container case
- shellscript
- 2017 티스토리 결산
- 도커컨테이너
- vim
- 이직
- Sh
- 도커티슈박스
- Linux
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함