1 异步任务

1.1 概述

  • 在Java应用中,绝大多数情况下都是通过同步的方式来实现交互处理的;但是在处理和第三方系统交互的时候,容易造成响应迟缓的情况,之前大部分都是使用多线程来完成此类任务,其实,在Spring3.x之后,就已经内置了@Async来完美解决这个问题。

1.2 使用步骤

  • 使用@EnableAysnc注解开启异步功能。
  • 将@Async注解标注在需要异步的类或方法上。

1.3 应用示例

  • 导入所需要的jar包的Maven坐标:
  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>
  • 使用@EnableAysnc注解开启异步功能:
  1. package com.sunxiaping.springboot;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.scheduling.annotation.EnableAsync;
  5. @SpringBootApplication
  6. @EnableAsync //开启异步注解功能
  7. public class SpringbootApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(SpringbootApplication.class, args);
  10. }
  11. }
  • AsyncService.java:
  1. package com.sunxiaping.springboot.service;
  2. import org.springframework.scheduling.annotation.Async;
  3. import org.springframework.stereotype.Service;
  4. @Service
  5. public class AsyncService {
  6. @Async
  7. public void hello(){
  8. try {
  9. Thread.sleep(3000);
  10. } catch (InterruptedException e) {
  11. e.printStackTrace();
  12. }
  13. System.out.println("。。。。数据处理中。。。。");
  14. }
  15. }
  • AsyncController.java:
  1. package com.sunxiaping.springboot.web;
  2. import com.sunxiaping.springboot.service.AsyncService;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. @RestController
  8. @RequestMapping(value = "/async")
  9. public class AsyncController {
  10. @Autowired
  11. private AsyncService asyncService;
  12. @GetMapping(value = "/hello")
  13. public String hello() {
  14. asyncService.hello();
  15. return "成功";
  16. }
  17. }

2 定时任务

2.1 概述

  • 项目开发中经常需要执行一些定时任务,比如需要在每天凌晨的时候,分析一次前一天的日志信息。Spring为我们提供了异步执行任务调度的方式,提供TaskExecutor、TaskScheduler接口。

2.2 使用步骤

  • 使用@EnableScheduling注解开启定时任务功能。
  • 将@Scheduled注解标注需要定时的类或方法上。

2.3 应用示例

  • 导入所需要的jar包的Maven坐标:
  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>
  • 使用@EnableScheduling注解开启定时任务功能:
  1. package com.sunxiaping.springboot;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.scheduling.annotation.EnableScheduling;
  5. @SpringBootApplication
  6. @EnableScheduling
  7. public class SpringbootApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(SpringbootApplication.class, args);
  10. }
  11. }
  • ScheduledService.java:
  1. package com.sunxiaping.springboot.service;
  2. import org.springframework.scheduling.annotation.Scheduled;
  3. import org.springframework.stereotype.Service;
  4. @Service
  5. public class ScheduledService {
  6. @Scheduled(cron = "*/5 * * * * ?")
  7. public void hello(){
  8. System.out.println("hello");
  9. }
  10. }

3 邮件任务

3.1 导入相关jar包的Maven坐标

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-mail</artifactId>
  4. </dependency>

3.2 配置application.yml

  1. spring:
  2. mail:
  3. username: 1900919313@qq.com
  4. password: unwcjvjwuknlbffb #授权码
  5. host: smtp.qq.com
  6. properties:
  7. mail:
  8. smpt:
  9. ssl:
  10. enable: true

3.3 测试

  1. package com.sunxiaping.springboot;
  2. import org.junit.Test;
  3. import org.junit.runner.RunWith;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.boot.test.context.SpringBootTest;
  6. import org.springframework.mail.SimpleMailMessage;
  7. import org.springframework.mail.javamail.JavaMailSender;
  8. import org.springframework.mail.javamail.MimeMessageHelper;
  9. import org.springframework.test.context.junit4.SpringRunner;
  10. import javax.mail.MessagingException;
  11. import javax.mail.internet.MimeMessage;
  12. import java.io.File;
  13. @SpringBootTest
  14. @RunWith(SpringRunner.class)
  15. public class SpringbootApplicationTests {
  16. @Autowired
  17. private JavaMailSender mailSender;
  18. /**
  19. * 简单邮件
  20. */
  21. @Test
  22. public void test(){
  23. SimpleMailMessage message = new SimpleMailMessage();
  24. //设置邮件
  25. message.setSubject("通知--开会啦");
  26. message.setText("一会开会啊");
  27. message.setTo("1975356467@qq.com");
  28. message.setFrom("1900919313@qq.com");
  29. mailSender.send(message);
  30. }
  31. /**
  32. * 复杂邮件
  33. */
  34. @Test
  35. public void test2() throws MessagingException {
  36. MimeMessage mimeMessage = mailSender.createMimeMessage();
  37. MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
  38. //设置邮件
  39. helper.setSubject("通知--开会啦");
  40. helper.setText("<b style='color:red'>今天开会啦</b>",true);
  41. helper.setTo("1975356467@qq.com");
  42. helper.setFrom("1900919313@qq.com");
  43. helper.addAttachment("1.jpg", new File("C:\\Users\\Administrator\\Pictures\\1.png"));
  44. mailSender.send(mimeMessage);
  45. }
  46. }