异步工作
背景
在咱们的业务解决中,比方解决实现须要3s能力实现,然而咱们须要如果要让用户等3s,则体验十分差,所以咱们采纳异步的形式去解决,能够通过线程池来解决 ,然而还要写线程,而springboot中曾经默认提供了这种能力 ,咱们只有开启即可应用。
具体应用
- 创立我的项目
咱们只有创立一个根本的springboot我的项目,而后只有增加web的依赖即可。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId></dependency>
- 编写场景模仿下面背景
service层:
@Servicepublic class AsyncService { public void sync(){ try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("办法中在执行中"); } public void async(){ try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("办法中在执行中"); }}
controller层:
@RestControllerpublic class AsyncController { @Autowired private AsyncService asyncService; @GetMapping("/sync") public String sync(){ asyncService.sync(); return "解决实现"; } @GetMapping("/async") public String async(){ asyncService.async(); return "解决实现"; }}
- 启动我的项目,测试
申请localhost:8080/sync
,发现会转圈期待3s后才有返回值:
- 问题解决
采纳异步的形式来解决这个问题。
首先在启动类上关上异步:@EnableAsync
,而后再调用的办法上增加注解@Async
# 启动类 @SpringBootApplication@EnableAsyncpublic class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); }}
#办法 @Async public void async(){ try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("办法中在执行中"); }
而后再次申请ocalhost:8080/async
,发现咱们的办法立马就返回了。这样就是实现了异步工作。
邮件工作
背景
通过springboot提供的mail-starter来实现邮件工作。
具体应用
- 增加依赖
<!--mail--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
- 配置文件
spring.mail.username=xxxx@qq.comspring.mail.password=你的qq受权码spring.mail.host=smtp.qq.com# qq须要配置sslspring.mail.properties.mail.smtp.ssl.enable=true
获取受权码:在QQ邮箱中的设置->账户->开启pop3和smtp服务.
- Spring单元测试
@AutowiredJavaMailSenderImpl mailSender;@Testpublic void contextLoads() { //邮件设置1:一个简略的邮件 SimpleMailMessage message = new SimpleMailMessage(); message.setSubject("告诉-今天来下班"); message.setText("今晚7:30散会"); message.setTo("xxx@qq.com"); message.setFrom("xxx@qq.com"); mailSender.send(message);}@Testpublic void contextLoads2() throws MessagingException { //邮件设置2:一个简单的邮件 MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setSubject("告诉-今天吃饭"); helper.setText("<b style='color:red'>明天 7:30来散会</b>",true); //发送附件 helper.addAttachment("1.jpg",new File("")); helper.addAttachment("2.jpg",new File("")); helper.setTo("xxx@qq.com"); helper.setFrom("xxx@qq.com"); mailSender.send(mimeMessage);
定时工作
背景
在指定的工夫或者场景下执行某些代码。
相关联的类和注解
TaskScheduler : 任务调度者TaskExecutor :工作执行者@EnableScheduling : 开启定时性能的注解@schedule :什么时候执行
具体应用
只有导入了springboot的web依赖,就默认给咱们导入了这个性能,不须要咱们再去导入。
对于cron表达式的能够参考我的另一篇文章:spring中@schedule注解的应用
- 在启动类上增加注解
@EnableScheduling
@SpringBootApplication@EnableAsync@EnableSchedulingpublic class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); }}
- service层编写办法
@Servicepublic class ScheduleService { public void testSchedule(){ System.out.println("工作被执行了"); }}
- 增加注解 @Scheduled
其中的cron表达式能够参考我的另一篇博客:spring中@schedule注解的应用,也就是固定的工夫周期去执行
@Servicepublic class ScheduleService { /** * 每隔10s执行一次 */ @Scheduled(cron = "0/10 * * * * ?") public void testSchedule(){ System.out.println(LocalDateTime.now()+"工作被执行了"); }}
- 后果
每隔10s执行一次,验证了准确性。