Java SpringTask定时自动化处理

目录

一、自动化处理

1.1 什么是自动化处理

1.2 SpringTask介绍

二、SpringTask的基本使用

2.1 引入依赖

2.2 通过控制台加入注解启用SpringTask

2.3 使用Cron表达式规定时间

2.4 通过@Schedule(Cron表达式) 实现定时任务(每两秒执行一次)

三、实战

3.1 创建一个交互表

3.2 引入mybatis-plus 并配置数据库

3.3 模拟访问的Controller

3.4 设置定时任务


一、自动化处理

1.1 什么是自动化处理

        自动化处理是指使用软件工具或程序自动执行原本需要人工干预的任务。这些任务可以是重复性的、耗时的或者需要高度准确性的操作。通过自动化,不仅可以提高工作效率和准确性,还可以释放人力资源以专注于更高价值的工作。

1.2 SpringTask介绍


二、SpringTask的基本使用

2.1 引入依赖

由于springTask 是SpringFramWork包的内容,所以不需要进行引入新的依赖。

2.2 通过控制台加入注解启用SpringTask

@SpringBootApplication
@EnableScheduling
public class SpringTaskApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringTaskApplication.class, args);
    }

}

2.3 使用Cron表达式规定时间

如果不会使用Cron表达式的使用可以直接使用cron的生成网站

https://cron.qqe2.com/

常用cron表达式:

2.4 通过@Schedule(Cron表达式) 实现定时任务(每两秒执行一次)

@Component
@Slf4j
public class springTaskTest {

//    每两秒执行一次
    @Scheduled(cron = "0/2 * * * * ?")
    public void AutoTask(){
        log.info("自动化代码执行中");
    }

}


三、实战

要求实现一个用户与AI助手对话交互表,要求一个用户一天最多能对话200次,并且为了控制并发量,每个用户在一分钟之内最多进行对话十次。

3.1 创建一个交互表

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: 当天的日期,作为主键的一部分,类型为DATE
  • total_requests: 当天的总请求次数,类型为INT,默认值为0。
  • minute_requests: 当前分钟的请求次数,类型为INT,默认值为0。
  • minute_start_time: 当前分钟开始的时间戳,类型为DATETIME
  • 主键由user_idrequest_date组成,以确保每个用户每天的记录唯一。
  • 添加了一个索引idx_minute_start_time以加快按minute_start_time查询的速度。

3.2 引入mybatis-plus 并配置数据库

依赖:

        <!--        数据库依赖-->
        <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:
  # 数据源配置
  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快速生成实体与架构

3.3 模拟访问的Controller

@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserRequestLogMapper userRequestLogMapper;

//    模拟进行对话
    @GetMapping("/chat")
    public String Chat(){
        UserRequestLog userRequestLog = userRequestLogMapper.selectById(1);

//        当有次数时候才能进行对话
        if (userRequestLog.getTotalRequests()>0 && userRequestLog.getMinuteRequests()>0){
//            减去数量
            userRequestLog.setMinuteRequests(userRequestLog.getMinuteRequests()-1);
            userRequestLog.setTotalRequests(userRequestLog.getTotalRequests()-1);
            userRequestLogMapper.updateById(userRequestLog);
            return "对话成功";
        }else {
            return "您暂时已经没有对话次数了";
        }

    }

}

3.4 设置定时任务

@Component
@Slf4j
public class springTaskTest {

    @Autowired
    UserRequestLogMapper userRequestLogMapper;


    //    每一分钟执行一次
    @Scheduled(cron = "0 0/1 * * * ?")
    public void AutoTask(){
        log.info("执行增加分钟对话次数");
        UserRequestLog userRequestLog = userRequestLogMapper.selectById(1);
        userRequestLog.setMinuteRequests(20);
        userRequestLogMapper.updateById(userRequestLog);

    }


    //    每天凌晨3点执行一次
    @Scheduled(cron = "0 0 3 * * ?")
    public  void DayAuto(){
        log.info("执行增加天数的总次数");
        UserRequestLog userRequestLog = userRequestLogMapper.selectById(1);
        userRequestLog.setTotalRequests(200);
        userRequestLogMapper.updateById(userRequestLog);
    }



}

测试:

 进行增加分钟次数:

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Alphamilk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值