2 * ================================================================================
\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
11 * http://www.apache.org/licenses/LICENSE-2.0
\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
20 package org.openecomp.portalapp.portal.utils;
\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
29 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
\r
31 public abstract class ParallelExecutor<T> {
\r
33 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ParallelExecutor.class);
\r
35 protected static abstract class ThreadOperation<T> {
\r
36 public abstract T execute(List<Object> parms);
\r
39 protected abstract ThreadOperation<T> getThreadOperation();
\r
41 private static class CallableOperationThread<T> implements Callable<T> {
\r
45 private ThreadOperation<T> operation;
\r
47 public CallableOperationThread(ThreadOperation<T> operation, List<Object> parms) {
\r
49 this.operation = operation;
\r
53 public T call() throws Exception {
\r
54 return this.operation.execute(this.parms);
\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
69 for (Future<T> future : list) {
\r
71 if (future != null) {
\r
72 result.add(future.get());
\r
74 } catch (Exception e) {
\r
75 logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));
\r
78 executor.shutdown();
\r