springboot中的任务处理

一.异步任务

在开发中有时用户提交的数据,后台需要一定时间才能做出响应,此时用户在前台也不能在等待中,此时就应该先开启异步请求处理,利用多线程,先给前台反馈,后台另一线程去处理数据。

1.创建异步处理请求

  1. package com.springboot.assigment.service;
  2. import org.springframework.scheduling.annotation.Async;
  3. import org.springframework.stereotype.Service;
  4. /**
  5. * @author panglili
  6. * @create 2022-07-12-8:19
  7. */
  8. //异步请求注解,表示此类开启异步请求
  9. @Async
  10. @Service
  11. public class AsyncService {
  12. public void Asycn(){
  13. try {
  14. Thread.sleep(3000);
  15. } catch (InterruptedException e) {
  16. e.printStackTrace();
  17. }
  18. System.out.println("数据处理中……");
  19. }
  20. }

此程序中,后台需要三秒等待才能处理好请求。

2.controller调用异步业务请求的方法

  1. package com.springboot.assigment.controller;
  2. import com.springboot.assigment.service.AsyncService;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.ResponseBody;
  7. /**
  8. * @author panglili
  9. * @create 2022-07-12-8:23
  10. */
  11. @Controller
  12. public class AsycnController {
  13. @Autowired
  14. AsyncService asyncService;
  15. @RequestMapping("/asycn")
  16. @ResponseBody
  17. public String asycn(){
  18. asyncService.Asycn();
  19. return "ok";
  20. }
  21. }

在执行完对异步业务的调用之后才会返回给前台一个ok!

3.主程序开启异步处理,创建多线程池!

  1. @EnableAsync
  2. //开启异步处理,底部开启了一个线程池存放异步请求的处理

总结:实现异步处理只需要加上两个注解,在异步请求服务上加上@Async在主程序上加上@EnableAsync 即可!

二.邮件任务

1.导包

  1. <!--邮件任务-->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-mail</artifactId>
  5. </dependency>

2.配置文件properties

  1. spring.mail.username=2558008051@qq.com
  2. spring.mail.password=lswpwkcyalcsdhjc
  3. #开启加密验证
  4. spring.mail.properties.mail.smtp.ssl.enable=true
  5. spring.mail.host=smtp.qq.com
  6. spring.mail.default-encoding=UTF-8
  7. spring.mail.properties.mail.smtp.auth=true
  8. spring.mail.properties.mail.smtp.starttls.enable=false
  9. spring.mail.properties.mail.smtp.starttls.required=false

3.邮件发送方法

  1. class SpringbootApplicationTests {
  2. @Autowired
  3. JavaMailSender mailSender;
  4. @Test
  5. void contextLoads() {
  6. SimpleMailMessage message = new SimpleMailMessage();
  7. message.setSubject("你好");
  8. message.setText("这是发送内容~");
  9. message.setTo("2558008051@qq.com");
  10. message.setFrom("2558008051@qq.com");
  11. mailSender.send(message);
  12. }
  13. }

简单的邮件发送功能完成!

三.定时执行任务

1.写一个需要定时执行的任务

  1. package com.springboot.assigment.service;
  2. import org.springframework.scheduling.annotation.Scheduled;
  3. import org.springframework.stereotype.Service;
  4. /**
  5. * @author panglili
  6. * @create 2022-07-12-10:00
  7. */
  8. @Service
  9. public class ScheduledService {
  10. //cron表达式定义时间
  11. //注解定时任务的时间
  12. @Scheduled(cron="0 15 10 * * ?")
  13. public void hello(){
  14. System.out.println("hello,你被执行了~");
  15. }
  16. }

2.主程序开启定时调用

  1. @EnableScheduling//开启定时功能支持

只需要两个简单的注解

@Scheduled://注解定时任务的时间

@EnableScheduling://开启定时功能支持

ok!

