springboot中的任务处理
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中的任务处理的更多相关文章
- SpringBoot中的日志使用:
SpringBoot中的日志使用(一) 一:日志简介: 常用的日志接口 commons-logging/slf4j 日志框架:log4j/logback/log4j2 日志接口屏蔽了日志框架的底层实现 ...
- SpringBoot中yaml配置对象
转载请在页首注明作者与出处 一:前言 YAML可以代替传统的xx.properties文件,但是它支持声明map,数组,list,字符串,boolean值,数值,NULL,日期,基本满足开发过程中的所 ...
- 如何在SpringBoot中使用JSP ?但强烈不推荐,果断改Themeleaf吧
做WEB项目,一定都用过JSP这个大牌.Spring MVC里面也可以很方便的将JSP与一个View关联起来,使用还是非常方便的.当你从一个传统的Spring MVC项目转入一个Spring Boot ...
- springboot中swaggerUI的使用
demo地址:demo-swagger-springboot springboot中swaggerUI的使用 1.pom文件中添加swagger依赖 2.从github项目中下载swaggerUI 然 ...
- spring-boot+mybatis开发实战:如何在spring-boot中使用myabtis持久层框架
前言: 本项目基于maven构建,使用mybatis-spring-boot作为spring-boot项目的持久层框架 spring-boot中使用mybatis持久层框架与原spring项目使用方式 ...
- 由浅入深学习springboot中使用redis
很多时候,我们会在springboot中配置redis,但是就那么几个配置就配好了,没办法知道为什么,这里就详细的讲解一下 这里假设已经成功创建了一个springboot项目. redis连接工厂类 ...
- Springboot中使用AOP统一处理Web请求日志
title: Springboot中使用AOP统一处理Web请求日志 date: 2017-04-26 16:30:48 tags: ['Spring Boot','AOP'] categories: ...
- SpringBoot 中常用注解
本篇博文将介绍几种SpringBoot 中常用注解 其中,各注解的作用为: @PathVaribale 获取url中的数据 @RequestParam 获取请求参数的值 @GetMapping 组合注 ...
- SpringBoot中关于Mybatis使用的三个问题
SpringBoot中关于Mybatis使用的三个问题 转载请注明源地址:http://www.cnblogs.com/funnyzpc/p/8495453.html 原本是要讲讲PostgreSQL ...
随机推荐
- [AcWIng 799] 最长连续不重复子序列
点击查看代码 #include<iostream> using namespace std; const int N = 1e5 + 10; int a[N], s[N]; int mai ...
- SQL注入的几种类型
SQL注入就是: 将构造SQL语句来插入到web提交的数据之中,让其返回数据时运行自己构造的恶意SQL语句. SQL注入构造恶意SQL语句的方法有: 构造堆叠,构造闭合,构造报错,构造时间差,等等 S ...
- vue - Vue脚手架(终结篇)/ vue动画
几天的内容不是很多,因为我们脚手架的学习告一段落了,也是为了跟明天开始的内容有一个区分. 明天将会有一个非常重要的内容来了,各位,vue中的ajax他来了,这个绝对是重量级,有点兴奋! 十一.TODO ...
- Mac 手动安装 bee工具
前因: 1.go升级到1.16以后,bee官网给的安装方法(go get XXX)不好使,需要指定version. 2.指定 go get XXX@v2.0.0 可以下载,但是bee工具还是用不了:c ...
- Spring 源码(13)Spring Bean 的创建过程(4)
Spring Bean的创建过程非常的复杂,上一篇重点介绍了Spring在创建Bean的过程中,使用InstantiationBeanPostProcessor进行提前创建Bean,我们可以通过CGL ...
- JSON数据传输大法第一式——用OADate处理日期格式
JSON作为一种轻量级的数据交换格式,通常采用完全独立于编程语言的文本格式来存储和表示数据.它的层次结构简洁清晰,易于人们的阅读和编写,此外机器编写和生成也会变得容易,可以有效地提升网络传输效率,这些 ...
- 219. Contains Duplicate II - LeetCode
Question 219. Contains Duplicate II Solution 题目大意:数组中两个相同元素的坐标之差小于给定的k,返回true,否则返回false 思路:用一个map记录每 ...
- Nginx分片限流
一.全局限流 在http节点中添加 # 创建限流规则 limit_req_zone $binary_remote_addr zone=addr:10m rate=1r/s;# 客户端进行限流 # li ...
- MySql实例关于ifnull,count,case when,group by(转力扣简单)
给定表 customer ,里面保存了所有客户信息和他们的推荐人. id | name | referee_id|+------+------+-----------+| 1 | Will ...
- unittest框架里的常用断言方法:用于检查数据
1.unittest框架里的常用断言方法:用于检查数据. (1)assertEqual(x,y) 检查两个参数类型相同并且值相等.(2)assertTrue(x) 检查唯一的参数值等于True(3)a ...