springboot中的异步、邮件、定时任务

异步任务

异步任务主要需要两个注解@EnableAsync这个是放在springboot主入口的,用于开启异步任务,@Async这个是放在业务层代码上,用于标注这个是一个异步任务。

业务层代码

package com.zkw.service;

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

@Service
public class AsyncService {

    @Async
    public void ThreadService(){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("数据正在加载.......");
    }
}

来一个controller

package com.zkw.controller;

import com.zkw.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
    private AsyncService asyncService;

    @GetMapping("/hello")
    public String Hello(){
        asyncService.ThreadService();
        //这是使用传统的方式开启一条线程,来进行异步任务的
        /*new Thread(()->{
            asyncService.ThreadService();
        }).start();*/

        return "ok";
    }
}


邮件任务

要先开启qq邮箱的smtp和pop3协议:传送门

首先导入一个依赖

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

然后配置一下yaml文件

spring.mail.username=1040419584@qq.com
spring.mail.password=123456
spring.mail.host=smtp.qq.com

# qq邮件需要特别开启的
spring.mail.properties.mail.smtp.ssl.enable=true

测试如下

 @Autowired
    JavaMailSenderImpl mailSender;

    //简单邮件发送
    @Test
    void contextLoads() {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setSubject("学习是springboot发送邮件---->这是邮件的标题");
        simpleMailMessage.setText("设置邮件发送的内容");

        //邮件接收者
        simpleMailMessage.setTo("xxiaowu99@163.com");

        //邮件发送者
        simpleMailMessage.setFrom("1040419584@qq.com");

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

    //复杂邮件发送
    @Test
    void Test1() throws MessagingException {
        //创建一个复杂的邮件
        MimeMessage mimeMessage = mailSender.createMimeMessage();

        //组装
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage,true);

        messageHelper.setSubject("邮件标题");
        // true 标志着开启识别发送文本中的html语言
        messageHelper.setText("<p style='color=red'>发送的内容</p>",true);

        //添加附件
        messageHelper.addAttachment("1.jpg",new File("C:\\Users\\10404\\Desktop\\1.png"));

        messageHelper.setTo("xxiaowu99@163.com");
        messageHelper.setFrom("1040419584@qq.com");

        mailSender.send(mimeMessage);
    }


定时任务

cron表达式:传送门

需要两个注解,分别是@EnableScheduling用于开启定时任务,Scheduled用于标注这是一个定时任务

业务层代码

package com.zkw.service;

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

@Service
public class SchedulingService {

    @Scheduled(cron = "30 34 10 * * ?")
    public void Scheduling(){
        System.out.println("被执行了.......");
    }
}

主入口

package com.zkw;

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

@SpringBootApplication
@EnableAsync // 开启异步任务
@EnableScheduling //开启时任务
public class SpringbootTestApplication {

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

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值