博客
关于我
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/

    你可能感兴趣的文章
    nodejs配置express服务器,运行自动打开浏览器
    查看>>
    NodeMCU教程 http请求获取Json中文乱码解决方案
    查看>>
    Nodemon 深入解析与使用
    查看>>
    NodeSession:高效且灵活的Node.js会话管理工具
    查看>>
    node~ http缓存
    查看>>
    node不是内部命令时配置node环境变量
    查看>>
    node中fs模块之文件操作
    查看>>
    Node中同步与异步的方式读取文件
    查看>>
    Node中的Http模块和Url模块的使用
    查看>>
    Node中自启动工具supervisor的使用
    查看>>
    Node入门之创建第一个HelloNode
    查看>>
    node全局对象 文件系统
    查看>>
    Node出错导致运行崩溃的解决方案
    查看>>
    Node响应中文时解决乱码问题
    查看>>
    node基础(二)_模块以及处理乱码问题
    查看>>
    node安装卸载linux,Linux运维知识之linux 卸载安装node npm
    查看>>
    node安装及配置之windows版
    查看>>
    Node实现小爬虫
    查看>>
    Node提示:error code Z_BUF_ERROR,error error -5,error zlib:unexpected end of file
    查看>>
    Node提示:npm does not support Node.js v12.16.3
    查看>>