타이밍 작업은 일반적으로 중간 및 대기업 수준의 프로젝트에 있습니다. 서버 및 데이터베이스에 대한 압력을 줄이기 위해 시간 기간은 종종 특정 비즈니스 논리를 완료하는 데 사용됩니다. 가장 흔한 것은 금융 서비스 시스템이 콜백을 밀어 붙인다는 것입니다. 일반적으로 결제 시스템 주문은 성공적인 콜백을받지 못하고 콘텐츠를 반환 할 때 계속 콜백을합니다. 이러한 종류의 콜백은 일반적으로 시간이 정해진 작업에 의해 완료됩니다. 보고서 세대도 있습니다. 우리는 일반적으로 고객 방문 수가 너무 적을 때이 작업을 완료합니다. 이는 종종 이른 아침에 있습니다. 이 시점에서는 시간이 정해진 작업을 사용하여 논리를 완료 할 수 있습니다. SpringBoot에는 타이밍 작업이 내장되어 있으며 타이밍을 사용할 수 있도록 하나의 주석 만 있으면됩니다.
개발에서 타이밍 작업이 일반적인 기능입니다. Spring Boot에서 타이밍 작업을 개발하는 것은 실제로 매우 간단합니다. 특정 코드는 다음과 같습니다.
1. 종속성 패키지 pom.xml을 구성하십시오
기본 Maven 저장소는 종종 액세스 할 수 없으므로 Alibaba Cloud의 Maven 저장소 이미지가 여기에서 사용됩니다.
<? xml version = "1.0"encoding = "utf-8"?> <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.xsd"> <modeversion> 4.0.0 </modelversion> <groupid> com.example> <artifactid> </artifactid> <버전> 0.0.1-snapShot </version> <packaging> jar </packaging> <name> 스프링-부트 예약식 </name> <description> 스프링 부츠를위한 데모 프로젝트 </descript> <!-Alibaba Cloud Maven Repository-> <repositories> <repository> <id> <이름> Aliyun Nexus </nexus. <Url> http://maven.aliyun.com/nexus/content/groups/public/ </url> <releases> <enabled> true </enabled> </repository> </repositories> <pluginrepository> <public </id> <name> aliyun nexus. <Url> http://maven.aliyun.com/nexus/content/groups/public/ </url> <releases> <enabled> true </enabled> </릴리스> <snapshots> <enabled> false </enabled> </snapinReitority> </pluginReatority> </pluginpository> <groupid> org.springframework.boot </groupid> <artifactid> 스프링-부트-스타터-팔렌트 </artifactid> <버전> 1.4.5.5.5.5.5.5.5.5.5.5.5.5.5.5.5.5.5.5.5.5.5.5.5.5.5.5. <project.reporting.outputencoding> utf-8 </project.reporting.outputencoding> <java.version> 1.8 </java.version> </properties> <pectionals> <gupelency> <groupid> org.springframwork.boot </groupid> <artifactid> spring-we-starterb </artifactid> <pectionency> <groupId> org.projectLombok </groupId> <artifactid> lombok </artifactid> <pectional> true </옵션> </dependency> <groupidency> <groupid> org.springframework.boot </groupid> <artifactid> spring-boot-starter-test> </artifactid> test> test> </dependencies> <build> <flugins> <flugin> <groupId> org.springframework.boot </groupid> <artifactid> spring-boot-maven-plugin </artifactid> </plugin> </plugins> </build> </project>
2. 작업 시나리오를 사용자 정의하십시오
정시 작업이 구현되어 고정 사이클, 고정 사이클 지연 간격 및 공식화 된 시점과 같은 시나리오를 제공합니다. 주석에 @scheduled 주석을 사용하십시오.
exampletimer.java
패키지 com.example; import java.text.simpledateformat; import java.util.date; import org.springframework.scheduling.annotation.scheduled; import org.springframework.stereotyp.component; @componentpublic class emprictpletimer {simpledateformat = new simpledateformat ( "hh : mm : ss"); @scheduled (fixedrate = 10000) public void timerrate () {system.out.println (dateformat.format (new date ());} // @scheduled (initialdelay = 100) (고정 된 타이어)를 실행 한 후 처음으로 실행되는 시간이 지연됩니다. {system.out.println ( "init :"+ dateformat.format (new date ());} // @scheduled (cron = "50 16 20 * *?") public void timercron () {system.out.println ( "현재 시간 :"+ dateformat (new date ());3. 응용 프로그램을 시작하십시오
프로그램을 시작하려면 @enablescheduling 주석을 추가해야합니다.
SpringBootscheduledApplication.java
패키지 com.example; import org.springframework.boot.springApplication; import org.springframework.boot.autoconfigure.springbootApplication; import org.springframework.scheduling.annotation.enablescheduling;@springbootapplication@enablic vublic springschedatic void main (string [] args) {springApplication.run (springbootscheduledApplication.class, args);}}4. 출력 결과
20 : 16 : 27Init : 20 : 16 : 28init : 20 : 16 : 30Init : 20 : 16 : 32init : 20 : 16 : 34init : 20 : 16 : 3620 : 16 : 37init : 20 : 16 : 38init : 20 : 16 : 40init : 20 : 16 : 42init : 20 : 16 : 44init : 20 : 16 : 4620 : 16 : 47 init : 20 : 4620 : 20 : 16 : 50Init : 20 : 16 : 50Init : 20 : 16 : 52Init : 20:16:54
요약
위의 내용은 예약을 통해 예정된 작업 코드를 구현하는 SpringBoot에 대한이 기사의 모든 내용이며, 모든 사람에게 도움이되기를 바랍니다. 관심있는 친구들은이 사이트를 계속 참조 할 수 있습니다.
스프링 부트 크로스 도메인 설정 인스턴스에 대한 자세한 설명
스프링 부츠를 빨리 알게됩니다
Springboot to Spring의 장점에 대한 간단한 토론
단점이 있으면 메시지를 남겨 두십시오. 이 사이트를 지원해 주신 친구들에게 감사드립니다!