View Javadoc
1   package org.apache.maven.plugins.invoker;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.LinkedHashMap;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.concurrent.Callable;
26  import java.util.concurrent.ExecutorService;
27  import java.util.concurrent.Executors;
28  import java.util.stream.Collectors;
29  
30  import org.apache.maven.plugins.invoker.model.BuildJob;
31  
32  /**
33   * Execute build jobs with parallel.
34   *
35   * @author Slawomir Jaranowski
36   */
37  class JobExecutor
38  {
39      interface ThrowableJobConsumer
40      {
41          void accept( BuildJob t ) throws Throwable;
42      }
43  
44      private final List<BuildJob> jobs;
45      private final int threadsCount;
46  
47      JobExecutor( List<BuildJob> jobs, int threadsCount )
48      {
49          this.jobs = jobs;
50          this.threadsCount = threadsCount;
51      }
52  
53      public void forEach( ThrowableJobConsumer jobConsumer )
54      {
55          // group and sort jobs by ordinal
56          Map<Integer, List<BuildJob>> groupedJobs = jobs.stream()
57              .sorted( ( j1, j2 ) -> Integer.compare( j2.getOrdinal(), j1.getOrdinal() ) )
58              .collect( Collectors.groupingBy( BuildJob::getOrdinal, LinkedHashMap::new, Collectors.toList() ) );
59  
60          ExecutorService executorService = Executors.newFixedThreadPool( threadsCount );
61  
62          groupedJobs.forEach( ( key, value ) ->
63          {
64              // prepare list of callable tasks
65              List<Callable<Void>> callableJobs = value.stream().map( buildJob -> (Callable<Void>) () ->
66              {
67                  try
68                  {
69                      jobConsumer.accept( buildJob );
70                  }
71                  catch ( Throwable e )
72                  {
73                      buildJob.setResult( BuildJob.Result.ERROR );
74                      buildJob.setFailureMessage( String.valueOf( e ) );
75                  }
76                  return null;
77              } ).collect( Collectors.toList() );
78  
79              try
80              {
81                  executorService.invokeAll( callableJobs );
82              }
83              catch ( InterruptedException e )
84              {
85                  Thread.currentThread().interrupt();
86                  throw new RuntimeException( e );
87              }
88          } );
89  
90          // all task are finished here
91          executorService.shutdownNow();
92      }
93  }