08SpringBoot 异步,定时,邮件任务

异步任务

适用情景:
比如我们在网站上发送邮件,后台去发送邮件,此时前台会造成响应不动,直到邮件发送完毕,响应才会成功的所以我们一般会采用多线程的方式去处理这些任务。

1.创建一个类AsyncService
编写方法,假装正在处理数据,使用线程设置一些延时,模拟同步的的等待情况,给hello方法添加@Async注解;

@Service
public class AsyncService {

//告诉Spring这是一个异步方法
   @Async
   public void hello(){
       try {
           Thread.sleep(3000);
      } catch (InterruptedException e) {
           e.printStackTrace();
      }
       System.out.println("业务进行中....");
  }
}

SpringBoot就会自己开一个线程池,进行调用!但是要让这个注解生效,我们还需要在主程序上添加@EnableAsync,开启异步注解功能

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

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

}

测试

@RestController
public class AsyncController {

   @Autowired
   AsyncService asyncService;

   @GetMapping("/hello")
   public String hello(){
       asyncService.hello();
       return "success";
  }
}

5.访问http://localhost:8080/hello进行测试,网页瞬间响应,后台代码依旧执行!

定时任务

适用情景:项目开发中需要执行一些定时任务,比如需要在每天凌晨的时候,分析前一天的日志信息,Spring为我们提供了异步执行的调度的方式,提供了两个接口和两个注解

TaskException接口
TaskSheduler接口

@EnableScheduling
@Scheduled
测试:
1.创建一个ScheduledService,利用cron表达式,定时执行任务

@Service
public class ScheduledService {
   //秒   分   时     日   月   周几
   //0 * * * * MON-FRI
   //注意cron表达式的用法;
   @Scheduled(cron = "0 * * * * 0-7")
   public void hello(){
       System.out.println("hello.....");
  }
}

2.这里写完定时任务结束后,我们需要在主程序上增加@EnableScheduling开启定时任务功能

@EnableAsync //开启异步注解功能
@EnableScheduling //开启基于注解的定时任务
@SpringBootApplication
public class SpringbootTaskApplication {

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

}

常用的cron表达式:
0 0 10,14,16 * * ? 每天上午10点,下午2点,4点
0 0/30 9-17 * * ? 朝九晚五工作时间内每半小时
0 0 12 ? * WED 表示每个星期三中午12点
“0 0 12 * * ?” 每天中午12点触发
“0 15 10 ? * *” 每天上午10:15触发
“0 15 10 * * ?” 每天上午10:15触发
“0 15 10 * * ? *” 每天上午10:15触发
“0 15 10 * * ? 2005” 2005年的每天上午10:15触发
“0 * 14 * * ?” 在每天下午2点到下午2:59期间的每1分钟触发
“0 0/5 14 * * ?” 在每天下午2点到下午2:55期间的每5分钟触发
“0 0/5 14,18 * * ?” 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
“0 0-5 14 * * ?” 在每天下午2点到下午2:05期间的每1分钟触发
“0 10,44 14 ? 3 WED” 每年三月的星期三的下午2:10和2:44触发
“0 15 10 ? * MON-FRI” 周一至周五的上午10:15触发
“0 15 10 15 * ?” 每月15日上午10:15触发
“0 15 10 L * ?” 每月最后一日的上午10:15触发
“0 15 10 ? * 6L” 每月的最后一个星期五上午10:15触发
“0 15 10 ? * 6L 2002-2005” 2002年至2005年的每月的最后一个星期五上午10:15触发
“0 15 10 ? * 6#3” 每月的第三个星期五上午10:15触发

邮件任务

适用情景:邮件发送SpringBoot也帮我们做了支持

  1. 引入相关pom.xml依赖
 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
  1. 需要在properties配置相应的参数,以spring.mail.xxx
    在spring.factories搜索MailSenderAutoConfiguration
    在这里插入图片描述
    点击进入MailSenderJndiConfiguration.class文件
    在这里插入图片描述
    点击进入MailProperties
    在这里插入图片描述会发现前缀是以“spring.mail”开头的,可以配置的相关参数都在此类里面。

我们知道相关参数配置的由来,这样就可以配置相关参数:

spring.mail.username=1518461697@qq.com
spring.mail.password=pphjkywjqqsxfifj6
spring.mail.host=smtp.qq.com
#qq需要配置SSL
spring.mail.properties.mail.smtp.ssL.enable=true
  1. username:发送方邮件地址
  2. password:授权码
    在这里插入图片描述3.邮件测试
    注意注入其实现类:JavaMailSenderImpl
@SpringBootTest
class ApplicationTests {

    @Autowired
    JavaMailSenderImpl mailSender;
    @Test
    void contextLoads() {
        SimpleMailMessage message=new SimpleMailMessage();
        message.setSubject("Thanks");
        message.setText("感谢自己的坚持");
        message.setFrom("1518461697@qq.com");
        message.setTo("1518461697@qq.com");
        mailSender.send(message);
    }

测试:
在这里插入图片描述
复杂邮件:包括基础的

,style标签,还有图片

  @Test
    public void send() throws MessagingException {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper=new MimeMessageHelper(mimeMessage,true);
        helper.setSubject("感谢信");
        helper.setText("<p style='color:red'>愿你鲜衣怒马,归来仍是少年</p>",true);
        helper.addAttachment("图片2",new File("C:\\Users\\wcj\\Desktop\\1.jpg"));

        helper.setFrom("1518461697@qq.com");
        helper.setTo("1518461697@qq.com");
        mailSender.send(mimeMessage);
    }

测试:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值