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

本文共 2347 字,大约阅读时间需要 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
    @SpringBootApplication
    public 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.com
      spring.mail.password=kxhfocyyrnmuecif#qq.com
      spring.mail.host=smtp.qq.com
      spring.mail.properties.mail.smtp.ssl.enable=true
      1. 注入邮箱发送实现类:
      2. @Component
        public 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实现复杂邮件的发送。例如:

        @Test
        public 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/

    你可能感兴趣的文章
    nginx反向代理解决跨域问题,使本地调试更方便
    查看>>
    nginx启动脚本
    查看>>
    Nginx在Windows下载安装启动与配置前后端请求代理
    查看>>
    Nginx多域名,多证书,多服务配置,实用版
    查看>>
    nginx开机启动脚本
    查看>>
    nginx异常:the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf
    查看>>
    nginx总结及使用Docker创建nginx教程
    查看>>
    nginx报错:the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:128
    查看>>
    nginx报错:the “ssl“ parameter requires ngx_http_ssl_module in usrlocalnginxconfnginx.conf128
    查看>>
    nginx日志分割并定期删除
    查看>>
    Nginx日志分析系统---ElasticStack(ELK)工作笔记001
    查看>>
    Nginx映射本地json文件,配置解决浏览器跨域问题,提供前端get请求模拟数据
    查看>>
    nginx最最最详细教程来了
    查看>>
    Nginx服务器---正向代理
    查看>>
    Nginx服务器上安装SSL证书
    查看>>
    Nginx服务器的安装
    查看>>
    Nginx模块 ngx_http_limit_conn_module 限制连接数
    查看>>
    nginx添加模块与https支持
    查看>>
    Nginx用户认证
    查看>>
    Nginx的location匹配规则的关键问题详解
    查看>>