springboot中的任务处理的更多相关文章

  1. SpringBoot中的日志使用:

    SpringBoot中的日志使用(一) 一:日志简介: 常用的日志接口 commons-logging/slf4j 日志框架:log4j/logback/log4j2 日志接口屏蔽了日志框架的底层实现 ...

  2. SpringBoot中yaml配置对象

    转载请在页首注明作者与出处 一:前言 YAML可以代替传统的xx.properties文件,但是它支持声明map,数组,list,字符串,boolean值,数值,NULL,日期,基本满足开发过程中的所 ...

  3. 如何在SpringBoot中使用JSP ?但强烈不推荐,果断改Themeleaf吧

    做WEB项目,一定都用过JSP这个大牌.Spring MVC里面也可以很方便的将JSP与一个View关联起来,使用还是非常方便的.当你从一个传统的Spring MVC项目转入一个Spring Boot ...

  4. springboot中swaggerUI的使用

    demo地址:demo-swagger-springboot springboot中swaggerUI的使用 1.pom文件中添加swagger依赖 2.从github项目中下载swaggerUI 然 ...

  5. spring-boot+mybatis开发实战:如何在spring-boot中使用myabtis持久层框架

    前言: 本项目基于maven构建,使用mybatis-spring-boot作为spring-boot项目的持久层框架 spring-boot中使用mybatis持久层框架与原spring项目使用方式 ...

  6. 由浅入深学习springboot中使用redis

    很多时候,我们会在springboot中配置redis,但是就那么几个配置就配好了,没办法知道为什么,这里就详细的讲解一下 这里假设已经成功创建了一个springboot项目. redis连接工厂类 ...

  7. Springboot中使用AOP统一处理Web请求日志

    title: Springboot中使用AOP统一处理Web请求日志 date: 2017-04-26 16:30:48 tags: ['Spring Boot','AOP'] categories: ...

  8. SpringBoot 中常用注解

    本篇博文将介绍几种SpringBoot 中常用注解 其中,各注解的作用为: @PathVaribale 获取url中的数据 @RequestParam 获取请求参数的值 @GetMapping 组合注 ...

  9. SpringBoot中关于Mybatis使用的三个问题

    SpringBoot中关于Mybatis使用的三个问题 转载请注明源地址:http://www.cnblogs.com/funnyzpc/p/8495453.html 原本是要讲讲PostgreSQL ...

随机推荐

  1. @Transactional的使用与失效

    @Transactinonal 注解在方法抛出RuntimeException类及其子类时.Error类及其子类时会回滚当前事务,使sql不提交: 只能作用于public的方法:写在类上时,代表给该类 ...

  2. 设置 Visual Studio 总是以管理员身份运行

    话不多说直接上干货 第一步: 打开 Visual Studio 的安装目录,找到 devenv.exe,然后右键快捷菜单选择"兼容性疑难解答". 第二步: 选择故障排查选项 疑难解 ...

  3. HashMap中红黑树插入节点的调整过程

    如果有对红黑树的定义及调整过程有过研究,其实很容易理解HashMap中的红黑树插入节点的调整过程. "红黑树定义及调整过程"的参考文章:<红黑树原理.查找效率.插入及变化规则 ...

  4. Ubuntu 系统安装,VMware

    系统版本   ubuntu-18.04.5-server-amd64.iso 1.自定义安装 2.默认下一步 3. 稍后安装操作系统 4.选择ubuntu 64位 5.选额安装的目录 6.设置虚拟机c ...

  5. 接口测试使用Python装饰器

    写接口case时,有时需要对cae做一些共性的操作,最典型的场景如:获取case执行时间.打印log等. 有没有一种办法来集中处理共性操作从而避免在每个case中都写相同的代码(如:每个case都需要 ...

  6. OAuth2密码模式已死,最先进的Spring Cloud认证授权方案在这里

    旧的Spring Security OAuth2停止维护已经有一段时间了,99%的Spring Cloud微服务项目还在使用这些旧的体系,严重青黄不接.很多同学都在寻找新的解决方案,甚至还有念念不忘密 ...

  7. redis & redis sentinel

    Redis 命令参考 Redis Sentinel Cheat Sheet Redis 哨兵节点之间相互自动发现机制(自动重写哨兵节点的配置文件) Redis哨兵模式(sentinel)学习总结及部署 ...

  8. 编程语言与python与pycharm的下载

    目录 编程语言的发展史 编程语言的分类 python解释器 python解释器的下载与安装 环境变量 执行python程序方式 pycharm编辑器 编程语言的发展史 机器语言是最开始的编程语言,之后 ...

  9. 20212115朱时鸿实验一《python程序设计》实验报告

    ------------恢复内容开始------------ #学号20212115 <python程序设计>实验一报告 课程: <python程序设计> 班级:2121 姓名 ...

  10. 一次XGBoost性能优化-超线程影响运算速度

    一.问题背景 一个朋友在使用 XGBoost 框架进行机器学习编码,他们的一个demo, 在笔记本的虚拟机(4核)运行的时候,只要8s, 但是在一个64核128G 的物理机上面的虚拟机去跑的时候,发现 ...