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. xpath & csv文件读写

    原理:拿到网页源代码并且进行分析 关键词:etree     .xpath      a[@href="dapao"]      a/@href       text() impo ...

  2. keepalived安装及配置文件详解

    一个执着于技术的公众号 在上一篇文章中,我们对Keepalived进行了简单入门学习: Keepalived入门学习 今天我们继续学习Keepalived服务. 一.安装Keepalived服务 两种 ...

  3. MyBatisPlus实现分页和查询操作就这么简单

    <SpringBoot整合MybatisPlus基本的增删改查,保姆级教程>在这篇文章中,我们详细介绍了分页的具体实现方法.但是,在日常的开发中还需要搜索功能的.下面让我们一起动起手来,实 ...

  4. C#/VB.NET 实现Word和ODT文档相互转换

    ODT文档格式一种开放文档格式(OpenDocument Text).通常,ODT格式的文件可以使用LibreOffice Writer.MS Word或其他一些文档编辑器来打开.我们在处理文档时,可 ...

  5. sklearn机器学习实战-KNN

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

  6. linux篇-Parse error: syntax error, unexpected ‘new’ (T_NEW) in /usr/local/nginx/html/cacti/lib/adodb

    1首先这是基于lnmp模式进行的 2yum安装 yum -y install httpd mysql mysql-server php php-mysql php-json php-pdo 3lib库 ...

  7. Flask表单验证

    学习内容:①判断请求方式(request.method) from flask import Flask,render_template,request app = Flask(__name__) @ ...

  8. 课堂测试——HDFS操作

    要求: 编程实现以下功能,并利用Hadoop提供的Shell命令完成相同任务: 向HDFS中上传任意文本文件,如果指定的文件在HDFS中已经存在,则由用户来指定是追加到原有文件末尾还是覆盖原有的文件: ...

  9. while和for循环的补充与数据类型的内置方法(int, float, str)

    目录 while与for循环的补充 while + else 死循环 while的嵌套 for补充 range函数 break与continue与else for循环的嵌套 数据类型的内置方法 int ...

  10. RabitMQ 简介

    每日一句 The secret of being miserable is to have leisure to bother about whether you are happy or not. ...