Spring Boot任務相關知識點

一、任務分類

  1. 異步任務
  2. 定時任務
  3. 郵件任務

1、異步任務

 使用場景:

在執行業務方法的時候,爲了返回的相應速度,就會用到異步處理,啓用多線程的方式去跑業務中的任務

配置異步任務的步驟:

第一步:需要在springboot的啓動類中開啓異步的註解 ------@EnableAsync

第二步:需要在我們異步處理業務的方法上添加上@Async註解

實例:

/************spring boot的啓動入口類*************/
@EnableAsync //開啓異步註解
@SpringBootApplication
public class MainApplication {

    public static void main(String[] arg){

        SpringApplication.run(MainApplication.class, arg);
    }
}

/************需要異步調用的業務方法*************/
@Service
public class AsynService {

    //@Async:這是spring這是一個異步方法
    @Async
    public void hello() {

        try{
            Thread.sleep(3000);
        }catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("處理數據中。。。");
    }
}

/************實際業務調用方法*************/
@RestController
public class HelloController {

    @Autowired
    AsynService asynService;

    @RequestMapping("/hello")
    public String index() {

        asynService.hello();
        return "Greetings from Spring Boot!";
    }
}

2、定時任務

使用場景:

項目開發中經常需要執行一些定時任務,比如需要在每天凌晨的時候,分析一次前一天的日誌信息。

配置定時任務的步驟:

第一步:需要在springboot啓動了上加上@EnableScheduling //開啓基於註解的定時任務。

第二步:在需要跑定時任務的方式上加@Scheduled(cron = "0 * * * * MON-FRI") //代表週一到週五每整一分鐘執行一次。

實例:

/*****spring boot的啓動類*****/
@EnableAsync //開啓異步註解
@EnableScheduling //開啓基於註解的定時任務
@SpringBootApplication
public class MainApplication {

    public static void main(String[] arg){

        SpringApplication.run(MainApplication.class, arg);
    }
}

/************需要跑定時任務的業務方法****************/
@Service
public class ScheduledService {

    /**
     * second(秒), minute(分), hour(時), day of month(日), month(月), and day of week(周).
     *
     * 0 * * * * MON-FRI
     */
    @Scheduled(cron = "0 * * * * MON-FRI") //代表週一到週五每整一分鐘執行一次
    public void hellsay() {

        System.out.println("say hello");
    }
}

cron表達式規則:

3、郵件任務

第一步:導入jar包

<!--郵件發送jar-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

第二步:發送者郵件配置

#郵件發送相關配置信息
spring.mail.username=
spring.mail.password=
spring.mail.properties.mail.smtp.ssl.enable=true

第三步:編寫業務代碼

@Service
public class MailSendService {

    @Autowired
    JavaMailSenderImpl mailSender;

    /**
     * 簡單郵件發送
     */
    public void test01() {

        SimpleMailMessage message = new SimpleMailMessage();

        //郵件設置
        message.setSubject("通知標題");
        message.setText("內容");

        message.setTo(""); //接受者
        message.setFrom(""); //發送者是說

        mailSender.send(message);
    }

    /**
     * 複雜郵件發送
     */
    public void test02() throws Exception{

        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);

        //郵件設置
        mimeMessageHelper.setSubject("通知標題");
        mimeMessageHelper.setText("內容"); //這個地方可以設置html標籤一些類容

        mimeMessageHelper.setTo(""); //接受者
        mimeMessageHelper.setFrom(""); //發送者是說

       //mimeMessageHelper.addAttachment(); //上傳文件等
        mailSender.send(mimeMessage);
    }

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章