广告
返回顶部
首页 > 资讯 > 后端开发 > Python >详解Springboot中的异步、定时、邮件任务
  • 681
分享到

详解Springboot中的异步、定时、邮件任务

2024-04-02 19:04:59 681人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

目录一、异步任务1、编写一个类AsyncService 2、编写一个AsyncController类3、开启异步二、邮件任务1、引入依赖2、配置mail3、测试三、定时任务1、编写一

一、异步任务

1、编写一个类AsyncService

异步处理还是非常常用的,比如我们在网站上发送邮件,后台会去发送邮件,此时前台会造成响应不动,直到邮件发送完毕,响应才会成功,所以我们一般会采用多线程的方式去处理这些任务。


package com.rk.service;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@Service
public class AsyncService {
    public void hello(){
        try {
            System.out.println("数据处理中~");
            Thread.sleep(3000);//停止三秒
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

2、编写一个AsyncController类


package com.rk.controller;
import com.rk.service.AsyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.WEB.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class AsyncController {
    @Autowired
    AsyncService asyncService;
    @GetMapping("/hello")
    public String hello(){
        asyncService.hello();
        return "success";
    }
}

现在启动项目进行测试,三秒后才会出现success,现在还不是异步

3、开启异步


 @Async//告诉spring这是一个异步方法
    public void hello(){
        try {
            System.out.println("数据处理中~");
            Thread.sleep(3000);//停止三秒
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


@EnableAsync//开启异步注解功能
@SpringBootApplication
public class Springboot09TestApplication {
    public static void main(String[] args) {
        SpringApplication.run(Springboot09TestApplication.class, args);
    }
}

二、邮件任务

1、引入依赖


       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

2、配置mail


#用户名
spring.mail.username=1624603357@qq.com
#密码
spring.mail.passWord=yblyxhvmnsurbbci
#发送邮件服务器
spring.mail.host=smtp.qq.com
#开启加密验证 ssl
spring.mail.properties.mail.smtp.ssl.enable=true

3、测试

简单邮件


     @Autowired
    JavaMailSenderImpl mailSender;
 
 
     @Test
    void contextLoads() {
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setSubject("你好,rk");//邮件标题
        mailMessage.setText("测试邮件");//邮件内柔
        mailMessage.setTo("r1624603357@126.com");//收件人邮箱
        mailMessage.setFrom("1624603357@qq.com");//发件人邮箱
        mailSender.send(mailMessage);
 
    }

复杂邮件


    @Test
    void contextLoads2() throws MessagingException {
        //一个复杂的邮件
        MimeMessage mimeMessage = mailSender.createMimeMessage();
 
        //组装
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
 
        //正文
        helper.setSubject("你好,rk");
        helper.setText("<h1 style='color:red'>测试邮件</h1>",true);
 
        //附件
        helper.addAttachment("1.png",new File("D:\\QLDownloadGame\\2\\1.png"));
        helper.addAttachment("rk.docx",new File("E:\\桌面\\rk.docx"));
 
        // 发/收件人
        helper.setTo("r1624603357@126.com");
        helper.setFrom("1624603357@qq.com");
 
        //发送
        mailSender.send(mimeMessage);
 
    }

三、定时任务

1、编写一个ScheduledService类


@Service
public class ScheduledService {
    //秒 分 时 日 月 周几
    //0 * * * * MON-FRI
    //注意cron表达式的用法;   每天20:28 0秒执行该方法
    @Scheduled(cron = "0 28 20 * * 0-7")
    public void hello(){
        System.out.println("现在是20:28");
        System.out.println("hello.....");
    }
}

项目启动后每天20:28:00执行hello方法

2、添加注解


@EnableAsync//开启异步注解功能
@EnableScheduling//开启定时功能注解
@SpringBootApplication
public class Springboot09TestApplication {
    public static void main(String[] args) {
        SpringApplication.run(Springboot09TestApplication.class, args);
    }
}

cron表达式练习

到此这篇关于Springboot的异步、定时、邮件任务的文章就介绍到这了,更多相关Springboot异步定时邮件任务内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: 详解Springboot中的异步、定时、邮件任务

本文链接: https://www.lsjlt.com/news/156185.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作