springboot手写定时器

spring boot 手写定时器

  • spring-boot 手写定时器
    • 思路
    • 代码
      • 定时触发任务
      • 执行要处理的订单
      • 在spring boot 启动后执行两个线程
    • 启动
    • 结语

spring-boot 手写定时器

思路

电商系统,用户下单之后,未付款之前.
下单之后, 减去库存, 十分钟之内未付款,删除订单,恢复库存.
定时器和业务分离

代码

定时触发任务

package com.example.tedemo.thread;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;

import java.text.SimpleDateFormat;
import java.util.*;

public class Timing implements Runnable {


    private static Map<String, List<String>> timeMap = new HashMap<>();
	
    public static void addTime(String now_date, String order_num) {
        boolean flag = timeMap.containsKey(now_date);
        if (flag) {
            timeMap.get(now_date).add(order_num);
        } else {
            List<String> order_nums = new ArrayList<>();
            timeMap.put(now_date, order_nums);
        }
    }

    //如果订单付款, 就从这里删除
    public static void deleteTime(String key) {
        for (List list : timeMap.values()) {
            for (int i = list.size() - 1; i >= 0; i--) {
                if (key.equals(list.get(0))||key==list.get(0)){
                    list.remove(i);
                    return;
                }

            }
        }
    }
    @Override
    public void run() {
        while (true) {
            try {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// SimpleDateFormat 是线程不安全的
                for (String time : timeMap.keySet()) {
                    Date now_date = new Date();
                    String string_now_date = simpleDateFormat.format(now_date);
                    if (string_now_date.equals(time) || time == string_now_date) {  //到了当前时间后
                        Consumption.add_order_nums(timeMap.get(time));//执行 删除订单
                        timeMap.remove(time);//执行后删除key
                    }
                }
                Thread.sleep(900);// 定时 每零点九秒执行一次,   一秒执行一次可能会有意外
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

执行要处理的订单

package com.example.tedemo.thread;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Consumption implements Runnable {
    private static List<String> order_nums = new ArrayList<>();

    public static void add_order_nums(List<String> order_num) {
        order_nums.addAll(order_num);
    }

    @Override
    public void run() {
        while (true) {
            try {
                for (int i = order_nums.size() - 1; i >= 0; i--) {
                    System.out.println("移除订单 从数据库删除, 加库存");
                    order_nums.remove(i);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

在spring boot 启动后执行两个线程

package com.example.tedemo.configuration;

import com.example.tedemo.thread.Consumption;
import com.example.tedemo.thread.Timing;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Order(1)
public class StartThe implements ApplicationRunner {

    @Override
    public void run(ApplicationArguments args) throws Exception {
        Timing timing = new Timing();
        Consumption consumption = new Consumption();

        Thread thread_timing = new Thread(timing);
        Thread thread_consumption = new Thread(consumption);
        thread_timing.start();
        thread_consumption.start();
    }
}

启动

启动之后 调用Timing的静态方法addTime
到时间后自动会执行定时任务
springboot手写定时器_第1张图片
代码地址:https://gitee.com/gitxnm/tedemo.git
启动后访问http://localhost:7894/swagger-ui.html

结语

思想是 生产者 消费者, 没有实际用途.只是突然想写出来看一看

你可能感兴趣的:(java)