2 * ================================================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ================================================================================
20 package org.openecomp.portalapp.portal.utils;
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.concurrent.Callable;
25 import java.util.concurrent.ExecutorService;
26 import java.util.concurrent.Executors;
27 import java.util.concurrent.Future;
29 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
31 public abstract class ParallelExecutor<T> {
33 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ParallelExecutor.class);
35 protected static abstract class ThreadOperation<T> {
36 public abstract T execute(List<Object> parms);
39 protected abstract ThreadOperation<T> getThreadOperation();
41 private static class CallableOperationThread<T> implements Callable<T> {
45 private ThreadOperation<T> operation;
47 public CallableOperationThread(ThreadOperation<T> operation, List<Object> parms) {
49 this.operation = operation;
53 public T call() throws Exception {
54 return this.operation.execute(this.parms);
59 public List<T> performAllOperations(int ThreadPoolSize, List<List<Object>> listOfParms) {
60 List<T> result = new ArrayList<T>();
61 if (ThreadPoolSize > 0 && listOfParms != null) {
62 ExecutorService executor = Executors.newFixedThreadPool(ThreadPoolSize);
63 List<Future<T>> list = new ArrayList<Future<T>>();
64 for (List<Object> parms : listOfParms) {
65 CallableOperationThread<T> getter = new CallableOperationThread<T>(this.getThreadOperation(), parms);
66 Future<T> submit = executor.submit(getter);
69 for (Future<T> future : list) {
72 result.add(future.get());
74 } catch (Exception e) {
75 logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));