16
16
17
17
package org .springframework .boot .build .bom .bomr ;
18
18
19
- import java .time .Duration ;
20
- import java .util .ArrayList ;
21
19
import java .util .Collection ;
20
+ import java .util .Collections ;
22
21
import java .util .List ;
23
22
import java .util .Map ;
24
23
import java .util .concurrent .ExecutionException ;
25
24
import java .util .concurrent .ExecutorService ;
26
25
import java .util .concurrent .Executors ;
27
26
import java .util .concurrent .Future ;
27
+ import java .util .stream .Collectors ;
28
+ import java .util .stream .Stream ;
28
29
29
30
import org .slf4j .Logger ;
30
31
import org .slf4j .LoggerFactory ;
31
32
32
33
import org .springframework .boot .build .bom .Library ;
33
- import org .springframework .boot .build .bom .UpgradePolicy ;
34
34
35
35
/**
36
- * Uses multiple threads to find library updates.
36
+ * {@link LibraryUpdateResolver} decorator that uses multiple threads to find library
37
+ * updates.
37
38
*
38
39
* @author Moritz Halbritter
40
+ * @author Andy Wilkinson
39
41
*/
40
- class MultithreadedLibraryUpdateResolver extends StandardLibraryUpdateResolver {
42
+ class MultithreadedLibraryUpdateResolver implements LibraryUpdateResolver {
41
43
42
44
private static final Logger LOGGER = LoggerFactory .getLogger (MultithreadedLibraryUpdateResolver .class );
43
45
44
46
private final int threads ;
45
47
46
- MultithreadedLibraryUpdateResolver (VersionResolver versionResolver , UpgradePolicy upgradePolicy , int threads ) {
47
- super (versionResolver , upgradePolicy );
48
+ private final LibraryUpdateResolver delegate ;
49
+
50
+ MultithreadedLibraryUpdateResolver (int threads , LibraryUpdateResolver delegate ) {
48
51
this .threads = threads ;
52
+ this .delegate = delegate ;
49
53
}
50
54
51
55
@ Override
@@ -54,34 +58,28 @@ public List<LibraryWithVersionOptions> findLibraryUpdates(Collection<Library> li
54
58
LOGGER .info ("Looking for updates using {} threads" , this .threads );
55
59
ExecutorService executorService = Executors .newFixedThreadPool (this .threads );
56
60
try {
57
- List <Future <LibraryWithVersionOptions >> jobs = new ArrayList <>();
58
- for (Library library : librariesToUpgrade ) {
59
- if (isLibraryExcluded (library )) {
60
- continue ;
61
- }
62
- jobs .add (executorService .submit (() -> {
63
- LOGGER .info ("Looking for updates for {}" , library .getName ());
64
- long start = System .nanoTime ();
65
- List <VersionOption > versionOptions = getVersionOptions (library , librariesByName );
66
- LOGGER .info ("Found {} updates for {}, took {}" , versionOptions .size (), library .getName (),
67
- Duration .ofNanos (System .nanoTime () - start ));
68
- return new LibraryWithVersionOptions (library , versionOptions );
69
- }));
70
- }
71
- List <LibraryWithVersionOptions > result = new ArrayList <>();
72
- for (Future <LibraryWithVersionOptions > job : jobs ) {
73
- try {
74
- result .add (job .get ());
75
- }
76
- catch (InterruptedException | ExecutionException ex ) {
77
- throw new RuntimeException (ex );
78
- }
79
- }
80
- return result ;
61
+ return librariesToUpgrade .stream ()
62
+ .map ((library ) -> executorService .submit (
63
+ () -> this .delegate .findLibraryUpdates (Collections .singletonList (library ), librariesByName )))
64
+ .flatMap (this ::getResult )
65
+ .collect (Collectors .toList ());
81
66
}
82
67
finally {
83
68
executorService .shutdownNow ();
84
69
}
85
70
}
86
71
72
+ private Stream <LibraryWithVersionOptions > getResult (Future <List <LibraryWithVersionOptions >> job ) {
73
+ try {
74
+ return job .get ().stream ();
75
+ }
76
+ catch (InterruptedException ex ) {
77
+ Thread .currentThread ().interrupt ();
78
+ throw new RuntimeException (ex );
79
+ }
80
+ catch (ExecutionException ex ) {
81
+ throw new RuntimeException (ex );
82
+ }
83
+ }
84
+
87
85
}
0 commit comments