SpringBoot之----异步任务、邮件发送、定时任务的实现

本文介绍SpringBoot中实现异步任务处理和定时任务调度的方法,包括配置、使用及常见表达式的解释。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.异步任务:

项目结构:
在这里插入图片描述

Springboot09TestApplication.java

package com.wlm;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;


@EnableAsync //开启异步注解功能
@SpringBootApplication
public class Springboot09TestApplication {

    public static void main(String[] args) {
        SpringApplication.run(Springboot09TestApplication.class, args);
    }

}

AsyncService.java

package com.wlm.service;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
public class AsyncService {
    //告诉spring这是一个异步的方法
    @Async
    public void hello(){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("数据正在处理");
    }
}

AsyncController.java

package com.wlm.controller;

import com.wlm.service.AsyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AsyncController {
    @Autowired
    AsyncService asyncService;
    @RequestMapping("/hello")
    public String hello(){
        asyncService.hello();//停止三秒,转圈
        return "ok";
    }

}

运行结果:
在这里插入图片描述

2.邮件发送

去QQ邮箱获取密匙:
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
pom.xml

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

application.properties

spring.mail.username=1776809811@qq.com
spring.mail.password=qtutxvgsfisbcbab
spring.mail.host=smtp.qq.com
# 开启加密授权验证
spring.mail.properties.mail.smtp.ssl.enble=true

Springboot09TestApplicationTests.java

package com.wlm;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@SpringBootTest
class Springboot09TestApplicationTests {

    @Autowired
    JavaMailSenderImpl mailSender;

    @Test
    void contextLoads() {
        //一个简单的邮件
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setSubject("咸鱼_翻身");
        mailMessage.setText("Java太简单了?,不,是你太年轻了!");
        mailMessage.setTo("1776809811@qq.com");
        mailMessage.setFrom("1776809811@qq.com");
        mailSender.send(mailMessage);
    }
    @Test
    void contextLoads2() throws MessagingException {
        //一个复杂的邮件
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        //组装
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
        helper.setSubject("咸鱼_翻身加强版");
        helper.setText("<p style='color:red'>Python太简单了?,不,是你太年轻了!</p>",true);
        //附件
        helper.addAttachment("1.jpg",new File("C:\\Users\\wlm\\Pictures\\Camera Roll\\1.png"));
        helper.setTo("1776809811@qq.com");
        helper.setFrom("1776809811@qq.com");
        mailSender.send(mimeMessage);
    }

}
3.定时任务

小伙伴们,这个很好用的(滑稽^^)
Springboot09TestApplication.java

package com.wlm;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;


@EnableAsync //开启异步注解功能
@EnableScheduling //开启定时功能注解
@SpringBootApplication
public class Springboot09TestApplication {

    public static void main(String[] args) {
        SpringApplication.run(Springboot09TestApplication.class, args);
    }

}

ScheduledService.java

package com.wlm.service;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

@Service
public class ScheduledService {
    //在一个特定的时间执行这个方法 Timer
    //cron 表达式
    //秒 分 时 日 月 周几
    //"0 47 22 * * ?" 每天22点47分30秒执行一次
    //"30 0/5 22,23 * * ?" 每天22点和23点 每隔5分钟 执行一次
    @Scheduled(cron = "30 47 22 * * ?")
    public void hello(){
        System.out.println("hello,被执行");
    }

}

常用表达式例子:

10 0 2 1 * ? *   表示在每月的1日的凌晨2点调整任务
(20 15 10 ? * MON-FRI   表示周一到周五每天上午10:15执行作
(30 15 10 ? 6L 2002-2006   表示2002-2006年的每个月的最后一个星期五上午10:15执行作
(40 0 10,14,16 * * ?   每天上午10点,下午2点,4点
(50 0/30 9-17 * * ?   朝九晚五工作时间内每半小时
(60 0 12 ? * WED    表示每个星期三中午12点
(70 0 12 * * ?   每天中午12点触发
(80 15 10 ? * *    每天上午10:15触发
(90 15 10 * * ?     每天上午10:15触发
(100 15 10 * * ? *    每天上午10:15触发
(110 15 10 * * ? 2005    2005年的每天上午10:15触发
(120 * 14 * * ?     在每天下午2点到下午2:59期间的每1分钟触发
(130 0/5 14 * * ?    在每天下午2点到下午2:55期间的每5分钟触发
(140 0/5 14,18 * * ?     在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
(150 0-5 14 * * ?    在每天下午2点到下午2:05期间的每1分钟触发
(160 10,44 14 ? 3 WED    每年三月的星期三的下午2:102:44触发
(170 15 10 ? * MON-FRI    周一至周五的上午10:15触发
(180 15 10 15 * ?    每月15日上午10:15触发
(190 15 10 L * ?    每月最后一日的上午10:15触发
(200 15 10 ? * 6L    每月的最后一个星期五上午10:15触发
(210 15 10 ? * 6L 2002-2005   2002年至2005年的每月的最后一个星期五上午10:15触发
(220 15 10 ? * 6#3   每月的第三个星期五上午10:15触发
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

咸鱼_翻身

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值