【个人技术经验及开发技巧分享】 【个人技术经验及开发技巧分享】
首页
  • 操作系统初识
  • JAVA基础
  • JVM
  • 开发框架
  • Redis
  • Zookeeper
  • 消息中间件
  • 持久化
  • 算法
  • 网络
  • 系统架构
  • 并发编程
  • 框架
  • 开发杂货
  • 线上排查
  • 技巧备忘
  • 部署指南
  • 版本管理
  • 工作流程
  • 发版流程
  • 友情链接
  • 网站备忘
  • 在线工具
  • 学习
  • 各种云
  • 应用下载

Louis

首页
  • 操作系统初识
  • JAVA基础
  • JVM
  • 开发框架
  • Redis
  • Zookeeper
  • 消息中间件
  • 持久化
  • 算法
  • 网络
  • 系统架构
  • 并发编程
  • 框架
  • 开发杂货
  • 线上排查
  • 技巧备忘
  • 部署指南
  • 版本管理
  • 工作流程
  • 发版流程
  • 友情链接
  • 网站备忘
  • 在线工具
  • 学习
  • 各种云
  • 应用下载
  • 开发杂货

    • ES查询压测
    • Spring Cloud Stream
    • 线上Tomcat配置参考
    • 配置Prometheus及健康检测
    • Feign支持BasicAuth验证
    • Feign远程调用
    • Hystrix单方法熔断配置
    • 邮件发送自定义Excel
    • 本地开发联调配置
    • RabbitMQ配置备忘
    • Nacos配置中心
    • Java代码杂记
    • Oracle脚本备忘
    • Mysql并发数与连接数
    • 批量算费本地工具类
      • 1 工具类
      • 2 读取文件(示例)
    • Apollo配置模糊查询
    • 异步任务AsyncIAE
    • 生产环境机器配置参考
  • 线上排查

  • 技巧备忘

  • 部署指南

  • 技术应用
  • 开发杂货
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
Mysql并发数与连接数
Apollo配置模糊查询

← Mysql并发数与连接数 Apollo配置模糊查询→

最近更新
01
SpringBoot
10-21
02
Spring
10-20
03
Sentinel
10-14
更多文章>
Copyright © 2022-2023 Louis | 粤ICP备2022060093号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式