SpringBoot2学习笔记(十一)任务

一.异步任务

使用异步任务只需要两个注解
@EnableAsync //开启异步任务
@Async //标志此方法为异步方法

步骤:
1.在程序主入口加上@EnableAsync注解,用于开启异步任务
2.在需要异步操作的方法上加上@Async注解即可

例子:
SpringBoot11TackApplication 主入口

@EnableAsync //开启异步任务
@SpringBootApplication
public class SpringBoot11TackApplication {

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

AsyncService

@Service
public class AsyncService {

    @Async //标志此方法为异步方法
    public void asyncPrint() throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            System.out.println(i);
            Thread.sleep(1000);
        }
    }
}

HelloController

@Controller
public class HelloController {

    @Autowired
    AsyncService asyncService;

    @RequestMapping("/hello")
    @ResponseBody
    public String hello() throws InterruptedException {
        asyncService.asyncPrint();
        System.out.println("Hello Word Async");
        return "Hello Word Async";
    }
}

结果:
这里写图片描述

二.定时任务

步骤:
1.在主入口上添加@EnableScheduling 注解,开启定时任务
2.在需要定时执行的方法上添加@Scheduled注解

例子:
SpringBoot11TackApplication

@EnableScheduling //开启定时任务
@SpringBootApplication
public class SpringBoot11TackApplication {

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

ScheduledService

@Service
public class ScheduledService {

    @Scheduled(cron = "0/4 * * * * SUN-SAT")
    public void helloSchedul(){
        System.out.println("Hello Spring Boot ...");
    }

}

启动主程序测试
结果:
这里写图片描述

corn相关表达式写法:

这里写图片描述

三.邮件任务

例子:

application.properties 配置文件配置基本的邮件发送信息

spring.mail.username=1336037686@qq.com
spring.mail.password= #使用授权码
spring.mail.host=smtp.qq.com
# 设置SSL
spring.mail.properties.mail.smtp.ssl.enable=true

使用:

    @Autowired
    JavaMailSender javaMailSender;

    /**
     * 发送简单邮件
     */
    @Test
    public void testMail(){
        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject("Hello SpringBoot");
        message.setText("Hello from 1336037686");

        message.setTo("[email protected]");
        message.setFrom("[email protected]");

        javaMailSender.send(message);
    }

    /**
     * 发送复杂邮件
     * @throws MessagingException
     */
    @Test
    public void testMail02() throws MessagingException {

        MimeMessage mimeMessage = javaMailSender.createMimeMessage();       
        //用于填充MimeMessage MimeMessageHelper(MimeMessage mimeMessage, boolean multipart)
        //第二个参数代表是否传输文件
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage,true);

        //设置邮件头
        mimeMessageHelper.setSubject("Hello SpringBoot Mail 02");
        //设置邮件内容,第二个参数表示是否是HTML
        mimeMessageHelper.setText("<b style='color:red'>Hello World</b>",true);
        //传输文件
        mimeMessageHelper.addAttachment("1.png",new File("C:\\Users\\LGX\\Desktop\\1.png"));
        //设置收件人
        mimeMessageHelper.setTo("[email protected]");
        //设置发件人
        mimeMessageHelper.setFrom("[email protected]"); 

        //发送邮件
        javaMailSender.send(mimeMessage);
    }

MimeMessageHelper的构造函数
MimeMessageHelper(MimeMessage mimeMessage)
MimeMessageHelper(MimeMessage mimeMessage, @Nullable String encoding)
MimeMessageHelper(MimeMessage mimeMessage, boolean multipart)
MimeMessageHelper(MimeMessage mimeMessage, boolean multipart, @Nullable String encoding)
MimeMessageHelper(MimeMessage mimeMessage, int multipartMode)
MimeMessageHelper(MimeMessage mimeMessage, int multipartMode, @Nullable String encoding)

MimeMessageHelper设置邮件内容
setText(String text)
setText(String text, boolean html)
setText(String plainText, String htmlText)

猜你喜欢

转载自blog.csdn.net/l1336037686/article/details/81145136
今日推荐