SpringBoot-三大任务

188 阅读2分钟

异步任务

1.构建

image-20210523095359475

创建一个异步处理类AsynService

package com.gip.service;

import org.springframework.stereotype.Service;

@Service
public class AsynService {

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

        System.out.println("数据正在处理");
    }
}

使用 Thread.sleep(3000);模拟处理

然后在controller中调用这个类实列方法hello()

AsynController

package com.gip.Controller;

import com.gip.service.AsynService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AsynController {
    @Autowired
    AsynService asynService;

    @GetMapping("/hello")
    public String hell0() {
        asynService.hello();
        return "hello";
    }
}

启动项目看看效果

image-20210523095638857

浏览器等待响应需要3秒时间

才返回hello字符串

2.开启异步注解

在异步类hello方法上门添加@Async

image-20210523095955654

然后在SpringbootTestApplication主启动类上开启异步的支持

image-20210523100112658

一般要开启什么功能都是@enable+....功能

重启项目运行,发现以及不会卡顿,异步处理完成

image-20210523100256255

邮件任务

1.添加依赖

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

双击shift,找邮件配置的源码

image-20210523111649798

这里就很清楚了

image-20210523111704006

可以配置如下属性

image-20210523112003842

2.开始配置

首先去你的邮箱中开启这个功能

image-20210523112149689

然后在application.properties配置

image-20210523114232096

3.测试

在测试类中写一个简单邮件

package com.gip;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.scheduling.annotation.Async;

@SpringBootTest
class SpringbootTestApplicationTests {

    @Autowired
    JavaMailSenderImpl mailSender;
    @Test
    void contextLoads() {
        SimpleMailMessage simpleMailMessage=new SimpleMailMessage();
        simpleMailMessage.setSubject("这是我的测试发送邮件");
        simpleMailMessage.setText("这里是正文内容");
        simpleMailMessage.setFrom("798484107@qq.com");
        simpleMailMessage.setTo("798484107@qq.com");
        mailSender.send(simpleMailMessage);
    }

}

首先注入这个实现类的实例image-20210523114343562

然后定义简单消息

SimpleMailMessage simpleMailMessage=new SimpleMailMessage();
simpleMailMessage.setSubject("这是我的测试发送邮件");
simpleMailMessage.setText("这里是正文内容");
//从哪发,发送者
simpleMailMessage.setFrom("798484107@qq.com");
//发给谁,接收者
simpleMailMessage.setTo("798484107@qq.com");
mailSender.send(simpleMailMessage);

mailSender.send方法发送

测试结果如下:

image-20210523114441107

新创建一个复杂邮件

@Test
void contextLoads2() throws MessagingException {
    //创建复杂邮件
    MimeMessage mimeMessage = mailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
    helper.setSubject("这是我的测试发送邮件222");
    helper.setText("<p style='color:red'>正文</p", true);
    helper.setFrom("798484107@qq.com");
    helper.setTo("798484107@qq.com");

    //附件
   helper.addAttachment("01.jpg",new File("F:\\桌面壁纸\\01.jpg"));


    mailSender.send(mimeMessage);
}

image-20210523130847935

注意:idea方法内快捷键:Ctrl+P`参数提示

定时任务

在启动类中加入注解@EnableScheduling

创建ScheduledService类

package com.gip.service;

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

@Service
public class ScheduledService {
//    Cron表达式是一个字符串,字符串以5或6个空格隔开
//    秒(0~59)
//    分钟(0~59)
//    小时(0~23)
//    天(日月)(0~31,但是你需要考虑你月的天数)
//    月(0~11)
//    天(星期)(1~7 1=SUN 或 SUN,MON,TUE,WED,THU,FRI,SAT)
//    年份(1970-2099)
    @Scheduled(cron = "* * * * * ?")
    public void hello(){
        System.out.println("定时任务被开启");
    }
}

注意:Spring Task不支持到年份,所以只能最多cron表达式6位

?只能用在日期和星期上,也可以直接用*代替了

timer 功能不强大,而且运行错误后,生命周期结束,以后都不可执行。所以一般不会用java timer,会用 Quartz

image-20210523144227857