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

    你可能感兴趣的文章
    npm install无法生成node_modules的解决方法
    查看>>
    npm install的--save和--save-dev使用说明
    查看>>
    npm node pm2相关问题
    查看>>
    npm run build 失败Compiler server unexpectedly exited with code: null and signal: SIGBUS
    查看>>
    npm run build报Cannot find module错误的解决方法
    查看>>
    npm run build部署到云服务器中的Nginx(图文配置)
    查看>>
    npm run dev 和npm dev、npm run start和npm start、npm run serve和npm serve等的区别
    查看>>
    npm run dev 报错PS ‘vite‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。
    查看>>
    npm scripts 使用指南
    查看>>
    npm should be run outside of the node repl, in your normal shell
    查看>>
    npm start运行了什么
    查看>>
    npm WARN deprecated core-js@2.6.12 core-js@<3.3 is no longer maintained and not recommended for usa
    查看>>
    npm 下载依赖慢的解决方案(亲测有效)
    查看>>
    npm 安装依赖过程中报错:Error: Can‘t find Python executable “python“, you can set the PYTHON env variable
    查看>>
    npm.taobao.org 淘宝 npm 镜像证书过期?这样解决!
    查看>>
    npm—小记
    查看>>
    npm上传自己的项目
    查看>>
    npm介绍以及常用命令
    查看>>
    NPM使用前设置和升级
    查看>>
    npm入门,这篇就够了
    查看>>