使用 maven 建立 SpringBoot 项目,引入 Web 场景启动器。html
一、编写异步服务类,注册到 IoC 容器:java
package zze.springboot.task.service; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; @Service public class AsyncService { @Async // 标识方法将会异步执行 public void hello() { try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("hello"); } }
二、使用注解开启异步任务支持:web
package zze.springboot.task; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableAsync; @EnableAsync // 开启异步任务支持 @SpringBootApplication public class TaskApplication { public static void main(String[] args) { SpringApplication.run(TaskApplication.class, args); } }
三、编写控制器测试:spring
package zze.springboot.task.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import zze.springboot.task.service.AsyncService; @RestController public class AsyncController { @Autowired private AsyncService asyncService; @GetMapping("/hello") public String testAsyncHello(){ asyncService.hello(); return "执行完毕"; /* 访问 localhost:8080/hello a、当即返回了 “执行完毕”,并无由于 asyncService.hello() 方法中的线程 sleep 等待 3 秒。 b、3 秒后控制台输出 hello OK,异步任务执行成功 */ } }
一、编写定时任务类,使用 Cron 表达式指定任务执行时机,注册到 IoC 容器:springboot
package zze.springboot.task.service; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import java.text.SimpleDateFormat; import java.util.Date; @Service public class ScheduleService { @Scheduled(cron = "*/3 * * * * ?") // 编写 cron 表达式,每 3 秒执行一次 public void test(){ Date now = new Date(); SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss"); System.out.println(timeFormat.format(now)); } }
二、使用注解开启定时任务支持:app
package zze.springboot.task; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; @EnableScheduling // 开启定时任务支持 @SpringBootApplication public class TaskApplication { public static void main(String[] args) { SpringApplication.run(TaskApplication.class, args); } }
三、启动项目测试:框架
hello at 21:32:57 hello at 21:33:00 hello at 21:33:03 hello at 21:33:06 /* 每间隔 3 秒控制台会执行 hello 方法 */
SpringBoot 中的定时任务 Cron 表达式与任务调度框架 Quartz 的 Cron 表达式规则类似,可参考【Quartz 表达式】。异步
但它们有一处区别是:async
邮件的自动配置类为 org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration 。maven
下面以 QQ 邮箱给 GMail 邮箱发送邮件为例。一、引入 Mail 场景启动器:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
二、获取 qq 邮箱受权码,登陆进入 qq 邮箱,进入设置,选择帐户,选择生成受权码:
三、邮箱相关配置:
spring.mail.username=632404164@qq.com // 使用生成的受权码 spring.mail.password=nffutccjfabdbchc spring.mail.host=smtp.qq.com # qq 邮箱须要开启 ssl spring.mail.properties.mail.smtp.sll.enable=true
四、测试:
package zze.springboot.mail; import org.junit.Test; import org.junit.runner.RunWith; 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.MimeMessageHelper; import org.springframework.test.context.junit4.SpringRunner; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.io.File; @RunWith(SpringRunner.class) @SpringBootTest public class MailApplicationTests { @Autowired JavaMailSender javaMailSender; // 发送普通文本内容 @Test public void test1() { SimpleMailMessage mailMessage = new SimpleMailMessage(); // 设置邮件标题 mailMessage.setSubject("标题"); // 设置邮件内容 mailMessage.setText("hello"); mailMessage.setFrom("632404164@qq.com"); mailMessage.setTo("zhangzhongen326@gmail.com"); javaMailSender.send(mailMessage);
} // 发送 html 内容带附件 @Test public void test2() throws MessagingException { MimeMessage mimeMessage = javaMailSender.createMimeMessage(); MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true); // 设置标题 mimeMessageHelper.setSubject("标题"); // 设置内容 mimeMessageHelper.setText("<font color='red'>hello</font>",true); // 设置附件,可设置多个 mimeMessageHelper.addAttachment("1.jpg", new File("C:\\Users\\Administrator\\Desktop\\1mail.png")); mimeMessageHelper.setFrom("632404164@qq.com"); mimeMessageHelper.setTo("zhangzhongen326@gmail.com"); javaMailSender.send(mimeMessage);
}
}