springboot中的任务处理

一.异步任务

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

1.创建异步处理请求

package com.springboot.assigment.service;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service; /**
* @author panglili
* @create 2022-07-12-8:19
*/ //异步请求注解,表示此类开启异步请求
@Async
@Service
public class AsyncService {
public void Asycn(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("数据处理中……");
}
}

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

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

package com.springboot.assigment.controller;

import com.springboot.assigment.service.AsyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; /**
* @author panglili
* @create 2022-07-12-8:23
*/
@Controller
public class AsycnController { @Autowired
AsyncService asyncService; @RequestMapping("/asycn")
@ResponseBody
public String asycn(){
asyncService.Asycn();
return "ok";
}
}

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

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

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

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

二.邮件任务

1.导包

<!--邮件任务-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>

2.配置文件properties

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

3.邮件发送方法

class SpringbootApplicationTests {

    @Autowired
JavaMailSender mailSender;
@Test
void contextLoads() { SimpleMailMessage message = new SimpleMailMessage();
message.setSubject("你好");
message.setText("这是发送内容~");
message.setTo("2558008051@qq.com");
message.setFrom("2558008051@qq.com");
mailSender.send(message); } }

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

三.定时执行任务

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

package com.springboot.assigment.service;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service; /**
* @author panglili
* @create 2022-07-12-10:00
*/
@Service
public class ScheduledService { //cron表达式定义时间
//注解定时任务的时间
@Scheduled(cron="0 15 10 * * ?")
public void hello(){
System.out.println("hello,你被执行了~");
}
}

2.主程序开启定时调用

@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. [报告] Microsoft :Application of deep learning methods in speech enhancement

    Application of deep learning methods in speech enhancement 语音增强中的深度学习应用 按: 本文是DNS,AEC,PLC等国际级语音竞赛的主办 ...

  2. ABP框架入门

    技术要求 在开始使用 ABP 框架之前,您需要在计算机上安装一些工具. IDE/编辑器 本书假设您使用的是Visual Studio 2022(支持 .NET 6.0 的 v10.0)或更高版本.如果 ...

  3. OpenHarmony 3.1 Beta版本关键特性解析——ArkUI容器类API介绍

    (以下内容来自开发者分享,不代表 OpenHarmony 项目群工作委员会观点) 刘鑫 容器类,顾名思义就是存储的类,用于存储各种数据类型的元素,并具备一系列处理数据元素的方法.在 ArkUI 开发框 ...

  4. sentinel基础概念及使用

    点赞再看,养成习惯,微信搜索「小大白日志」关注这个搬砖人. 文章不定期同步公众号,还有各种一线大厂面试原题.我的学习系列笔记. 什么是sentinel sentinel是Spring Cloud Al ...

  5. XCTF练习题---WEB---Training-WWW-Robots

    XCTF练习题---WEB---Training-WWW-Robots flag:cyberpeace{e37180e3f5ad17b4ac71a131e2de1fcb} 解题步骤: 1.观察题目,打 ...

  6. XCTF练习题---WEB---xff_referer

    XCTF练习题---WEB---xff_referer flag:cyberpeace{9626408a4b37eab65854d8ccd22f671c} 解题步骤: 1.观察题目,打开场景 2.注意 ...

  7. Linux screen命令详解

    开源Linux 长按二维码加关注~ 上一篇:局域网IP冲突罪魁祸首是谁? 很多时候,我们都需要执行一些需要很长时间的任务.如果这时候,你的网络连接突然断开了,那么你之前所做的所有工作可能都会丢失,所做 ...

  8. 2020年DevOps工程师入门指南

    DevOps兴起于2010年代,到现在DevOps已经在行业中拥有了一席之地,并在继续发展壮大. 有兴趣成为一名DevOps工程师吗?如果想要成为一名DevOps工程师,需要做到以下五点: 要有开发者 ...

  9. sklearn机器学习实战-KNN

    KNN分类 KNN是惰性学习模型,也被称为基于实例的学习模型 简单线性回归是勤奋学习模型,训练阶段耗费计算资源,但是预测阶段代价不高 首先工作是把label的内容进行二值化(如果多分类任务,则考虑On ...

  10. 从NSSRound#1学到了什么

    sql_by_sql 二次注入: 更改密码的功能形如: update user set password='%s' where username='%s'; 的语句就可以存在二次注入,即假设有个adm ...