Tasks in Springboot (asynchronous tasks-Scheduled Tasks-mail tasks)

Source: Internet
Author: User

1.pom file

<?XML version= "1.0" encoding= "UTF-8"?><Projectxmlns= "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>Com.zy</groupId>    <Artifactid>Spring-boot-task-demo</Artifactid>    <version>0.0.1-snapshot</version>    <Packaging>Jar</Packaging>    <name>Spring-boot-task-demo</name>    <Description>Demo Project for Spring Boot</Description>    <Parent>        <groupId>Org.springframework.boot</groupId>        <Artifactid>Spring-boot-starter-parent</Artifactid>        <version>2.0.4.RELEASE</version>        <RelativePath/> <!--Lookup parent from repository -    </Parent>    <Properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>        <java.version>1.8</java.version>    </Properties>    <Dependencies>        <Dependency>            <groupId>Org.springframework.boot</groupId>            <Artifactid>Spring-boot-starter-web</Artifactid>        </Dependency>        <Dependency>            <groupId>Org.springframework.boot</groupId>            <Artifactid>Spring-boot-starter-test</Artifactid>            <Scope>Test</Scope>        </Dependency>    </Dependencies>    <Build>        <Plugins>            <plugin>                <groupId>Org.springframework.boot</groupId>                <Artifactid>Spring-boot-maven-plugin</Artifactid>            </plugin>        </Plugins>    </Build></Project>

2. Startup class

 Package Com.zy; Import org.springframework.boot.SpringApplication; Import org.springframework.boot.autoconfigure.SpringBootApplication; Import Org.springframework.scheduling.annotation.EnableAsync; @SpringBootApplication // supports asynchronous threads, which require an asynchronous invocation of the service layer method with @async annotations, which are combined to work @EnableAsync
To turn on annotation-based scheduled tasks
@EnableScheduling
 Public class  publicstaticvoid  main (string[] args) {Springapplication.run ( Springboottaskdemoapplication. class , args); } }

3.controller Test class

 PackageCom.zy.controller;ImportCom.zy.service.AsyncServiceImpl;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RestController; @RequestMapping ("/task/") @RestController Public classAsynccontroller {@AutowiredPrivateAsyncserviceimpl Asyncservice; @RequestMapping ("/async")     PublicObject Async (String name) {returnname; }}

4.service Layer

4.1 Asynchronous task Implementation classes

 PackageCom.zy.service;ImportOrg.springframework.scheduling.annotation.Async;ImportOrg.springframework.stereotype.Service; @Service ("Asyncservice") Public classAsyncserviceimpl {@Async Publicstring Async (string name) {Try{Thread.Sleep (5000); } Catch(interruptedexception e) {e.printstacktrace (); }        return"Good Morning:" +name; }}

4.2 Timing Task Implementation class

 PackageCom.zy.service;Importorg.springframework.scheduling.annotation.Scheduled;ImportOrg.springframework.stereotype.Service; @Service Public classScheduledserviceimpl {/*** Second (sec), Minute (min), hour (time), day of month, month (months), Days of Week (weeks). * 0 * * * * Mon-fri * "0 0/5 14,18 * *" 14 o'clock daily, and 18 o'clock full, every 5 minutes * "0 15 10? * 1-6 "Every month from Monday to Saturday 10:15 run once *" 0 0 2? * 6L "Last Saturday 2 o'clock in the morning every month * *" 0 0 2 LW *? "The last working day of each month is performed 2 o'clock in the morning once *" 0 0 2-4? * 1#1 "The first month of Monday from 2 o'clock in the morning to 4, each hour is executed once;*/    //@Scheduled (cron = "0 * * * * * MON-SAT")//@Scheduled (cron = "0,1,2,3,4 * * * * * MON-SAT")//@Scheduled (cron = "0-4 * * * * * MON-SAT")@Scheduled (Cron= "0/50 * * * * MON-SAT")//executes once every 50 seconds     Public voidSayschedule () {System.out.println ("Sayschedule"); }}

Tasks in Springboot (asynchronous tasks-Scheduled Tasks-mail tasks)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.