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. Spring 源码(8)Spring BeanPostProcessor的注册、国际化及事件发布机制

    上一篇文章https://www.cnblogs.com/redwinter/p/16198942.html介绍了Spring的注解的解析过程以及Spring Boot自动装配的原理,大概回顾下:Sp ...

  2. CSS展开收起

    有一个问题是,在上述例子中,把段落内容的"浮动元素"去掉后,段落最后从"行"字开始换行了,"收起"却不换行,也就是会存在有两个字内容看不见情 ...

  3. 【多线程】守护线程 Daemon

    守护线程 Daemon 线程分为用户线程和守护线程 虚拟机必须确保用户线程执行完毕 虚拟机不用等待守护线程执行完毕 如,后台记录操作日志,监控内存,垃圾回收等待.. 代码示例: /** * @Desc ...

  4. 771. Jewels and Stones - LeetCode

    Question 771. Jewels and Stones Solution 题目大意:两个字符串J和S,其中J中每个字符不同,求S中包含有J中字符的个数,重复的也算 思路:Set记录字符串J中的 ...

  5. Linux用户权限集中管理方案

    一.问题 服务器多,各个服务器上的管理人员多,ROOT权限泛滥,经常导致文件莫名其妙丢失,老手和新手对服务器的熟知程度不同,安全存在不稳定和操作安全隐患. 二.方案 利用sudo配置指定用户只能执行指 ...

  6. hdu多校题解

    hdu2020多校-1 J Math is Simple 给定 \(n\) ,求 \[\sum\limits_{1\le a<b\le n \\ gcd(a,b)=1 \\ a+b\ge n} ...

  7. JavaDoc——JavaSE基础

    JavaDoc 文档注释内容的含义 @author // 作者 @version // 版本 @since // 最早支持的Java版本 @param // 接收的参数 @return // 返回值 ...

  8. CSP J/S 初赛总结

    CSP J/S 初赛总结 2021/9/19 19:29 用官方答案估计 J 涂卡的时候唯一的一支 2B 铅笔坏了,只能用笔芯一个个涂 选择 \(-6\ pts\) 判断 \(-3\ pts\) 回答 ...

  9. Mysql中文存储、显示及不区分大小写控制

    刚开始使用mysql,以为安装了完了就可以使用了,结果是我太天真了.mysql5.7版本,默认严格区分大小写,并且不支持中文存储. 严格区分大小写,即A表和a表示两个不同的表 实例 修改 在/etc/ ...

  10. 拙见--springMVC的controller接受的请求参数

    1-这种是最常用的表单参数提交,ContentType指定为application/x-www-form-urlencoded,也就是会进行URL编码. 1.1-对象类型实体Bean接收请求参数(表单 ...