三种任务及案例

本文介绍了如何在SpringBoot项目中实现异步任务、邮件发送以及定时任务。首先,通过创建Service并使用@Async注解实现异步处理。接着,配置邮件服务,包括发送简单邮件和复杂邮件。最后,利用@EnableScheduling开启定时任务,并通过@Scheduled定义cron表达式来定时执行任务。

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

一、异步任务

1.1、创建一个SpringBoot项目,导入web依赖

1.2、创建异步任务

package com.massimo.springboot09task.service;

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

@Service
public class AsyncService {

    @Async //让Spring知道这是一个异步的方法
    public void hello(){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("数据正在处理......");
    }
}

1.3、Controller

package com.massimo.springboot09task.controller;

import com.massimo.springboot09task.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";
    }
}

1.4、开启异步注解功能

package com.massimo.springboot09task;

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

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

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

}

二、邮件任务

2.1、导入依赖

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

2.2、在application.properties进行配置

spring.mail.username=xxxxx@qq.com
spring.mail.password=授权码
spring.mail.host=smtp.qq.com
# 开启加密验证
spring.mail.properties.mail.smtp.ssl.enable=true

2.3、在测试类中编写一个简单的邮件发送任务

@SpringBootTest
class Springboot09TaskApplicationTests {

    @Autowired
    JavaMailSenderImpl mailSender;

    @Test
    void contextLoads() {

        //一个简单的邮件
        SimpleMailMessage mailMessage = new SimpleMailMessage();

        mailMessage.setSubject("你好,马西莫");
        mailMessage.setText("感谢一路走来你的帮助");

        mailMessage.setTo("xxxxx@qq.com");
        mailMessage.setFrom("xxxxx@qq.com");

        mailSender.send(mailMessage);
    }

}

2.4、一个复杂的邮件发送任务

@Test
    void contextLoads2() throws MessagingException {
        //一个复杂的邮件
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        //组装
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);

        //正文
        helper.setSubject("你好,马西莫");
        helper.setText("<p style='color:red'>感谢一路走来你的帮助</p>",true);

        //附件
        helper.addAttachment("1.jpg",new File("D:\\桌面\\1.jpg"));

        helper.setTo("xxxxx@qq.com");
        helper.setFrom("xxxxx@qq.com");

        mailSender.send(mimeMessage);
    }

三、定时任务

3.1、五个了解

TaskScheduler 任务调度者
TaskExecutor 任务执行者
@EnableScheduling 开启定时功能的注解
@Scheduled 什么时候执行
Cron表达式

3.2、开启定时功能

package com.massimo.springboot09task;

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 Springboot09TaskApplication {

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

}

3.3、编写定时任务

package com.massimo.springboot09task.service;

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

@Service
public class ScheduledService {

    //在一个特定的时间执行方法

    //cron  表达式
    //秒  分  时  日  月  周几


    //每天的19点50分30秒执行一次 30 50 19 * * ?
    //每两秒执行一次  0/2 * * * * ?
    @Scheduled(cron = "30 50 19 * * ?")
    public void hello(){
        System.out.println("定时任务已被执行!");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值