(cao,没学异步)

在5秒钟后执行,这就属于异步的一种

Thread.sleep(3000);

Service层,这层写个异步执行测试

@Service
public class AsyncService {

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

controller层测试,调用service层

@RestController
public class AsyncController {
    @Autowired
    AsyncService asyncService;
    @RequestMapping("/a")
    public String test(){
        asyncService.hello();
        return "ok";
    }
}

主类

@SpringBootApplication
public class AsynchronousApplication {

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

}

当打开/a连接时候,浏览器反应了5秒钟后打开页面。

在springboot中有关于异步的注解

@Service
public class AsyncService {

    @Async
    public void hello(){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
@RestController
public class AsyncController {
    @Autowired
    AsyncService asyncService;
    @RequestMapping("/a")
    public String test(){
        asyncService.hello();
        return "ok";
    }
}
@EnableAsync
@SpringBootApplication
public class AsynchronousApplication {

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

}

先显示页面,service方法在5秒钟执行。

邮件发送

导入所需依赖

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

代码

@SpringBootTest
class AsynchronousApplicationTests {

    @Autowired
    JavaMailSenderImpl mailSender;
    @Test
    void contextLoads() {
        //Simple
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setSubject("HELLO WORLD");
        mailMessage.setText("这是一封测试邮件");
        mailMessage.setTo("1226776310@qq.com");
        mailMessage.setFrom("1226776310@qq.com");
        mailSender.send(mailMessage);
    }

    @Test
    void contextLoads2() throws MessagingException {
        //complex
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        //组装
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);

        helper.setSubject("HELLO WORLD");
        helper.setText("<p style='color:red'>这是一封测试邮件</p>",true);
        //附件
        helper.addAttachment("Header.png",new File("/Users/quanwu/Documents/动漫/Header.png"));

        helper.setTo("1159336305@qq.com");
        helper.setFrom("1226776310@qq.com");
        mailSender.send(mimeMessage);
    }

    public void sendMail(Boolean html,String subject,String text,String attachment,String filePath,String setTo,String setFrom) throws MessagingException {
        //complex
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        //组装
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);

        helper.setSubject(subject);
        helper.setText(text,html);
        //附件
        helper.addAttachment(attachment,new File(filePath));

        helper.setTo(setTo);
        helper.setFrom(setFrom);
        mailSender.send(mimeMessage);
    }

}

TaskScheduler 任务调度者
TaskExecutor 任务执行者

@EnableScheduling    //开启定时功能的注解
@Scheduled     //什么时候执行

Cron表达式

首先开启定时任务的注解

@EnableAsync//开启异步
@EnableScheduling//开启定时功能的注解
@SpringBootApplication
public class AsynchronousApplication {

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

}

测试

它会在每一天的10:9打印hello world

关于cron表达式,自行百度

@Service
public class ScheduledService {
    //秒 分 时 日 月 星期
    @Scheduled(cron = "0 9 10 * * 0-7")
    public void test(){
        System.out.println("hello world");
    }
}