最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java?SpringTask定時(shí)自動(dòng)化處理方法

 更新時(shí)間:2024年08月02日 12:03:42   作者:Alphamilk  
這篇文章主要介紹了Java?SpringTask定時(shí)自動(dòng)化處理,通過自動(dòng)化,不僅可以提高工作效率和準(zhǔn)確性,還可以釋放人力資源以專注于更高價(jià)值的工作,需要的朋友可以參考下

一、自動(dòng)化處理

1.1 什么是自動(dòng)化處理

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

1.2 SpringTask介紹

二、SpringTask的基本使用

2.1 引入依賴

由于springTask 是SpringFramWork包的內(nèi)容,所以不需要進(jìn)行引入新的依賴。

2.2 通過控制臺加入注解啟用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 通過@Schedule(Cron表達(dá)式) 實(shí)現(xiàn)定時(shí)任務(wù)(每兩秒執(zhí)行一次)

@Component
@Slf4j
public class springTaskTest {
//    每兩秒執(zhí)行一次
    @Scheduled(cron = "0/2 * * * * ?")
    public void AutoTask(){
        log.info("自動(dòng)化代碼執(zhí)行中");
    }
}

三、實(shí)戰(zhàn)

要求實(shí)現(xiàn)一個(gè)用戶與AI助手對話交互表,要求一個(gè)用戶一天最多能對話200次,并且為了控制并發(fā)量,每個(gè)用戶在一分鐘之內(nèi)最多進(jìn)行對話十次。

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: 用戶ID,作為主鍵的一部分,類型為BIGINT。
  • request_date: 當(dāng)天的日期,作為主鍵的一部分,類型為DATE。
  • total_requests: 當(dāng)天的總請求次數(shù),類型為INT,默認(rèn)值為0。
  • minute_requests: 當(dāng)前分鐘的請求次數(shù),類型為INT,默認(rèn)值為0。
  • minute_start_time: 當(dāng)前分鐘開始的時(shí)間戳,類型為DATETIME
  • 主鍵由user_idrequest_date組成,以確保每個(gè)用戶每天的記錄唯一。
  • 添加了一個(gè)索引idx_minute_start_time以加快按minute_start_time查詢的速度。

3.2 引入mybatis-plus 并配置數(shù)據(jù)庫

依賴:

        <!--        數(shù)據(jù)庫依賴-->
        <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 模擬訪問的Controller

@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserRequestLogMapper userRequestLogMapper;
//    模擬進(jìn)行對話
    @GetMapping("/chat")
    public String Chat(){
        UserRequestLog userRequestLog = userRequestLogMapper.selectById(1);
//        當(dāng)有次數(shù)時(shí)候才能進(jìn)行對話
        if (userRequestLog.getTotalRequests()>0 && userRequestLog.getMinuteRequests()>0){
//            減去數(shù)量
            userRequestLog.setMinuteRequests(userRequestLog.getMinuteRequests()-1);
            userRequestLog.setTotalRequests(userRequestLog.getTotalRequests()-1);
            userRequestLogMapper.updateById(userRequestLog);
            return "對話成功";
        }else {
            return "您暫時(shí)已經(jīng)沒有對話次數(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í)行增加分鐘對話次數(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);
    }
}

測試:

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

到此這篇關(guān)于Java SpringTask定時(shí)自動(dòng)化處理的文章就介紹到這了,更多相關(guān)Java SpringTask定時(shí)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

镇江市| 巴楚县| 惠州市| 宜川县| 苍溪县| 金山区| 顺平县| 清镇市| 盐津县| 元朗区| 鹤庆县| 南部县| 福安市| 江源县| 石嘴山市| 梅河口市| 宜兰县| 门头沟区| 错那县| 永善县| 江源县| 阿坝| 名山县| 巩留县| 凤翔县| 沭阳县| 南澳县| 鲜城| 盐城市| 衡阳市| 唐海县| 龙岩市| 冀州市| 武隆县| 五家渠市| 凉城县| 嘉定区| 榕江县| 定州市| 双辽市| 泾阳县|