■ Spring에서 지원하는 Scheduling 작업예제를 Eclipse에서 실행했다. 작업은 Scheculing Tasks를 참조했다. JDK버전은 1.8, Maven버전은 3.3.9이다. Eclipse에서 Maven Project를 아래와 같이 생성한다. 여기서 Packaging는 jar를 선택한다.

■ pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>net.iotinfra</groupId> <artifactId>pilot.scheduling</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> </parent> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
■ ScheduledTasks.java 실제 스케줄 작업을 할 클래스 파일을 생성한다. 작업내용은 5초마다 현재시간을 출력해주는 작업이다.
Scheduled 어노테이션은 특정 메소드가 실행되는시기를 정의한다. 참고 :이 예제에서는 각 호출의 시작 시간에서 측정 된 메서드 호출 간격을 지정하는 fixedRate를 사용한다. fixedDelay와 같은 다른 옵션이 있다. 이 옵션은 작업 완료시 측정 된 호출 간격을 지정한다. 더 정교한 작업 스케줄링을 위해 @Scheduled (cron = “..”) 표현식을 사용할 수도 있다.
package hello; 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())); } }
■ Application.java의 main() 메소드는 Spring Boot의 SpringApplication.run () 메소드를 사용하여 애플리케이션을 시작한다.
@SpringBootApplication은 다음을 모두 추가하는 편리한 어노테이션이다.
- @Configuration은 클래스를 애플리케이션 컨텍스트의 Bean 정의 소스로 태그한다.
- @EnableAutoConfiguration은 Spring Boot에게 클래스 패스 설정, 다른 bean 및 다양한 속성 설정을 기반으로 bean 추가를 시작하도록 지시합니다.
- 일반적으로 @EnableWebMvc를 Spring MVC app에 추가하지만, Spring Boot는 classpath에서 spring-webmvc를 볼 때 자동으로 추가한다. 이것은 응용 프로그램에 웹 응용 프로그램으로 플래그를 지정하고 DispatcherServlet 설정과 같은 주요 동작을 활성화한다.
- @ComponentScan은 Spring에게 hello 패키지의 다른 구성 요소, 구성 및 서비스를 찾아서 컨트롤러를 찾을 수 있게 한다.
- @EnableScheduling은 백그라운드 태스크 실행 프로그램이 작성되도록한다. 이것이 없으면 아무 것도 예정되지 않습니다.
package hello; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication @EnableScheduling public class Application { public static void main(String[] args) { SpringApplication.run(Application.class); } }
■ 완성된 프로그램 구조 – Maven Goal을 clean install로 주고 빌드를 하면 아래와 같이 pilot.scheduling-0.0.1-SNAPSHOT.jar파일이 생성된다.

■ Eclipse 메뉴 > Run > Run Configurations…를 선택한다.

■ Main class에 Application클래스 파일을 입력 후 Run 버튼을 클릭해서 실행한다.

■ Eclipse Console에서 프로그램이 실행되고 있다. 5초마다 현재 시간을 출력하고 있다.

