异步任务

引入
  在实际应用中,常常会使用到异步任务。比如发送邮件的时候,后台需要一段时间去发送,而前台这边需要等后台响应结束后才能进行下一步,这种问题一般使用多线程就能解决。
  开启异步任务的注解为@EnableAsync 使用的注解为@Async

package com.song.AsyncService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
@Async
public class AsyncService {
   
    public void hello (){
   
        try {
   
            Thread.sleep(3000);
        } catch (InterruptedException e) {
   
            e.printStackTrace();
        }
        System.out.println("数据正在处理中.......");
    }
}

当我们执行这个hello方法的时候他会使得下一步延迟3秒运行。比如:

import com.song.AsyncService.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";
    }
}

当我启动项目在浏览器输入localhost:8080的时候他会延迟3秒之后再return "OK"

邮件任务

邮件任务其实挺有意思的,想做的话自己可以做一个简单的邮箱。

  • 首先,要想实现这个功能必须先引入maven依赖
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
  • 开启邮箱服务(在你的邮箱设置–>账户里面),获取给你的密码

  • 配置:

spring.mail.username=2214011069@qq.com
spring.mail.password=写你刚才获取的密码
spring.mail.host=smtp.qq.com
# qq需要配置ssl
spring.mail.properties.mail.smtp.ssl.enable=true

然后我们需要里面的一个类 JavaMailSenderImpl 这个类是实现了JavaMailSender类,我们一直往下点进去看 JavaMailSender是继承了MailSender 这个接口


这里总共有两个方法,看注解可以看出,一个只是单纯发送一个简单的邮件,而另一个是发送复杂的邮件。下面是我的实现代码:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

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

@Service
public class MailService {
   
    @Autowired
    JavaMailSenderImpl mailSender;
    /** * 简单的邮件发送 */
  public  void SendSimpleMail(String Subject, String text, String toQQ, String fromQQ) {
   

        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject(Subject);
        message.setText(text);
        message.setTo(toQQ+"@qq.com");
        message.setFrom(fromQQ+"@qq.com");
        mailSender.send(message);
    }

    /** * 复杂的邮件发送 * * @throws MessagingException */
    public void SendMimeMail(String Subject, File file, String text, String toQQ, String fromQQ) throws MessagingException {
   
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setSubject(Subject);
        helper.setText(text, true);
        //发送附件
        helper.addAttachment(file.getName(), file);
        helper.setTo(toQQ+"@qq.com");
        helper.setFrom(fromQQ+"@qq.com");
        mailSender.send(message);
    }


}

注意:不能频繁的给别人发邮件,这样qq邮箱会被封。

定时任务

定时任务就是你指定个时间去运行你的代码,
首先开启注解定时功能@EnableScheduling 然后就可以在方法上面使用

package com.song.AsyncService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

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

@Service
public class SchedulingService {
   
    @Autowired
    MailService mailService;
    //秒 分 时 日 月 周几
    //0 * * * * MON-FRI
    @Scheduled(cron = "0 57 15 * * *")
    public void test() throws MessagingException {
   
      mailService.SendMimeMail("憨憨接收",new File("C:\\Users\\ASUS\\Desktop\\1.jpg"),"你是憨憨","1421073473","2214011069");
        System.out.println("执行成功!");
    }
}

这里我就是将定时任务和邮件任务结合在一起了,然后我可以实现每天早上6:00给XXX发“早安”。这是corn在线生成器的链接https://cron.qqe2.com/可以在这里设置你想设置的时间。