异步任务|学习笔记

简介: 快速学习异步任务

开发者学堂课程【SpringBoot 快速掌握 - 高级应用:课时名称】学习笔记,与课程紧密联系,让用户快速学习知识

课程地址https://developer.aliyun.com/learning/course/613/detail/9309


异步任务

定时任务的创建

创建一个工程,

groups:comatguigu,Artifact:springboot-04-task

Package: com.atguigu.task,点击 next。

选择1,5.12版本,先只选择 web 模块,点击 next,完成创建。

先来测试 eb 任务,

创建一个 src,点击  task,new,java class,

Creat new class

Name:service.asyncservice,点击 OK 创建完成。

Eb 任务还是非常常见的,例如发送邮件,处理数据时,不想让他阻塞下面的线程,就可以用多线程的形式进行一些 eb 处理。

package com.atguigu.task.service;

import org.springframework.stereotype.Service;

@Service

public class AsyncService {

public void hello(){

try{

Thread.sLeep( millis: 3000) ;

}catch (InterruptedException e) {

e.printstackTrace();

}

System.out.println("处理数据中...");

}

}

调用这个方法需要漫长的等待点击 task,new,java class, Creat new class

Name:controller.asynccontroller

package com.atguigu.task.controller;

import com.atguigu.task.service.AsyncService;

importorg.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;

importorg.springframework.web.bind.annotation.RestController;

@RestController

public class AsyncController i

@Autowired

AsyncService asyncService;

@GetMapping( " / he1lo")

public String hello(){

asyncService.hello();

return "success";l

运行,

假如不希望用户在三秒以后得到响应,可以写成多线程的方式让他运行,但每次编写过于麻烦,所以可以在上述方法中加一个注解。

@Async

mportorg.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;

importorg.springframework.web.bind.annotation.RestController;

@RestController

@Async//告诉spring这是一个异步方法

public class AsyncController i

@Autowired

AsyncService asyncService;

@GetMapping( " / he1lo")

public String hello(){

asyncService.hello();

return "success";l

同时需要:

package com.atguigu.task;

import ...

EnableAsync 1/开启异步注解功能

SpringBootApplication

public class Springboot84TaskApplication {

publicstaticvoidmain(String[] args){ SpringApplication.run(Springboot04TaskApplication.

}

此时立即响应,并且不需要等待三秒。

相关文章
|
4天前
|
存储 关系型数据库 分布式数据库
PostgreSQL 18 发布,快来 PolarDB 尝鲜!
PostgreSQL 18 发布,PolarDB for PostgreSQL 全面兼容。新版本支持异步I/O、UUIDv7、虚拟生成列、逻辑复制增强及OAuth认证,显著提升性能与安全。PolarDB-PG 18 支持存算分离架构,融合海量弹性存储与极致计算性能,搭配丰富插件生态,为企业提供高效、稳定、灵活的云数据库解决方案,助力企业数字化转型如虎添翼!
|
15天前
|
弹性计算 关系型数据库 微服务
基于 Docker 与 Kubernetes(K3s)的微服务:阿里云生产环境扩容实践
在微服务架构中,如何实现“稳定扩容”与“成本可控”是企业面临的核心挑战。本文结合 Python FastAPI 微服务实战,详解如何基于阿里云基础设施,利用 Docker 封装服务、K3s 实现容器编排,构建生产级微服务架构。内容涵盖容器构建、集群部署、自动扩缩容、可观测性等关键环节,适配阿里云资源特性与服务生态,助力企业打造低成本、高可靠、易扩展的微服务解决方案。
1309 5
|
1天前
|
监控 JavaScript Java
基于大模型技术的反欺诈知识问答系统
随着互联网与金融科技发展,网络欺诈频发,构建高效反欺诈平台成为迫切需求。本文基于Java、Vue.js、Spring Boot与MySQL技术,设计实现集欺诈识别、宣传教育、用户互动于一体的反欺诈系统,提升公众防范意识,助力企业合规与用户权益保护。
|
14天前
|
机器学习/深度学习 人工智能 前端开发
通义DeepResearch全面开源!同步分享可落地的高阶Agent构建方法论
通义研究团队开源发布通义 DeepResearch —— 首个在性能上可与 OpenAI DeepResearch 相媲美、并在多项权威基准测试中取得领先表现的全开源 Web Agent。
1343 87
|
1天前
|
JavaScript Java 大数据
基于JavaWeb的销售管理系统设计系统
本系统基于Java、MySQL、Spring Boot与Vue.js技术,构建高效、可扩展的销售管理平台,实现客户、订单、数据可视化等全流程自动化管理,提升企业运营效率与决策能力。
|
3天前
|
弹性计算 安全 数据安全/隐私保护
2025年阿里云域名备案流程(新手图文详细流程)
本文图文详解阿里云账号注册、服务器租赁、域名购买及备案全流程,涵盖企业实名认证、信息模板创建、域名备案提交与管局审核等关键步骤,助您快速完成网站上线前的准备工作。
189 82
2025年阿里云域名备案流程(新手图文详细流程)