java框架之SpringBoot(14)-任務

使用 maven 建立 SpringBoot 項目,引入 Web 場景啓動器。html

異步任務

一、編寫異步服務類,註冊到 IoC 容器:java

package zze.springboot.task.service;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
public class AsyncService {
    @Async // 標識方法將會異步執行
    public void hello() {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("hello");
    }
}
zze.springboot.task.service.AsyncService

二、使用註解開啓異步任務支持:web

package zze.springboot.task;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;

@EnableAsync // 開啓異步任務支持
@SpringBootApplication
public class TaskApplication {

    public static void main(String[] args) {
        SpringApplication.run(TaskApplication.class, args);
    }

}
zze.springboot.task.TaskApplication

三、編寫控制器測試:spring

package zze.springboot.task.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import zze.springboot.task.service.AsyncService;

@RestController
public class AsyncController {

    @Autowired
    private AsyncService asyncService;

    @GetMapping("/hello")
    public String testAsyncHello(){
        asyncService.hello();
        return "執行完畢";
        /*
        訪問 localhost:8080/hello
            a、當即返回了 「執行完畢」,並無由於 asyncService.hello() 方法中的線程 sleep 等待 3 秒。
            b、3 秒後控制檯輸出 hello
        OK,異步任務執行成功
         */
    }
}
zze.springboot.task.controller.AsyncController

定時任務

一、編寫定時任務類,使用 Cron 表達式指定任務執行時機,註冊到 IoC 容器:springboot

package zze.springboot.task.service;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class ScheduleService {
    @Scheduled(cron = "*/3 * * * * ?")  // 編寫 cron 表達式,每 3 秒執行一次
    public void test(){
        Date now = new Date();
        SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
        System.out.println(timeFormat.format(now));
    }
}
zze.springboot.task.service.ScheduleService

二、使用註解開啓定時任務支持:app

package zze.springboot.task;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableScheduling // 開啓定時任務支持
@SpringBootApplication
public class TaskApplication {

    public static void main(String[] args) {
        SpringApplication.run(TaskApplication.class, args);
    }

}
zze.springboot.task.TaskApplication

三、啓動項目測試:框架

hello at 21:32:57
hello at 21:33:00
hello at 21:33:03
hello at 21:33:06
/*
每間隔 3 秒控制檯會執行 hello 方法
*/
test

SpringBoot 中的定時任務 Cron 表達式與任務調度框架 Quartz 的 Cron 表達式規則類似,可參考【Quartz 表達式】。異步

但它們有一處區別是:async

  • SpringBoot 任務的 Cron 表達式中星期部分 1-6 爲週一到週六,0 和 7都爲週日。
  • Quartz 框架的 Cron 表達式中星期部分 1-7 爲週日到週六。

郵件任務

郵件的自動配置類爲 org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration 。maven

下面以 QQ 郵箱給 GMail 郵箱發送郵件爲例。

一、引入 Mail 場景啓動器:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

二、獲取 qq 郵箱受權碼,登陸進入 qq 郵箱,進入設置,選擇帳戶,選擇生成受權碼:

三、郵箱相關配置:

spring.mail.username=632404164@qq.com
// 使用生成的受權碼
spring.mail.password=nffutccjfabdbchc
spring.mail.host=smtp.qq.com

# qq 郵箱須要開啓 ssl
spring.mail.properties.mail.smtp.sll.enable=true
application.properties

四、測試:

package zze.springboot.mail;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.test.context.junit4.SpringRunner;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@RunWith(SpringRunner.class)
@SpringBootTest
public class MailApplicationTests {

    @Autowired
    JavaMailSender javaMailSender;

    // 發送普通文本內容
    @Test
    public void test1() {
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        // 設置郵件標題
        mailMessage.setSubject("標題");
        // 設置郵件內容
        mailMessage.setText("hello");

        mailMessage.setFrom("632404164@qq.com");
        mailMessage.setTo("zhangzhongen326@gmail.com");
        javaMailSender.send(mailMessage);

                 


    }

    // 發送 html 內容帶附件
    @Test
    public void test2() throws MessagingException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
        // 設置標題
        mimeMessageHelper.setSubject("標題");
        // 設置內容
        mimeMessageHelper.setText("<font color='red'>hello</font>",true);

        // 設置附件,可設置多個
        mimeMessageHelper.addAttachment("1.jpg", new File("C:\\Users\\Administrator\\Desktop\\1mail.png"));

        mimeMessageHelper.setFrom("632404164@qq.com");
        mimeMessageHelper.setTo("zhangzhongen326@gmail.com");
        javaMailSender.send(mimeMessage);

                 


    }

}
test
相關文章
相關標籤/搜索