Kevin's blog Kevin's blog
首页
  • AI基础
  • RAG技术
  • 提示词工程
  • Wireshark抓包
  • 常见问题
  • 数据库
  • 代码技巧
  • 浏览器
  • 手册教程
  • 技术应用
  • 流程规范
  • github技巧
  • git笔记
  • vpn笔记
  • 知识概念
  • 学习笔记
  • 环境搭建
  • linux&运维
  • 微服务
  • 经验技巧
  • 实用手册
  • arthas常用
  • spring应用
  • javaAgent技术
  • 网站
友情链接
  • 分类
  • 标签
  • 归档

Kevin

你可以迷茫,但不可以虚度
首页
  • AI基础
  • RAG技术
  • 提示词工程
  • Wireshark抓包
  • 常见问题
  • 数据库
  • 代码技巧
  • 浏览器
  • 手册教程
  • 技术应用
  • 流程规范
  • github技巧
  • git笔记
  • vpn笔记
  • 知识概念
  • 学习笔记
  • 环境搭建
  • linux&运维
  • 微服务
  • 经验技巧
  • 实用手册
  • arthas常用
  • spring应用
  • javaAgent技术
  • 网站
友情链接
  • 分类
  • 标签
  • 归档
  • 手册教程

  • 技术应用

    • 工作杂货

      • Jprofiler排查
      • ES查询压测
      • 阿尔萨斯(Arthas)
      • 定时任务
      • DruidDataSource配置
      • Spring Cloud Stream
      • 线上Tomcat配置参考
      • 配置Prometheus及健康检测
      • Feign远程调用
      • Hystrix单方法熔断配置
      • 本地开发联调配置
      • Java代码杂记
      • SQL脚本杂记
      • 批量算费本地工具类
        • 1 工具类
        • 2 读取文件(示例)
      • Apollo配置模糊查询
      • 开发问题记录
      • 机器配置参考
    • 技巧备忘

  • 流程规范

  • GitHub技巧

  • VPN

  • Git笔记

  • 实用手册
  • 技术应用
  • 工作杂货
luoxiaofeng
2022-05-11
目录

批量算费本地工具类

# 1 工具类

@Slf4j
public class Recost {

    //重算费请求接口
    private static final String URL = "https://xxx/xxx/xxx/reComCost";
    //算费接口请求头设置:token ------- 使用时替换生产用户登录的token
    private static final String HEADER_AUTHTOKEN = "263464be9c924309af15b7ec21d87739";
    //算费接口请求头设置:路由
    private static final String HEADER_ROUTENAME = "sendWaybillSite";

    //几个线程并发执行
    //所有运单号分成几组执行
    private static final int SPLIT_LEN = 6;
    //每次算费运单个数
    private static final int WAYBILL_COST_SIZE = 250;

    //读取的文件名  ------- 放resources目录下
    private static final String FILE_NAME = "waybillno.txt";

    private static ExecutorService executorService = Executors.newFixedThreadPool(SPLIT_LEN);

    public static void main(String[] args) {
        try {
            long l1 = System.currentTimeMillis();
            List<String> waybillStrList = loadWaybillNoParams();
            int len = (int) Math.ceil((double) waybillStrList.size() / SPLIT_LEN);
            List<List<String>> waybillGroupList = groupList(waybillStrList, len);

            List<CompletableFuture<Void>> futureList = new ArrayList<>();
            for (List<String> list : waybillGroupList) {
                CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
                    for (String waybillStr : list) {
                        String params = "waybillNos=" + waybillStr;
                        String response = sendPost(URL, params);
                        log.info(response);
                    }
                }, executorService);
                futureList.add(future);
            }
            CompletableFuture<Void> futureAll = CompletableFuture.allOf(futureList.stream().toArray(CompletableFuture[]::new));
            futureAll.join();
            long l2 = System.currentTimeMillis();

            log.info("接口耗时:{}", (l2 - l1));

            executorService.shutdown();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
     * List分割
     */
    public static List<List<String>> groupList(List<String> list, int len) {
        List<List<String>> listGroup = new ArrayList<>();
        int listSize = list.size();
        //子集合的长度
        int toIndex = 2;
        for (int i = 0; i < list.size(); i += len) {
            if (i + len > listSize) {
                len = listSize - i;
            }
            List<String> newList = list.subList(i, i + len);
            listGroup.add(newList);
        }
        return listGroup;
    }

    public static List<String> loadWaybillNoParams() throws IOException, URISyntaxException {
        List<String> resultList = new ArrayList<>();
        URI uri = ClassLoader.getSystemResource(FILE_NAME).toURI();
        List<String> list = Files.readAllLines(Paths.get(uri));
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            int pos = i + 1;
            sb.append(list.get(i));
            if (pos % WAYBILL_COST_SIZE == 0 || pos == list.size()) {
                resultList.add(sb.toString());
                sb = new StringBuilder();
            } else {
                sb.append(",");
            }
        }
        return resultList;
    }

    /**
     * @param url
     * @param params name1=value1&name2=value2
     * @return
     */
    public static String sendPost(String url, String params) {
        PrintWriter out;
        out = null;
        BufferedReader in = null;
        StringBuilder result = new StringBuilder();
        try {
            URL realUrl = new URL(url);
            // 打开和URL之间的连接
            URLConnection conn = realUrl.openConnection();
            // 设置通用的请求属性
            conn.setRequestProperty("authToken", HEADER_AUTHTOKEN);
            conn.setRequestProperty("routeName", HEADER_ROUTENAME);
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            out = new PrintWriter(conn.getOutputStream());
            // 发送请求参数
            out.print(params);
            // flush输出流的缓冲
            out.flush();
            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(
                    new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result.append(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //使用finally块来关闭输出流、输入流
        finally{
            try{
                if(out!=null){
                    out.close();
                }
                if(in!=null){
                    in.close();
                }
            }
            catch(IOException ex){
                ex.printStackTrace();
            }
        }
        return result.toString();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144

# 2 读取文件(示例)

waybillno.txt

abc300006078626
abc300006043651
abc300006201477
abc300006251049
1
2
3
4
上次更新: 2022/06/02, 11:20:10
SQL脚本杂记
Apollo配置模糊查询

← SQL脚本杂记 Apollo配置模糊查询→

最近更新
01
AI是如何学习的
06-05
02
chatGpt提示原则
06-05
03
提示词工程实践指南
06-05
更多文章>
| Copyright © 2022-2025 Kevin | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式