[PORTAL-7] Rebase
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / utils / ParallelExecutor.java
1 /*-\r
2  * ================================================================================\r
3  * ECOMP Portal\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ================================================================================\r
19  */\r
20 package org.openecomp.portalapp.portal.utils;\r
21 \r
22 import java.util.ArrayList;\r
23 import java.util.List;\r
24 import java.util.concurrent.Callable;\r
25 import java.util.concurrent.ExecutorService;\r
26 import java.util.concurrent.Executors;\r
27 import java.util.concurrent.Future;\r
28 \r
29 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;\r
30 \r
31 public abstract class ParallelExecutor<T> {\r
32 \r
33         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ParallelExecutor.class);\r
34 \r
35         protected static abstract class ThreadOperation<T> {\r
36                 public abstract T execute(List<Object> parms);\r
37         }\r
38 \r
39         protected abstract ThreadOperation<T> getThreadOperation();\r
40 \r
41         private static class CallableOperationThread<T> implements Callable<T> {\r
42 \r
43                 List<Object> parms;\r
44 \r
45                 private ThreadOperation<T> operation;\r
46 \r
47                 public CallableOperationThread(ThreadOperation<T> operation, List<Object> parms) {\r
48                         this.parms = parms;\r
49                         this.operation = operation;\r
50                 }\r
51 \r
52                 @Override\r
53                 public T call() throws Exception {\r
54                         return this.operation.execute(this.parms);\r
55                 }\r
56 \r
57         }\r
58 \r
59         public List<T> performAllOperations(int ThreadPoolSize, List<List<Object>> listOfParms) {\r
60                 List<T> result = new ArrayList<T>();\r
61                 if (ThreadPoolSize > 0 && listOfParms != null) {\r
62                         ExecutorService executor = Executors.newFixedThreadPool(ThreadPoolSize);\r
63                         List<Future<T>> list = new ArrayList<Future<T>>();\r
64                         for (List<Object> parms : listOfParms) {\r
65                                 CallableOperationThread<T> getter = new CallableOperationThread<T>(this.getThreadOperation(), parms);\r
66                                 Future<T> submit = executor.submit(getter);\r
67                                 list.add(submit);\r
68                         }\r
69                         for (Future<T> future : list) {\r
70                                 try {\r
71                                         if (future != null) {\r
72                                                 result.add(future.get());\r
73                                         }\r
74                                 } catch (Exception e) {\r
75                                         logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));\r
76                                 }\r
77                         }\r
78                         executor.shutdown();\r
79                 }\r
80                 return result;\r
81         }\r
82 \r
83 }\r