Java?SpringTask定時(shí)自動(dòng)化處理方法
一、自動(dòng)化處理
1.1 什么是自動(dòng)化處理
自動(dòng)化處理是指使用軟件工具或程序自動(dòng)執(zhí)行原本需要人工干預(yù)的任務(wù)。這些任務(wù)可以是重復(fù)性的、耗時(shí)的或者需要高度準(zhǔn)確性的操作。通過(guò)自動(dòng)化,不僅可以提高工作效率和準(zhǔn)確性,還可以釋放人力資源以專(zhuān)注于更高價(jià)值的工作。
1.2 SpringTask介紹

二、SpringTask的基本使用
2.1 引入依賴(lài)
由于springTask 是SpringFramWork包的內(nèi)容,所以不需要進(jìn)行引入新的依賴(lài)。
2.2 通過(guò)控制臺(tái)加入注解啟用SpringTask
@SpringBootApplication
@EnableScheduling
public class SpringTaskApplication {
public static void main(String[] args) {
SpringApplication.run(SpringTaskApplication.class, args);
}
}2.3 使用Cron表達(dá)式規(guī)定時(shí)間

如果不會(huì)使用Cron表達(dá)式的使用可以直接使用cron的生成網(wǎng)站
https://cron.qqe2.com/

常用cron表達(dá)式:

2.4 通過(guò)@Schedule(Cron表達(dá)式) 實(shí)現(xiàn)定時(shí)任務(wù)(每?jī)擅雸?zhí)行一次)
@Component
@Slf4j
public class springTaskTest {
// 每?jī)擅雸?zhí)行一次
@Scheduled(cron = "0/2 * * * * ?")
public void AutoTask(){
log.info("自動(dòng)化代碼執(zhí)行中");
}
}
三、實(shí)戰(zhàn)
要求實(shí)現(xiàn)一個(gè)用戶(hù)與AI助手對(duì)話(huà)交互表,要求一個(gè)用戶(hù)一天最多能對(duì)話(huà)200次,并且為了控制并發(fā)量,每個(gè)用戶(hù)在一分鐘之內(nèi)最多進(jìn)行對(duì)話(huà)十次。
3.1 創(chuàng)建一個(gè)交互表
CREATE TABLE user_request_log (
user_id BIGINT NOT NULL,
request_date DATE NOT NULL,
total_requests INT DEFAULT 200,
minute_requests INT DEFAULT 10,
minute_start_time DATETIME,
PRIMARY KEY (user_id, request_date),
INDEX idx_minute_start_time (minute_start_time)
);user_id: 用戶(hù)ID,作為主鍵的一部分,類(lèi)型為BIGINT。request_date: 當(dāng)天的日期,作為主鍵的一部分,類(lèi)型為DATE。total_requests: 當(dāng)天的總請(qǐng)求次數(shù),類(lèi)型為INT,默認(rèn)值為0。minute_requests: 當(dāng)前分鐘的請(qǐng)求次數(shù),類(lèi)型為INT,默認(rèn)值為0。minute_start_time: 當(dāng)前分鐘開(kāi)始的時(shí)間戳,類(lèi)型為DATETIME。- 主鍵由
user_id和request_date組成,以確保每個(gè)用戶(hù)每天的記錄唯一。 - 添加了一個(gè)索引
idx_minute_start_time以加快按minute_start_time查詢(xún)的速度。
3.2 引入mybatis-plus 并配置數(shù)據(jù)庫(kù)
依賴(lài):
<!-- 數(shù)據(jù)庫(kù)依賴(lài)-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.5</version>
<exclusions>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>application.yml
spring:
# 數(shù)據(jù)源配置
datasource:
url: jdbc:mysql://localhost:3306/ap_security?characterEncoding=utf-8&serverTimezone=UTC
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
application:
name: SpringTask使用mybatis-plus快速生成實(shí)體與架構(gòu)

3.3 模擬訪(fǎng)問(wèn)的Controller
@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {
@Autowired
UserRequestLogMapper userRequestLogMapper;
// 模擬進(jìn)行對(duì)話(huà)
@GetMapping("/chat")
public String Chat(){
UserRequestLog userRequestLog = userRequestLogMapper.selectById(1);
// 當(dāng)有次數(shù)時(shí)候才能進(jìn)行對(duì)話(huà)
if (userRequestLog.getTotalRequests()>0 && userRequestLog.getMinuteRequests()>0){
// 減去數(shù)量
userRequestLog.setMinuteRequests(userRequestLog.getMinuteRequests()-1);
userRequestLog.setTotalRequests(userRequestLog.getTotalRequests()-1);
userRequestLogMapper.updateById(userRequestLog);
return "對(duì)話(huà)成功";
}else {
return "您暫時(shí)已經(jīng)沒(méi)有對(duì)話(huà)次數(shù)了";
}
}
}3.4 設(shè)置定時(shí)任務(wù)
@Component
@Slf4j
public class springTaskTest {
@Autowired
UserRequestLogMapper userRequestLogMapper;
// 每一分鐘執(zhí)行一次
@Scheduled(cron = "0 0/1 * * * ?")
public void AutoTask(){
log.info("執(zhí)行增加分鐘對(duì)話(huà)次數(shù)");
UserRequestLog userRequestLog = userRequestLogMapper.selectById(1);
userRequestLog.setMinuteRequests(20);
userRequestLogMapper.updateById(userRequestLog);
}
// 每天凌晨3點(diǎn)執(zhí)行一次
@Scheduled(cron = "0 0 3 * * ?")
public void DayAuto(){
log.info("執(zhí)行增加天數(shù)的總次數(shù)");
UserRequestLog userRequestLog = userRequestLogMapper.selectById(1);
userRequestLog.setTotalRequests(200);
userRequestLogMapper.updateById(userRequestLog);
}
}測(cè)試:


進(jìn)行增加分鐘次數(shù):


到此這篇關(guān)于Java SpringTask定時(shí)自動(dòng)化處理的文章就介紹到這了,更多相關(guān)Java SpringTask定時(shí)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在springboot中添加mvc功能的正確姿勢(shì)講解
這篇文章主要介紹了在springboot中添加mvc功能的正確姿勢(shì),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
Java使用Catcher捕獲異常的實(shí)現(xiàn)
本文主要介紹了Java使用Catcher捕獲異常的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05
SpringBoot如何動(dòng)態(tài)改變?nèi)罩炯?jí)別
這篇文章主要介紹了SpringBoot如何動(dòng)態(tài)改變?nèi)罩炯?jí)別,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下2020-12-12
idea上提交項(xiàng)目到gitee 最后出現(xiàn) Push rejected的問(wèn)題處理方法
這篇文章主要介紹了idea上面提交項(xiàng)目到gitee 最后出現(xiàn) Push rejected的問(wèn)題處理方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09

