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

    你可能感兴趣的文章
    python 中 dict 的另一种用法
    查看>>
    Python 中 PIL 读取图片出现异常旋转的解决方法
    查看>>
    python读取word表格内容(1)
    查看>>
    python 中os.path.join 双斜杠的解决办法
    查看>>
    python 中PIL.Image和OpenCV图像格式相互转换
    查看>>
    Python 中Semaphore 信号量对象、Event事件、Condition
    查看>>
    python 中with的使用及样例
    查看>>
    python读取wav文件并播放[pyaudio/wave]
    查看>>
    python读取txt文件的行数
    查看>>
    Python 中内置的最大堆 API
    查看>>
    Python 中只有一个 True 和一个 False 对象吗?
    查看>>
    python读取mtcars数据集并实现以下操作_关于数据处理。。,Python交流,技术交流区,鱼C论坛 - Powered by Discuz!...
    查看>>
    Python 中多线程与多处理之间的区别
    查看>>
    Python 中如何使用 lambda 函数
    查看>>
    Python 中如何创建多行字符串?
    查看>>
    Python 中如何处理异常?
    查看>>
    Python 中如何实现列表的切片?
    查看>>
    Python 中如何实现字典的排序?
    查看>>
    Python 中常用的数据类型及相关操作详解
    查看>>
    python 中文乱码
    查看>>