| /** |
| * |
| * Copyright (c) 2011, 2018 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany) |
| * |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License 2.0 |
| * which accompanies this distribution, and is available at |
| * https://www.eclipse.org/legal/epl-2.0/ |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| * Contributors: |
| * Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation |
| * |
| */ |
| package org.eclipse.osbp.xtext.signal.common; |
| |
| import java.util.ArrayList; |
| import java.util.concurrent.ExecutorService; |
| import java.util.concurrent.Executors; |
| import java.util.concurrent.FutureTask; |
| |
| import org.eclipse.osbp.xtext.datainterchange.common.WorkerThreadRunnable; |
| import org.quartz.Job; |
| import org.quartz.JobExecutionContext; |
| import org.quartz.JobExecutionException; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| public class OSBPSignalJob extends OSBPSignalCommonData implements Job { |
| /** the log */ |
| private Logger log = LoggerFactory.getLogger("scheduler"); |
| /** the executor service for the processing of all job's tasks*/ |
| private ExecutorService executorService = Executors.newCachedThreadPool(); |
| |
| /** Default constructor is needed for quartz.*/ |
| public OSBPSignalJob() {} |
| |
| @Override |
| /** |
| * Triggers the job's logic. |
| */ |
| public void execute(JobExecutionContext context) throws JobExecutionException { |
| log.info("Execution of ["+context.getJobDetail().getKey()+"] ..."); |
| if(context.getJobDetail().getJobDataMap().getBoolean(OSBPSignalConstants.SEQUENTIAL)){ |
| executeTasksSequentially((String)context.getJobDetail().getJobDataMap().get(OSBPSignalConstants.TASKSLIST)); |
| }else{ |
| executeTasksParallel((String)context.getJobDetail().getJobDataMap().get(OSBPSignalConstants.TASKSLIST)); |
| } |
| } |
| |
| /** |
| * Executes the list of tasks sequentially. |
| * @param handlername handler name, having the list of tasks to be executed |
| * @return true if the execution was successful, false if not |
| */ |
| public void executeTasksSequentially(String handlername) { |
| log.info("Start - Sequential execution of list of tasks from handler ["+handlername+"] ..."); |
| |
| for (WorkerThreadRunnable interchange : getListOfTasks().get(handlername)) { |
| try { |
| executorService.submit(interchange).get(); |
| } catch (Exception e) { |
| log.error("Execution - " + interchange.getDirection() + " for " + interchange.getName() |
| + " interupted!\n" + e.getMessage()); |
| } |
| } |
| log.info("End - Sequential execution of list of tasks from handler ["+handlername+"] ..."); |
| } |
| |
| /** |
| * Executes the list of tasks in parallel fashion. |
| * @param handlername handler name, having the list of tasks to be executed |
| */ |
| @SuppressWarnings("unchecked") |
| public void executeTasksParallel(String handlername) { |
| log.info("Start - Parallel execution of list of tasks from handler ["+handlername+"] ..."); |
| |
| boolean allTasksDone = false; |
| ArrayList<FutureTask<WorkerThreadRunnable>> tasks = new ArrayList<>(); |
| for (WorkerThreadRunnable dataint : getListOfTasks().get(handlername)) { |
| tasks.add((FutureTask<WorkerThreadRunnable>) executorService.submit(dataint)); |
| } |
| while (!allTasksDone) { |
| allTasksDone = allTasksDone(tasks); |
| } |
| if (allTasksDone) { |
| log.info("All tasks done!"); |
| } |
| log.info("End - Parallel execution of list of tasks from handler ["+handlername+"] ..."); |
| } |
| |
| } |