- JSP 파일 캐시 방지 2023년 1월 9일
- SpringBoot Port 변경 2022년 12월 27일
- jenkins, Tomcat return http status error: 401 에러 2022년 10월 12일
- jenkins 빌드할때 Unsupported class file major version 63 에러 발생 2022년 10월 12일
- Python mysql like 검색 2022년 8월 22일
- agGrid cell style 지정 2022년 3월 12일
- AngularJS Error: error:tplrt Invalid Template Root 2019년 12월 10일
- Java 콤마로 구분된 코드들을 List로 변환 2019년 12월 6일
- SuppressWarnings, unused , 경고무시 어노테이션 2019년 8월 13일
- Spring 트랜잭션 관리 2019년 6월 20일
- Spring RESTful 웹 서비스 사용하기 2019년 6월 19일
- Spring RESTful 웹 서비스 2019년 6월 19일
- Spring Scheduling Task 예제 2019년 6월 18일
- AngularJS fromJson, toJson 사용법 2019년 5월 15일
- 워드프레스 댓글 관리(댓글 삭제 방법) 2019년 4월 8일
- Java 메일전송 예제 2019년 4월 3일
- Java에서 properties파일 읽기 2019년 4월 3일
- 이클립스 에디터 동일 파일 화면 분할 2019년 3월 27일
- Java 배열을 리스트로, 리스트를 배열로 변환 2019년 3월 27일
- weblogic.jdbc.wrapper.clob_oracle_sql_clob cannot be cast to oracle.sql.clob 2019년 3월 21일
- Java keytool 사용법 2019년 3월 14일
- Tomcat 시작시 ServletContextListener를 통한 mySQL 접속 2018년 5월 8일
- XML파일 정보를 읽어 Java VO에 저장 2018년 5월 8일
- Eclipse에서 Maven으로 Tomcat 시작 2018년 5월 8일
- spring mybatis mysql 연결 예제 2018년 5월 4일
- myBatis 조건문 예제 2018년 5월 3일
- apache commons StringUtils 예제 2018년 5월 3일
- apache commons DateFormatUtils 예제 2018년 5월 3일
- Spring Path Parameter 예제 2018년 2월 13일
- 자바 래퍼 클래스(Java Wrapper Class)? 2018년 2월 13일
- java string 변수 switch, junit test 2018년 2월 12일
- AngularJS $http, spring TEXT response 예제 2018년 1월 17일
- jdom2를 이용한 spring xml response(응답) 2017년 12월 5일
- jquery xml 요청, spring xml 리턴 2017년 12월 5일
- spring controller에서 get, post 동시 허용 방법 2017년 12월 5일
- No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK? 2017년 12월 4일
- mybatis, oracle procedure(오라클 커서)를 통한 데이터 조회 2017년 11월 6일
- HTTP 406 Not Acceptable in Spring, Jackson, Json 2017년 11월 6일
- 단정문 assertEquals와 assertSame의 차이점 2017년 4월 12일
- java continue, break 키워드 2017년 3월 25일
- maven filtering – 메이븐 필터링 2016년 10월 24일
- maven profiles – 개발/운영 환경별 빌드 2016년 10월 24일
- maven pom.xml에서 로그 출력하기 2016년 5월 30일
- spring service junit test 2016년 5월 25일
- ServletContextLister를 구현한 웹 애플리케이션 시작 시 작업 2016년 5월 2일
- mybatis mapper junit test 2016년 5월 2일
- org.apache.ibatis.binding.BindingException: Invalid bound statement (not found) 2016년 5월 2일
- JUnit Controller Test java.lang.NoClassDefFoundError: javax/servlet/SessionCookieConfig 2016년 3월 22일
- Maven skipTests – Maven JUnit 테스트 안하기 2016년 3월 18일
- maven compile option -Xlint:deprecation 2016년 3월 17일
- Maven encoding 2016년 3월 17일
- TIBCO ActiveSpace Eclipse Maven 2016년 3월 16일
- java eclipse static import 2016년 3월 15일
- junit 단정문 – Assertions 2016년 3월 14일
- junit & spring-test maven dependency 2016년 3월 14일
- Jetty Maven Plugin 2016년 3월 9일
- java.lang.UnsupportedClassVersionError: org/eclipse/jetty/maven/plugin/JettyRunMojo : Unsupported major.minor version 52.0 2016년 3월 9일
- Web Application MessageResource 2016년 3월 8일
- java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered? 2016년 3월 8일
- Java String, 자바 문자열 2016년 3월 3일
- org.mariadb.jdbc.internal.common.QueryException : No database selected 2016년 2월 19일
- java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet 2016년 2월 19일
- jquery spring – checkbox로 선택된 배열 주고 받기 2015년 8월 15일
- jquery spring ajax call 2015년 8월 15일
- spring json return 2015년 8월 15일
- spring exception – SimpleMappingExceptionResolver 2015년 8월 8일
- sqlSessionFactory빈 생성 에러 2015년 1월 1일
- redundant cast to ‘XXX’ 2015년 1월 1일
- 이클립스 콘솔버퍼 제한 2015년 1월 1일
- Map is a raw type 2015년 1월 1일
- servlet-api duplicate 2015년 1월 1일
- eclipse maven 디버그(debug) 모드로 빌드 2014년 11월 21일
- No Spring WebApplicationInitializer types detected on classpath 에러 2014년 11월 21일
- logback 일별 로깅 – logback daily logging 2014년 11월 16일
- logback 파일에 로깅하기 – logback file logging 2014년 11월 15일
- Logback 기본 설정 2014년 11월 15일
- logback encoder pattern 2014년 11월 11일
- org.apache.ibatis.binding.BindingException 디버깅 2014년 11월 11일
- maven-war-plugin webXml, 개발 환경별 web.xml 관리 2014년 11월 10일
- 명령행 모드 메이븐 프로젝트 생성하기 2014년 9월 17일
- eclipse maven tomcat 자동 재시작 2014년 9월 13일