springboot异步任务

news/2025/2/21 4:23:07/

 在Service类声明一个注解@Async作为异步方法的标识

package com.qf.sping09test.service;import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;@Service
public class AsyncService {//告诉spring这是一个异步的方法@Asyncpublic void hello(){try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("数据正在处理...");}}

controller

package com.qf.sping09test.controller;import com.qf.sping09test.service.AsyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class AsyncController {@AutowiredAsyncService asyncService;@RequestMapping("/hello")public String hello(){asyncService.hello();//停止三秒,转圈return "ok";}}

主启动类需要加一个注解

package com.qf.sping09test;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;@SpringBootApplication
@EnableAsync
public class Sping09TestApplication {public static void main(String[] args) {SpringApplication.run(Sping09TestApplication.class, args);}}


http://www.ppmy.cn/news/1026676.html

相关文章

WPS-0DAY-20230809的分析和利用复现

WPS-0DAY-20230809的分析和初步复现 一、漏洞学习1、本地复现环境过程 2、代码解析1.htmlexp.py 3、通过修改shellcode拿shell曲折的学习msf生成sc 二、疑点1、问题2、我的测试测试方法测试结果 一、漏洞学习 强调:以下内容仅供学习和测试,一切行为均在…

Mahout教程_编程入门自学教程_菜鸟教程-免费教程分享

教程简介 Mahout 是 Apache Software Foundation(ASF) 旗下的一个开源项目,提供一些可扩展的机器学习领域经典算法的实现,旨在帮助开发人员更加方便快捷地创建智能应用程序。Mahout包含许多实现,包括聚类、分类、推荐…

题目大解析(3)

前言 这里的题目大多是用c写的。 题目 字符串中的第一个唯一字符翻转字符串验证回文串把字符串转换成整数 字符串中的第一个唯一字符 原题链接:字符串中的第一个唯一字符 计数法: class Solution { public:int firstUniqChar(string s) {int arr[130] …

高数作业啊

出函数 f ( x ) x 3 − 3 x 2 f(x)x^3-3 x^2 f(x)x3−3x2 ,确定其单调性区间、极值点以及凹凸性。如果 y u 2 yu^2 yu2 且 u 3 x 7 u3 x7 u3x7 ,求 d y d x \frac{d y}{d x} dxdy​ 。(链式法则)对于函数 f ( x , y ) x…

STM32 F103C8T6学习笔记2:GPIO的认识—GPIO的基本输入输出—点亮一个LED

今日继续学习使用 STM32 F103C8T6开发板 点亮一个LED灯,文章提供源码,测试工程,实验效果图,希望我的归纳总结会对大家有帮助~ 目录 GPIO的认识与分类 : 引脚安排整理: 定时器的引脚例举: …

Scractch3.0_Arduino_ESP32_学习随记_IO中断(六)

IO中断 目的器材程序联系我们 目的 ESP32 IO中断的使用。 中断: 当IO中断事件发生时,MCU将优先执行中断的程序。 打个比方: 你正在读一本书,突然手机收到一条紧急消息。你不想错过这个重要的消息,所以你立即停下手中的…

【深度学习_TensorFlow】调用keras高层API重写手写数字识别项目

写在前面 上一阶段我们完成了手写数字识别项目的构建,了解了网络构建、训练、测试的基本流程,但是对于一些常见的操作,因其使用过于频繁,实际上并无必要手动实现,而早已被封装为函数了。 这篇文章我们将了解keras高层…

2. Hello World

Hello World 我们将用 Java 编写两个程序。发送单个消息的生产者和接收消息并打印 出来的消费者。我们将介绍 Java API 中的一些细节。 在下图中,“ P”是我们的生产者,“ C”是我们的消费者。中间的框是一个队列-RabbitMQ 代 表使用者保留的消息缓冲区 …