SpringBoot-07-任务管理

一、异步任务

①没有开启异步之前

package com.gg.service;

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

@Service
public class AsyncService {

    
    
    public void hello(){
        try{
            Thread.sleep(5000);
        }catch (InterruptedException e){
            e.printStackTrace();
        }

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

controller层调用了service层

package com.gg.controller;

import com.gg.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("/ok")
    public String hello(){
        asyncService.hello();   //  网页停止5秒,转圈
        return "ok!";
    }
}

 

 ②开启异步后

开启异步注解功能:@EnableAsync

标注方法为一个异步方法:@Async

 

 此时启动效果

 

 二、邮件任务

①简单的邮件服务

添加邮件的相关依赖

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

 运行环境在测试单元中进行

package com.gg;

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.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;

@SpringBootTest
class Springboot07TaskApplicationTests {

    @Autowired
    JavaMailSenderImpl mailSender;

    @Test
    void contextLoads() {
        //  简单的邮件
        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject("你好");
        message.setText("我在学习");
        message.setFrom("888888888@qq.com");
        message.setTo("88888888@qq.com");
        mailSender.send(message);
    }

}

测试结果:

②复杂邮件服务

@Test
    void contextLoads2() throws MessagingException {
        //  一个复杂的邮件
        MimeMessage mimeMailMessage = mailSender.createMimeMessage();
        //  组装
        MimeMessageHelper helper = new MimeMessageHelper(mimeMailMessage,true);
        helper.setSubject("你好");
        //  邮件的内容
        helper.setText("<p style='color:red'>我在学习阿</p>",true);
        //  设置邮件附件  helper.addAttachment(附件名称,new File(文件地址))
        helper.addAttachment("CSS属性书写顺序.PNG",new File("C:\\Users\\LZG\\Desktop\\CSS属性书写顺序.PNG"));
        helper.addAttachment("CSS属性书写顺序2.PNG",new File("C:\\Users\\LZG\\Desktop\\CSS属性书写顺序.PNG"));
        //  邮件的发送和接收信息
        helper.setFrom("8888888880@qq.com");
        helper.setTo("888888888@qq.com");


        //  发送邮件
        mailSender.send(mimeMailMessage);
    }

测试结果:

三、定时任务

TaskScheduler:任务调度者

TaskExecutor:任务执行者

@EnableScheduling:定时功能的注解

@Scheduled:什么时候执行

 

 

package com.gg.service;

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

@Service
public class ScheduledService {

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

    //  cron表达式
    //  秒   分   时   日   月   周几
    @Scheduled(cron="0 * * * * 0-7")
    public void hello(){
        System.out.println("被执行了......");
    }
}

 到了下一分钟的0秒开始执行一次

 特殊字符:

* :代表所有可能的值。

:表示指定范围。

:表示列出枚举值。

:表示为步长。用任意开始没间隔的步长执行一次,2/5 从2开始,每隔5执行一次

:日/星期冲突匹配符。

L:最后一个的意思。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值