博客
关于我
SpringBoot-Web-异步、定时、邮件任务
阅读量:272 次
发布时间:2019-03-01

本文共 2315 字,大约阅读时间需要 7 分钟。

Spring Boot 异步任务与定时任务实践指南

异步任务

在Spring Boot应用中,通过@Async注解可以实现方法级的异步执行。要开启异步任务功能,需要在主程序上使用@EnableAsync注解。

常用corn表达式示例

  • 每2秒执行一次:0 * * * * ?
  • 每2分钟执行一次:0 0 * * * ?
  • 每月1日的凌晨2点执行:0 2 1 * * ?
  • 周一到周五每天上午10:15执行:0 15 10 ? * MON-FRI
  • 2002-2006年的每月最后一个星期五上午10:15执行:0 15 10 ? 6L 2002-2006
  • 每天上午10:00, 下午14:00, 下午16:00执行:0 0 10,14,16 * * ?
  • 工作日内每半小时执行一次:0 0/30 9-17 * * ?
  • 每周三中午12点执行:0 12 ? * WED
  • 每天中午12点执行:0 12 * * ?
  • 每天上午10:15执行:0 15 10 * * ?
  • 定时任务

    通过@Scheduled注解可以在方法上定义定时任务。只需配置好corn表达式,开启注解后,启动主程序即可运行。

    示例配置

    @EnableScheduling@SpringBootApplicationpublic class Springboot05Application {    public static void main(String[] args) {        SpringApplication.run(Springboot05Application.class, args);    }}

    常用corn表达式

  • 每2秒执行一次:0 * * * * ?
  • 每2分钟执行一次:0 0 * * * ?
  • 每月1日的凌晨2点执行:0 2 1 * * ?
  • 周一到周五每天上午10:15执行:0 15 10 ? * MON-FRI
  • 2002-2006年的每月最后一个星期五上午10:15执行:0 15 10 ? 6L 2002-2006
  • 每天上午10:00, 下午14:00, 下午16:00执行:0 0 10,14,16 * * ?
  • 工作日内每半小时执行一次:0 0/30 9-17 * * ?
  • 每周三中午12点执行:0 12 ? * WED
  • 每天中午12点执行:0 12 * * ?
  • 每天上午10:15执行:0 15 10 * * ?
  • 邮件任务

    简单邮件发送

  • 导入依赖:
  • org.springframework.boot
    spring-boot-starter-mail
    1. 配置邮件 Properties 文件:
    2. spring.mail.username=2500813866@qq.comspring.mail.password=kxhfocyyrnmuecif#qq.comspring.mail.host=smtp.qq.comspring.mail.properties.mail.smtp.ssl.enable=true
      1. 注入邮箱发送实现类:
      2. @Componentpublic class MyMail {    @Resource    private JavaMailSenderImpl mailSender;    @Test    public void sendMail() {        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();        simpleMailMessage.setSubject("你好");        simpleMailMessage.setText("这是一封邮件的内容");        simpleMailMessage.setTo("2440545145@qq.com");        simpleMailMessage.setFrom("2500813866@qq.com");        mailSender.send(simpleMailMessage);    }}

        复杂邮件发送

        基于简单邮件的实现,可以通过MimeMessage实现复杂邮件的发送。例如:

        @Testpublic void sendMail() throws MessagingException {    MimeMessage mimeMessage = mailSender.createMimeMessage();    MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);        helper.setSubject("这是一封复杂的邮件");    helper.setText("

        你猜我是什么颜色

        ", true); helper.addAttachment("1.jpg", new File("C:\\Users\\Desktop\\clipboard.png")); helper.setTo("2440545145@qq.com"); helper.setFrom("2500813866@qq.com"); mailSender.send(mimeMessage);}

        注意事项

        • 邮件发送依赖配置正确,特别是QQ邮箱需要开启加密验证。
        • 复杂邮件可将逻辑封装到工具类中,提高代码维护性。

    转载地址:http://gffa.baihongyu.com/

    你可能感兴趣的文章
    Pinterest 大规模缓存集群的架构剖析
    查看>>
    pintos project (2) Project 1 Thread -Mission 1 Code
    查看>>
    PinYin4j库的使用
    查看>>
    PIP
    查看>>
    pip install goose-extractor // SyntaxError: Missing parentheses in call to 'print'
    查看>>
    pip install mysqlclient报错
    查看>>
    pip install 出现报asciii码错误的解决
    查看>>
    pip throws TypeError: parse() got an unexpected keyword argument ‘transport_encoding‘ 在尝试安装新软件包时
    查看>>
    pip 下载慢
    查看>>
    pip 升级报错AttributeError: ‘NoneType’ object has no attribute ‘bytes’
    查看>>
    pip 安装opencv-python卡死
    查看>>
    pip 安装出现异常
    查看>>
    Pip 安装失败:需要 SSL
    查看>>
    Pip 安装挂起
    查看>>
    pip 或 pip3 为 Python 3 安装包?
    查看>>
    pip 文件损坏导致 pip无法使用 报错 ImportError: cannot import name 'main' from 'pip._int
    查看>>
    pip 无法从 requirements.txt 安装软件包
    查看>>
    pip/pip3更换国内源
    查看>>
    pip3 install PyQt5 --user 失败
    查看>>
    pip3命令全解析:Python3包管理工具的详细使用指南
    查看>>