SpringBoot异步任务、邮件发送、定时任务

SpringBoot异步任务、邮件发送、定时任务

1.异步任务

1.编写业务模拟延时进程

package com.cjp.service;

import org.springframework.stereotype.Service;

@Service
public class SynchService {
    public void hello(){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("数据处理完毕!");
    }
}

2.controller层调用业务层

package com.cjp.controller;

import com.cjp.service.SynchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SynchController {

    @Autowired
    SynchService synchService;
    
    @RequestMapping("/hello")
    public String hello(){
        synchService.hello();
        return "ok";
    }
}

3.在异步任务方法上加上注解@Async

@Service
@Async
public class SynchService {
    public void hello(){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("数据处理完毕!");
    }
}

4.在启动类中使用注解@EnableAsync开启异步功能

package com.cjp;

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


@EnableAsync
@SpringBootApplication
public class Springboot05MailApplication {

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

}

2.邮件发送

1、导入依赖

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

2.配置信息

spring.mail.username=1770364681@qq.com
spring.mail.password=
spring.mail.host=smtp.qq.com

#开启加密验证
spring.mail.properties.mail.smtp.ssl.enable=true

3.编写业务代码

package com.cjp;

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.MimeMailMessage;
import org.springframework.mail.javamail.MimeMessageHelper;

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

@SpringBootTest
class Springboot05MailApplicationTests {

    @Autowired
    JavaMailSenderImpl javaMailSender;
    @Test
    //简单邮件
    void contextLoads() {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject("你的会员已过期!");
        message.setText("您的qq会员已于2022年10月3日过期!");
        message.setTo("1770364681@qq.com");
        message.setFrom("1770364681@qq.com");
        javaMailSender.send(message);
    }

    @Test
    //复杂邮件
    void contextLoads1() throws MessagingException {
        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message,true);
        helper.setSubject("你好呀!你的QQ会员过期了哦");
        helper.setText("<p style='color:red'>您的qq会员已于2022年10月3日过期!</p>",true);
        helper.addAttachment("简历",new File("D:\\Desktop\\简历-cjp.docx"));
        helper.setTo("1770364681@qq.com");
        helper.setFrom("1770364681@qq.com");
        javaMailSender.send(message);
    }

}

3.定时任务

1.定时任务相关的两个接口、两个注解

TaskExecutor :任务执行者
TaskScheduler:任务调度程序
    
@EnableScheduling  //开启定时任务支持
@Scheduled //什么时候执行
    
cro表达式

操作:

  • 首先在启动类上使用注解开启定时任务

  • 编写定时任务的业务代码:

    package com.cjp.service;
    
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Service;
    
    @Service
    public class ScheduledService {
    
        //秒 分 时 日 月 星期几
        /*
        * 30 0/5 10,8 * * ?  每天十点和八点 每隔五分钟执行一次
        *
        * */
        @Scheduled(cron = "0 55 15 * * ?")
        public void hello(){
            System.out.println("程序执行了!!!");
        }
    }
    
    
  • 使用cron表达式控制程序执行的时间

  • 完成测试

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值