-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
1286 lines (479 loc) · 77.6 KB
/
index.html
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="google-site-verification" content="FQ4s-NjVsojslMD8_pxEjX8DvHIf5l9M6jnGMxHAqzU" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">
<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />
<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />
<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />
<link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">
<link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">
<link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">
<link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">
<meta name="keywords" content="Hexo, NexT" />
<meta property="og:type" content="website">
<meta property="og:title" content="漠北空城">
<meta property="og:url" content="http://yuechang.github.io/index.html">
<meta property="og:site_name" content="漠北空城">
<meta property="og:locale" content="zh-Hans">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="漠北空城">
<script type="text/javascript" id="hexo.configurations">
var NexT = window.NexT || {};
var CONFIG = {
root: '/',
scheme: 'Gemini',
version: '5.1.4',
sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
fancybox: true,
tabs: true,
motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
duoshuo: {
userId: '0',
author: '博主'
},
algolia: {
applicationID: 'BBGGBMEBHY',
apiKey: 'a8034b03f1ed5ebe16b4fc52a5d16939',
indexName: 'prod_blog',
hits: {"per_page":10},
labels: {"input_placeholder":"搜索","hits_empty":"没有找到任何结果关于: ${query}","hits_stats":"找到 ${hits} 个结果,耗时 ${time} ms"}
}
};
</script>
<link rel="canonical" href="http://yuechang.github.io/"/>
<title>漠北空城</title>
</head>
<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">
<div class="container sidebar-position-left
page-home">
<div class="headband"></div>
<header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
<div class="header-inner"><div class="site-brand-wrapper">
<div class="site-meta ">
<div class="custom-logo-site-title">
<a href="/" class="brand" rel="start">
<span class="logo-line-before"><i></i></span>
<span class="site-title">漠北空城</span>
<span class="logo-line-after"><i></i></span>
</a>
</div>
<p class="site-subtitle">听海观潮</p>
</div>
<div class="site-nav-toggle">
<button>
<span class="btn-bar"></span>
<span class="btn-bar"></span>
<span class="btn-bar"></span>
</button>
</div>
</div>
<nav class="site-nav">
<ul id="menu" class="menu">
<li class="menu-item menu-item-home">
<a href="/" rel="section">
<i class="menu-item-icon fa fa-fw fa-home"></i> <br />
首页
</a>
</li>
<li class="menu-item menu-item-about">
<a href="/about.html" rel="section">
<i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
关于
</a>
</li>
<li class="menu-item menu-item-tags">
<a href="/tags/" rel="section">
<i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
标签
</a>
</li>
<li class="menu-item menu-item-archives">
<a href="/archives/" rel="section">
<i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
归档
</a>
</li>
<li class="menu-item menu-item-search">
<a href="javascript:;" class="popup-trigger">
<i class="menu-item-icon fa fa-search fa-fw"></i> <br />
搜索
</a>
</li>
</ul>
<div class="site-search">
<div class="algolia-popup popup search-popup">
<div class="algolia-search">
<div class="algolia-search-input-icon">
<i class="fa fa-search"></i>
</div>
<div class="algolia-search-input" id="algolia-search-input"></div>
</div>
<div class="algolia-results">
<div id="algolia-stats"></div>
<div id="algolia-hits"></div>
<div id="algolia-pagination" class="algolia-pagination"></div>
</div>
<span class="popup-btn-close">
<i class="fa fa-times-circle"></i>
</span>
</div>
</div>
</nav>
</div>
</header>
<main id="main" class="main">
<div class="main-inner">
<div class="content-wrap">
<div id="content" class="content">
<section id="posts" class="posts-expand">
<article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
<div class="post-block">
<link itemprop="mainEntityOfPage" href="http://yuechang.github.io/2023/01/02/CompletableFuture使用/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="漠北空城">
<meta itemprop="description" content="">
<meta itemprop="image" content="/portrait.jpg">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="漠北空城">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2023/01/02/CompletableFuture使用/" itemprop="url">CompletableFuture使用</a></h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建于" itemprop="dateCreated datePublished" datetime="2023-01-02T12:00:37+08:00">
2023-01-02
</time>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p>本文地址:<a href="https://www.yuech.net/2023/01/02/CompletableFuture%E4%BD%BF%E7%94%A8/" target="_blank" rel="noopener">CompletableFuture使用</a></p>
<h3 id="1、自定义线程池配置类"><a href="#1、自定义线程池配置类" class="headerlink" title="1、自定义线程池配置类"></a>1、自定义线程池配置类</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* Copyright © 2022 Yuech and/or its affiliates. All rights reserved. */</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">package</span> ...;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> lombok.Data;</span><br><span class="line"><span class="keyword">import</span> org.springframework.boot.context.properties.ConfigurationProperties;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 定时任务线程池配置</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> Yuech</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span> 1.0</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@since</span> 2022-11-24 16:01</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Data</span></span><br><span class="line"><span class="meta">@ConfigurationProperties</span>(prefix = <span class="string">"common.pool"</span>)</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CommonThreadPoolProperties</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 线程池是否可用</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">boolean</span> enabled = <span class="keyword">true</span>;</span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 核心线程数</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">int</span> corePoolSize = <span class="number">20</span>;</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 最大线程数</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">int</span> maxPoolSize = <span class="number">40</span>;</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 线程池维护线程所允许的空闲时间</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">int</span> keepAliveSeconds = <span class="number">300</span>;</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 队列最大长度</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">int</span> queueCapacity = -<span class="number">1</span>;</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 线程名前缀</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">private</span> String threadNamePrefix = <span class="string">"Common-Execute-"</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h3 id="2、自定义线程池"><a href="#2、自定义线程池" class="headerlink" title="2、自定义线程池"></a>2、自定义线程池</h3><p>使用CallerRunsPolicy线程池丢弃策略,如果线程池满了,将会交给主线程执行<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* Copyright © 2022 Yuech and/or its affiliates. All rights reserved. */</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.annotation.Autowired;</span><br><span class="line"><span class="keyword">import</span> org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;</span><br><span class="line"><span class="keyword">import</span> org.springframework.boot.context.properties.EnableConfigurationProperties;</span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.Bean;</span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.ComponentScan;</span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.Configuration;</span><br><span class="line"><span class="keyword">import</span> org.springframework.core.task.AsyncTaskExecutor;</span><br><span class="line"><span class="keyword">import</span> org.springframework.scheduling.annotation.EnableAsync;</span><br><span class="line"><span class="keyword">import</span> org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.concurrent.ThreadPoolExecutor;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 线程池配置</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> Yuech</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span> 1.0</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@since</span> 2022-11-24 15:57</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="meta">@EnableAsync</span></span><br><span class="line"><span class="meta">@ConditionalOnProperty</span>(prefix = <span class="string">"common.pool"</span>, value = <span class="string">"enabled"</span>, matchIfMissing = <span class="keyword">true</span>)</span><br><span class="line"><span class="meta">@EnableConfigurationProperties</span>({CommonThreadPoolProperties.class})</span><br><span class="line"><span class="meta">@ComponentScan</span>(value = <span class="string">"com.yuech.config"</span>)</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CommonThreadPoolConfig</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Autowired</span></span><br><span class="line"> <span class="keyword">private</span> CommonThreadPoolProperties properties;</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Bean</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> ThreadPoolTaskExecutor <span class="title">commonAsyncTaskExecutor</span><span class="params">()</span> </span>{</span><br><span class="line"> ThreadPoolTaskExecutor executor = <span class="keyword">new</span> ThreadPoolTaskExecutor();</span><br><span class="line"> <span class="comment">// 核心线程池大小</span></span><br><span class="line"> executor.setCorePoolSize(properties.getCorePoolSize());</span><br><span class="line"> <span class="comment">// 最大线程数</span></span><br><span class="line"> executor.setMaxPoolSize(properties.getMaxPoolSize());</span><br><span class="line"> <span class="comment">// 队列容量</span></span><br><span class="line"> executor.setQueueCapacity(properties.getQueueCapacity());</span><br><span class="line"> <span class="comment">// 活跃时间</span></span><br><span class="line"> executor.setKeepAliveSeconds(properties.getKeepAliveSeconds());</span><br><span class="line"> <span class="comment">// 线程名字前缀</span></span><br><span class="line"> executor.setThreadNamePrefix(properties.getThreadNamePrefix());</span><br><span class="line"></span><br><span class="line"> <span class="comment">/*</span></span><br><span class="line"><span class="comment"> * setRejectedExecutionHandler:当pool已经达到max size的时候,如何处理新任务</span></span><br><span class="line"><span class="comment"> * CallerRunsPolicy:不在新线程中执行任务,而是由调用者所在的线程来执行</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> executor.setRejectedExecutionHandler(<span class="keyword">new</span> ThreadPoolExecutor.CallerRunsPolicy());</span><br><span class="line"> executor.initialize();</span><br><span class="line"> <span class="keyword">return</span> executor;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line">}</span><br></pre></td></tr></table></figure></p>
<h3 id="3、CompletableFuture调用"><a href="#3、CompletableFuture调用" class="headerlink" title="3、CompletableFuture调用"></a>3、CompletableFuture调用</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> ...;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> lombok.extern.slf4j.Slf4j;</span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.BeanUtils;</span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.annotation.Autowired;</span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.annotation.Qualifier;</span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.annotation.Value;</span><br><span class="line"><span class="keyword">import</span> org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;</span><br><span class="line"><span class="keyword">import</span> org.springframework.stereotype.Service;</span><br><span class="line"><span class="keyword">import</span> org.springframework.transaction.annotation.Transactional;</span><br><span class="line"><span class="keyword">import</span> org.springframework.util.CollectionUtils;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> javax.annotation.Resource;</span><br><span class="line"><span class="keyword">import</span> java.time.LocalDateTime;</span><br><span class="line"><span class="keyword">import</span> java.util.HashMap;</span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"><span class="keyword">import</span> java.util.Map;</span><br><span class="line"><span class="keyword">import</span> java.util.concurrent.CompletableFuture;</span><br><span class="line"><span class="keyword">import</span> java.util.concurrent.ExecutionException;</span><br><span class="line"><span class="keyword">import</span> java.util.stream.Collectors;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 日志服务实现类</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> Yuech</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@since</span> 2022-10-20 17:01</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Slf</span>4j</span><br><span class="line"><span class="meta">@Service</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">AccessLogServiceImpl</span> <span class="keyword">extends</span> <span class="title">ServiceImpl</span><<span class="title">AccessLogMapper</span>, <span class="title">AccessLog</span>> <span class="keyword">implements</span> <span class="title">AccessLogService</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Resource</span></span><br><span class="line"> <span class="keyword">private</span> AccessLogInfoService infoService;</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Resource</span></span><br><span class="line"> <span class="keyword">private</span> AccessLogManager accessLogManager;</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Resource</span>(name = <span class="string">"commonAsyncTaskExecutor"</span>)</span><br><span class="line"> <span class="keyword">private</span> ThreadPoolTaskExecutor commonAsyncTaskExecutor;</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Transactional</span>(rollbackFor = Exception.class)</span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> Result<AccessLogPushResponseDTO> <span class="title">resendOrPushAgain</span><span class="params">(List<AccessLogVO> accessLogList)</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="keyword">if</span> (CollectionUtils.isEmpty(accessLogList)) {</span><br><span class="line"> <span class="keyword">return</span> Result.success(<span class="keyword">new</span> AccessLogPushResponseDTO(CommonConstant.ZERO_LONG,</span><br><span class="line"> CommonConstant.ZERO_LONG, <span class="keyword">null</span>));</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">// 重新下发或重新推送</span></span><br><span class="line"> List<CompletableFuture<AccessLogPushResponseDetailDTO>> responseFutureList =</span><br><span class="line"> accessLogList.stream().map(accessLog -> doResendOrPushAgain(accessLog)).collect(Collectors.toList());</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 使用allOf方法来表示所有的并行任务</span></span><br><span class="line"> CompletableFuture<Void> allFutures =</span><br><span class="line"> CompletableFuture</span><br><span class="line"> .allOf(responseFutureList.toArray(<span class="keyword">new</span> CompletableFuture[responseFutureList.size()]));</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 下面的方法可以帮助我们获得所有子任务的处理结果</span></span><br><span class="line"> CompletableFuture<List<AccessLogPushResponseDetailDTO>> finalResults = allFutures</span><br><span class="line"> .thenApply(v -> responseFutureList.stream().map(accountFindingFuture -> accountFindingFuture.join())</span><br><span class="line"> .collect(Collectors.toList()));</span><br><span class="line"></span><br><span class="line"> AccessLogPushResponseDTO responseDto = <span class="keyword">new</span> AccessLogPushResponseDTO();</span><br><span class="line"> Result<AccessLogPushResponseDTO> result = Result.success(responseDto);</span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> List<AccessLogPushResponseDetailDTO> responseDetailList = finalResults.get();</span><br><span class="line"> <span class="keyword">if</span> (CollectionUtils.isEmpty(responseDetailList)) {</span><br><span class="line"> <span class="keyword">return</span> Result.success(<span class="keyword">new</span> AccessLogPushResponseDTO(CommonConstant.ZERO_LONG,</span><br><span class="line"> CommonConstant.ZERO_LONG, <span class="keyword">null</span>));</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> Long successCount = CommonConstant.ZERO_LONG;</span><br><span class="line"> Long failureCount = CommonConstant.ZERO_LONG;</span><br><span class="line"> <span class="keyword">for</span> (AccessLogPushResponseDetailDTO dto : responseDetailList) {</span><br><span class="line"></span><br><span class="line"> <span class="keyword">if</span> (dto == <span class="keyword">null</span>) {</span><br><span class="line"> <span class="keyword">continue</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span> (AccessResponseStatus.SUCCESS.getStatus().equals(dto.getResult())) {</span><br><span class="line"> successCount += <span class="number">1</span>;</span><br><span class="line"> } <span class="keyword">else</span> <span class="keyword">if</span> (AccessResponseStatus.FAIL.getStatus().equals(dto.getResult())) {</span><br><span class="line"> failureCount += <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> responseDto.setDetailList(responseDetailList);</span><br><span class="line"> responseDto.setSuccessCount(successCount);</span><br><span class="line"> responseDto.setFailureCount(failureCount);</span><br><span class="line"> } <span class="keyword">catch</span> (InterruptedException e) {</span><br><span class="line"> log.error(<span class="string">"resendOrPushAgain InterruptedException异常"</span>, e);</span><br><span class="line"> result = Result.DefaultFailure(<span class="string">"resendOrPushAgain ExecutionException异常 : "</span> + e.getMessage());</span><br><span class="line"> } <span class="keyword">catch</span> (ExecutionException e) {</span><br><span class="line"> log.error(<span class="string">"resendOrPushAgain ExecutionException异常"</span>, e);</span><br><span class="line"> result = Result.DefaultFailure(<span class="string">"resendOrPushAgain ExecutionException异常 : "</span> + e.getMessage());</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> result;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">private</span> CompletableFuture<AccessLogPushResponseDetailDTO> <span class="title">doResendOrPushAgain</span><span class="params">(AccessLogVO accessLog)</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> CompletableFuture.supplyAsync(() -> {</span><br><span class="line"> AccessLogAndDetailDTO dto = getAccessLogAndDetailInfo(accessLog);</span><br><span class="line"> <span class="keyword">if</span> (dto == <span class="keyword">null</span>) {</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">new</span> AccessLogPushResponseDetailDTO(<span class="keyword">null</span>, AccessResponseStatus.FAIL.getStatus(),</span><br><span class="line"> AccessLogConstants.SEND_OR_PUSH_ACCESS_LOG_IS_NULL);</span><br><span class="line"> }</span><br><span class="line"> HttpReq httpReq = <span class="keyword">null</span>;</span><br><span class="line"> String config = dto.getConfig();</span><br><span class="line"> String id = dto.getId();</span><br><span class="line"> <span class="keyword">if</span> (StringUtils.isNotBlank(config)) {</span><br><span class="line"> httpReq = JacksonUtils.toObj(config, HttpReq.class);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span> (httpReq == <span class="keyword">null</span>) {</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">new</span> AccessLogPushResponseDetailDTO(id, AccessResponseStatus.FAIL.getStatus(),</span><br><span class="line"> AccessLogConstants.SEND_OR_PUSH_ACCESS_LOG_CONFIG_IS_NULL);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">// ...</span></span><br><span class="line"></span><br><span class="line"> Integer result = AccessResponseStatus.SUCCESS.getStatus();</span><br><span class="line"> AccessLogDto accessLogDto = <span class="keyword">new</span> AccessLogDto();</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">new</span> AccessLogPushResponseDetailDTO(id, result, accessLogDto.getRes());</span><br><span class="line"> }, commonAsyncTaskExecutor);</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h3 id="4、参考"><a href="#4、参考" class="headerlink" title="4、参考"></a>4、参考</h3><p><a href="http://events.jianshu.io/p/8c9dc192fa63" target="_blank" rel="noopener">http://events.jianshu.io/p/8c9dc192fa63</a></p>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</div>
</article>
<article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
<div class="post-block">
<link itemprop="mainEntityOfPage" href="http://yuechang.github.io/2022/04/21/Jsch+Hutool实现文件上传与远程命令执行/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="漠北空城">
<meta itemprop="description" content="">
<meta itemprop="image" content="/portrait.jpg">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="漠北空城">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2022/04/21/Jsch+Hutool实现文件上传与远程命令执行/" itemprop="url">Jsch+Hutool实现文件上传与远程命令执行</a></h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建于" itemprop="dateCreated datePublished" datetime="2022-04-21T14:12:08+08:00">
2022-04-21
</time>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p>本文地址:<a href="https://www.yuech.net//2022/04/21/Jsch+Hutool%E5%AE%9E%E7%8E%B0%E6%96%87%E4%BB%B6%E4%B8%8A%E4%BC%A0%E4%B8%8E%E8%BF%9C%E7%A8%8B%E5%91%BD%E4%BB%A4%E6%89%A7%E8%A1%8C/" target="_blank" rel="noopener">Jsch+Hutool实现文件上传与远程命令执行</a></p>
<p>环境信息:<br>JDK版本:JDK_1.8<br>Jsch版本:0.1.5<br>Hutool版本:5.7.21</p>
<hr>
<h3 id="1、添加maven依赖"><a href="#1、添加maven依赖" class="headerlink" title="1、添加maven依赖"></a>1、添加maven依赖</h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">groupId</span>></span>com.jcraft<span class="tag"></<span class="name">groupId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">artifactId</span>></span>jsch<span class="tag"></<span class="name">artifactId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">version</span>></span>0.1.55<span class="tag"></<span class="name">version</span>></span></span><br><span class="line"><span class="tag"></<span class="name">dependency</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"><<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">groupId</span>></span>cn.hutool<span class="tag"></<span class="name">groupId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">artifactId</span>></span>hutool-all<span class="tag"></<span class="name">artifactId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">version</span>></span>5.7.21<span class="tag"></<span class="name">version</span>></span></span><br><span class="line"><span class="tag"></<span class="name">dependency</span>></span></span><br></pre></td></tr></table></figure>
<h3 id="2、文件上传"><a href="#2、文件上传" class="headerlink" title="2、文件上传"></a>2、文件上传</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* Copyright © 2022 Yuech and/or its affiliates. All rights reserved. */</span></span><br><span class="line"><span class="keyword">package</span> com.yc.apollo.test;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> cn.hutool.core.io.FileUtil;</span><br><span class="line"><span class="keyword">import</span> cn.hutool.core.io.IoUtil;</span><br><span class="line"><span class="keyword">import</span> cn.hutool.core.util.StrUtil;</span><br><span class="line"><span class="keyword">import</span> cn.hutool.extra.ftp.AbstractFtp;</span><br><span class="line"><span class="keyword">import</span> cn.hutool.extra.ssh.JschUtil;</span><br><span class="line"><span class="keyword">import</span> cn.hutool.extra.ssh.Sftp;</span><br><span class="line"><span class="keyword">import</span> com.jcraft.jsch.*;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.File;</span><br><span class="line"><span class="keyword">import</span> java.util.HashMap;</span><br><span class="line"><span class="keyword">import</span> java.util.Map;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> Yuech</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span> 1.0</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@since</span> 2022-01-10 11:30</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">FileUploadTest</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="comment">//本地文件路径</span></span><br><span class="line"> String localFile = <span class="string">"d:\\user\\yuechang\\desktop\\test.txt"</span>; </span><br><span class="line"> <span class="comment">//上传到远程的文件路径,要保证登录用户有写权限</span></span><br><span class="line"> String remoteFile = <span class="string">"/app/deploy"</span>; </span><br><span class="line"> Session session = <span class="keyword">null</span>;</span><br><span class="line"> Sftp sftp = <span class="keyword">null</span>;</span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> session = JschUtil.openSession(HostInfoConstants.REMOTE_HOST,</span><br><span class="line"> HostInfoConstants.REMOTE_PORT,</span><br><span class="line"> HostInfoConstants.USERNAME,</span><br><span class="line"> HostInfoConstants.PASSWORD);</span><br><span class="line"></span><br><span class="line"> sftp = <span class="keyword">new</span> Sftp(session, AbstractFtp.DEFAULT_CHARSET);</span><br><span class="line"> String normalize = FileUtil.normalize(remoteFile);</span><br><span class="line"> sftp.upload(normalize, <span class="keyword">new</span> File(localFile));</span><br><span class="line"></span><br><span class="line"> } <span class="keyword">finally</span> {</span><br><span class="line"> IoUtil.close(sftp);</span><br><span class="line"> }</span><br><span class="line"> System.out.println(<span class="string">"上传成功"</span>);</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h3 id="3、远程命令执行"><a href="#3、远程命令执行" class="headerlink" title="3、远程命令执行"></a>3、远程命令执行</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* Copyright © 2022 Yuech and/or its affiliates. All rights reserved. */</span></span><br><span class="line"><span class="keyword">package</span> com.yc.apollo.test;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> cn.hutool.core.io.IoUtil;</span><br><span class="line"><span class="keyword">import</span> cn.hutool.core.util.CharsetUtil;</span><br><span class="line"><span class="keyword">import</span> cn.hutool.core.util.StrUtil;</span><br><span class="line"><span class="keyword">import</span> cn.hutool.extra.ssh.ChannelType;</span><br><span class="line"><span class="keyword">import</span> cn.hutool.extra.ssh.JschUtil;</span><br><span class="line"><span class="keyword">import</span> com.jcraft.jsch.ChannelExec;</span><br><span class="line"><span class="keyword">import</span> com.jcraft.jsch.JSchException;</span><br><span class="line"><span class="keyword">import</span> com.jcraft.jsch.Session;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"><span class="keyword">import</span> java.io.InputStream;</span><br><span class="line"><span class="keyword">import</span> java.nio.charset.Charset;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 远程命令执行测试类</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> Yuech</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span> 1.0</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@since</span> 2022-04-13 9:36</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CmdExecTest</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> String cmd = <span class="string">"mv /app/deploy/test.txt /app/deploy/test_20220413.txt"</span>;</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException, JSchException </span>{</span><br><span class="line"></span><br><span class="line"> Charset charset = CharsetUtil.CHARSET_UTF_8;</span><br><span class="line"> Session session = JschUtil.createSession(HostInfoConstants.REMOTE_HOST,</span><br><span class="line"> HostInfoConstants.REMOTE_PORT,</span><br><span class="line"> HostInfoConstants.USERNAME,</span><br><span class="line"> HostInfoConstants.PASSWORD);</span><br><span class="line"></span><br><span class="line"> ChannelExec channel = (ChannelExec) JschUtil.createChannel(session, ChannelType.EXEC);</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 添加环境变量</span></span><br><span class="line"> channel.setCommand(cmd);</span><br><span class="line"> InputStream inputStream = channel.getInputStream();</span><br><span class="line"> InputStream errStream = channel.getErrStream();</span><br><span class="line"> channel.connect();</span><br><span class="line"></span><br><span class="line"> <span class="keyword">final</span> String[] error = <span class="keyword">new</span> String[<span class="number">1</span>];</span><br><span class="line"> <span class="keyword">final</span> String[] result = <span class="keyword">new</span> String[<span class="number">1</span>];</span><br><span class="line"> <span class="comment">//</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> System.out.println(error[<span class="number">0</span>] = IoUtil.read(errStream, charset));</span><br><span class="line"> } <span class="keyword">catch</span> (Exception e) {</span><br><span class="line"> e.printStackTrace();</span><br><span class="line"> <span class="keyword">if</span> (!StrUtil.contains(e.getMessage(), <span class="string">"Pipe closed"</span>)) {</span><br><span class="line"> <span class="comment">// DefaultSystemLog.getLog().error("读取 exec err 流发生异常", e);</span></span><br><span class="line"> error[<span class="number">0</span>] = <span class="string">"读取 exec err 流发生异常"</span> + e.getMessage();</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> result[<span class="number">0</span>] = IoUtil.read(inputStream, charset);</span><br><span class="line"> } <span class="keyword">catch</span> (Exception e) {</span><br><span class="line"> e.printStackTrace();</span><br><span class="line"> <span class="keyword">if</span> (!StrUtil.contains(e.getMessage(), <span class="string">"Pipe closed"</span>)) {</span><br><span class="line"> <span class="comment">//DefaultSystemLog.getLog().error("读取 exec 流发生异常", e);</span></span><br><span class="line"> result[<span class="number">0</span>] = <span class="string">"读取 exec 流发生异常"</span> + e.getMessage();</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> System.out.println(<span class="string">"结束 \n"</span> + result[<span class="number">0</span>] + <span class="string">" "</span> + error[<span class="number">0</span>]);</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h3 id="4、参考信息"><a href="#4、参考信息" class="headerlink" title="4、参考信息"></a>4、参考信息</h3><p><a href="https://github.com/dromara/Jpom" target="_blank" rel="noopener">jpom</a></p>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</div>
</article>
<article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
<div class="post-block">
<link itemprop="mainEntityOfPage" href="http://yuechang.github.io/2022/01/01/PDCA Cycle/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="漠北空城">
<meta itemprop="description" content="">
<meta itemprop="image" content="/portrait.jpg">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="漠北空城">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2022/01/01/PDCA Cycle/" itemprop="url">PDCA Cycle</a></h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建于" itemprop="dateCreated datePublished" datetime="2022-01-01T18:59:08+08:00">
2022-01-01
</time>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h2 id="PDCA-Cycle"><a href="#PDCA-Cycle" class="headerlink" title="PDCA Cycle"></a>PDCA Cycle</h2><blockquote>
<p><a href="https://www.yuech.net/2022/01/01/PDCA%20Cycle/" target="_blank" rel="noopener">PDCA Cycle</a></p>
</blockquote>
<h3 id="1、PDCA-释义"><a href="#1、PDCA-释义" class="headerlink" title="1、PDCA 释义"></a>1、PDCA 释义</h3><ul>
<li>P:Plan</li>
<li>D: Do</li>
<li>C:Check</li>
<li>A:Action</li>
</ul>
<h3 id="2、PDCA-Cycle循环"><a href="#2、PDCA-Cycle循环" class="headerlink" title="2、PDCA Cycle循环"></a>2、PDCA Cycle循环</h3><p><img src="https://asq.org/-/media/Images/Learn-About-Quality/PDCA-Cycle/pdca-cycle.png?h=180&w=216&la=en" alt="PDCA Cycle循环"></p>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</div>
</article>
<article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
<div class="post-block">
<link itemprop="mainEntityOfPage" href="http://yuechang.github.io/2021/09/11/TCP的三次握手和四次挥手/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="漠北空城">
<meta itemprop="description" content="">
<meta itemprop="image" content="/portrait.jpg">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="漠北空城">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2021/09/11/TCP的三次握手和四次挥手/" itemprop="url">TCP的三次握手和四次挥手</a></h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建于" itemprop="dateCreated datePublished" datetime="2021-09-11T16:59:08+08:00">
2021-09-11
</time>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h2 id="TCP的三次握手和四次挥手"><a href="#TCP的三次握手和四次挥手" class="headerlink" title="TCP的三次握手和四次挥手"></a>TCP的三次握手和四次挥手</h2><blockquote>
<p><a href="https://www.yuech.net/2021/09/11/TCP%E7%9A%84%E4%B8%89%E6%AC%A1%E6%8F%A1%E6%89%8B%E5%92%8C%E5%9B%9B%E6%AC%A1%E6%8C%A5%E6%89%8B/" target="_blank" rel="noopener">TCP的三次握手和四次挥手</a></p>
</blockquote>
<h3 id="1、TCP头部的规范定义"><a href="#1、TCP头部的规范定义" class="headerlink" title="1、TCP头部的规范定义"></a>1、TCP头部的规范定义</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">0</span> <span class="number">15</span>|<span class="number">16</span> <span class="number">31</span></span><br><span class="line">+------------------------------------------+------------------------------------------+</span><br><span class="line">| <span class="number">16</span>位源端口号 | <span class="number">16</span>位目的端口号 |</span><br><span class="line">+------------------------------------------+------------------------------------------+</span><br><span class="line">| <span class="number">32</span>位序号/<span class="function">Sequence <span class="title">number</span> <span class="params">(seq)</span> |</span></span><br><span class="line"><span class="function">+------------------------------------------+------------------------------------------+</span></span><br><span class="line"><span class="function">| 32位确认号/Acknowledge <span class="title">number</span> <span class="params">(ack)</span> |</span></span><br><span class="line"><span class="function">+------------------------------------------+------------------------------------------+</span></span><br><span class="line"><span class="function">|4位头部信息|6位保留|URG|ACK|PSH|RST|SYN|FIN | 16位窗口大小 |</span></span><br><span class="line"><span class="function">+------------------------------------------+------------------------------------------+</span></span><br><span class="line"><span class="function">| 16位校验和 | 16位紧急指针 |</span></span><br><span class="line"><span class="function">+------------------------------------------+------------------------------------------+</span></span><br><span class="line"><span class="function">| 选项最多40字节 |</span></span><br><span class="line"><span class="function">+------------------------------------------+------------------------------------------+</span></span><br></pre></td></tr></table></figure>
<h3 id="2、TCP的三次握手:"><a href="#2、TCP的三次握手:" class="headerlink" title="2、TCP的三次握手:"></a>2、TCP的三次握手:</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Client(SYN_SENT) --> SYN=<span class="number">1</span>,seq=k --> Server(SYN_RCVD)</span><br><span class="line">Client(ESTABLISHED) <-- SYN=<span class="number">1</span>,ACK=<span class="number">1</span>,ack=k+<span class="number">1</span>,seq=m <-- Server(SYN_RCVD)</span><br><span class="line">Client(ESTABLISHED) --> ACK=<span class="number">1</span>,seq=m+<span class="number">1</span>,seq=l --> Server(ESTABLISHED)</span><br></pre></td></tr></table></figure>
<p>为什么需要三次握手:<br>能够避免server端资源的浪费。<br>1、当Client端发送连接请求并没有丢失,而是在某个网络接点上滞留了,以至于延迟到连接释放以后的某个节点才到达Server;<br>2、本来只是一个早已失效的报文段,Server端接收到之后误认为这是Client端的一个新的请求,于是发送了连接确认报文;<br>3、如果不采用”三次握手”,只要等Server发送确认,新的连接就已经建立了;<br>4、等待Client端发送数据,但是此时Client不会理睬Server端的确认,也不会向Server端发送数据;<br>5、但是Server端认为连接已经建立还在等着Client的数据,此时就会存在Server端资源的浪费;</p>
<h3 id="3、TCP的四次挥手"><a href="#3、TCP的四次挥手" class="headerlink" title="3、TCP的四次挥手"></a>3、TCP的四次挥手</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Client(FIN_WAIT_1) --> FIN=<span class="number">1</span>,seq=k --> Server(CLOSE_WAIT)</span><br><span class="line">Client(FIN_WAIT_2) <-- ACK=<span class="number">1</span>,ack=k+<span class="number">1</span>,seq=m <-- Server(CLOSE_WAIT)</span><br><span class="line">Client(FIN_WAIT_2) <-- FIN=<span class="number">1</span>,seq=i <-- Server(LAST_ACK)</span><br><span class="line">Client(TIME_WAIT/<span class="number">2</span>MSL) --> ACK=<span class="number">1</span>,ack=i+<span class="number">1</span>,seq=l --> Server(CLOSE)</span><br></pre></td></tr></table></figure>
<h3 id="4、为什么需要四次挥手"><a href="#4、为什么需要四次挥手" class="headerlink" title="4、为什么需要四次挥手"></a>4、为什么需要四次挥手</h3><p>首先TCP是一种面向连接的、可靠的、基于字节流的运输层通讯协议,TCP是全双工模式<br>建立连接时,通过SYN+ACK能够确定双方均在线即可正常收发报文;<br>由于双工模式,假设Client端没有需要发送的报文了,于是发送FIN报文,Server端收到后给出了ACK确认报文;<br>但是有可能Server端还存在需要发送的报文,这个时候就需要Server端发送一次FIN报文,让Client端来确认</p>
<h3 id="5、为什么需要等待2MSL?"><a href="#5、为什么需要等待2MSL?" class="headerlink" title="5、为什么需要等待2MSL?"></a>5、为什么需要等待2MSL?</h3><p>MSL:报文段最大生存时间,它是任何报文在丢弃前在网络内的最长时间。<br>第一点:保证TCP全双工能够正常可靠关闭;<br>第二点:保证这次连续的重复数据段从网络中消失;</p>
<h3 id="6、参考博客"><a href="#6、参考博客" class="headerlink" title="6、参考博客"></a>6、参考博客</h3><blockquote>
<p><a href="https://www.cnblogs.com/onesea/p/13053697.html" target="_blank" rel="noopener">一文彻底搞懂 TCP三次握手、四次挥手过程及原理</a></p>
</blockquote>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</div>
</article>
<article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
<div class="post-block">
<link itemprop="mainEntityOfPage" href="http://yuechang.github.io/2020/04/11/UML类图六种关系说明与Java代码展示/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="漠北空城">
<meta itemprop="description" content="">
<meta itemprop="image" content="/portrait.jpg">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="漠北空城">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2020/04/11/UML类图六种关系说明与Java代码展示/" itemprop="url">UML类图六种关系说明与Java代码展示</a></h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建于" itemprop="dateCreated datePublished" datetime="2020-04-11T23:39:45+08:00">
2020-04-11
</time>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p>UML类图六种关系说明与Java代码展示</p>
<p>工具信息</p>
<ul>
<li>Enterprise Architect 12</li>
</ul>
<h3 id="1、UML类图六种关系"><a href="#1、UML类图六种关系" class="headerlink" title="1、UML类图六种关系"></a>1、UML类图六种关系</h3><ul>
<li>1、泛化关系(generalization)</li>
<li>2、实现关系(realize)</li>
<li>3、聚合关系(aggregation)</li>
<li>4、组合关系(composition) </li>
<li>5、关联关系(association)</li>
<li>6、依赖关系(dependency)</li>
</ul>
<p>这六种关系以及对应的符合是否了解呢,下面我们通过一张很经典的例子来展示一下。</p>
<h3 id="2、例子"><a href="#2、例子" class="headerlink" title="2、例子"></a>2、例子</h3><p><img src="https://github.com/raw/yuechang/blog-comment/master/blog-picture/uml-class-diagram.png" alt="uml-class-diagram"></p>
<ul>
<li>Vehicle类为一个抽象类</li>
<li>Vehicle类下面有两个继承类:Car和Bicycly,它们之间的关系为实现关系,使用带空心箭头的虚线表示;</li>
<li>Car与SUV之间也是继承关系,它们之间的关系为泛化关系,使用带空心的实线表示;</li>
<li>Car与Engine之间是组合关系,使用实心菱形的实线表示;</li>
<li>Student与Class之间是聚合关系,使用空心菱形的实线表示;</li>
<li>Student与IdCard之间为关联关系,使用实线表示;</li>
<li>Student上学需要骑Bicycle,与Bicycle是一种依赖关系,使用带箭头的虚线表示;</li>
</ul>
<p>下面详细介绍这六种关系。</p>
<h3 id="3、类之间的关系与Java代码展示"><a href="#3、类之间的关系与Java代码展示" class="headerlink" title="3、类之间的关系与Java代码展示"></a>3、类之间的关系与Java代码展示</h3><p>他们可以分为三组来区别对比记忆</p>
<ul>
<li>实现关系(realize)和泛化关系(generalization)</li>
<li>聚合关系(aggregation)和组合关系(composition) </li>
<li>关联关系(association)和依赖关系(dependency)</li>
</ul>
<!--noindex-->
<div class="post-button text-center">
<a class="btn" href="/2020/04/11/UML类图六种关系说明与Java代码展示/#more" rel="contents">
阅读全文 »
</a>
</div>
<!--/noindex-->
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</div>
</article>
</section>
<nav class="pagination">
<span class="page-number current">1</span><a class="page-number" href="/page/2/">2</a><span class="space">…</span><a class="page-number" href="/page/14/">14</a><a class="extend next" rel="next" href="/page/2/"><i class="fa fa-angle-right"></i></a>
</nav>
</div>
</div>
<div class="sidebar-toggle">
<div class="sidebar-toggle-line-wrap">
<span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
<span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
<span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
</div>
</div>
<aside id="sidebar" class="sidebar">
<div class="sidebar-inner">
<section class="site-overview-wrap sidebar-panel sidebar-panel-active">
<div class="site-overview">
<div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
<img class="site-author-image" itemprop="image"
src="/portrait.jpg"
alt="漠北空城" />
<p class="site-author-name" itemprop="name">漠北空城</p>
<p class="site-description motion-element" itemprop="description"></p>
</div>
<nav class="site-state motion-element">
<div class="site-state-item site-state-posts">
<a href="/archives/">
<span class="site-state-item-count">69</span>
<span class="site-state-item-name">日志</span>
</a>
</div>