2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org
.argeo
.slc
.core
.execution
;
19 import java
.util
.ArrayList
;
20 import java
.util
.Collections
;
21 import java
.util
.HashSet
;
22 import java
.util
.List
;
25 import org
.apache
.commons
.logging
.Log
;
26 import org
.apache
.commons
.logging
.LogFactory
;
27 import org
.argeo
.slc
.SlcException
;
28 import org
.argeo
.slc
.execution
.ExecutionModulesManager
;
29 import org
.argeo
.slc
.execution
.ExecutionProcess
;
30 import org
.argeo
.slc
.process
.RealizedFlow
;
31 import org
.argeo
.slc
.process
.SlcExecution
;
33 /** Thread of the SLC Process, starting the sub executions. */
34 public class ProcessThread
extends Thread
{
35 private final static Log log
= LogFactory
.getLog(ProcessThread
.class);
37 private final ExecutionModulesManager executionModulesManager
;
38 private final ExecutionProcess process
;
39 private final ProcessThreadGroup processThreadGroup
;
41 private Set
<ExecutionThread
> executionThreads
= Collections
42 .synchronizedSet(new HashSet
<ExecutionThread
>());
44 private Boolean hadAnError
= false;
45 private Boolean killed
= false;
47 public ProcessThread(ThreadGroup processesThreadGroup
,
48 ExecutionModulesManager executionModulesManager
,
49 ExecutionProcess process
) {
50 super(processesThreadGroup
, "SLC Process #" + process
.getUuid());
51 this.executionModulesManager
= executionModulesManager
;
52 this.process
= process
;
53 processThreadGroup
= new ProcessThreadGroup(executionModulesManager
,
57 public final void run() {
58 log
.info("\n##\n## SLC Process #" + process
.getUuid()
61 String oldStatus
= process
.getStatus();
62 process
.setStatus(ExecutionProcess
.RUNNING
);
63 executionModulesManager
.dispatchUpdateStatus(process
, oldStatus
,
64 ExecutionProcess
.RUNNING
);
68 } catch (InterruptedException e
) {
73 // waits for all execution threads to complete (in case they were
74 // started asynchronously)
75 for (ExecutionThread executionThread
: executionThreads
) {
76 if (executionThread
.isAlive()) {
78 executionThread
.join();
79 } catch (InterruptedException e
) {
89 /** Make sure this is called BEFORE all the threads are interrupted. */
90 private void computeFinalStatus() {
91 String oldStatus
= process
.getStatus();
92 // TODO: error management at flow level?
94 process
.setStatus(ExecutionProcess
.KILLED
);
96 process
.setStatus(ExecutionProcess
.ERROR
);
98 process
.setStatus(ExecutionProcess
.COMPLETED
);
99 executionModulesManager
.dispatchUpdateStatus(process
, oldStatus
,
100 process
.getStatus());
101 log
.info("\n## SLC Process #" + process
.getUuid() + " "
102 + process
.getStatus() + "\n");
105 /** Called when being killed */
106 private synchronized void die() {
108 computeFinalStatus();
109 for (ExecutionThread executionThread
: executionThreads
) {
111 executionThread
.interrupt();
112 } catch (Exception e
) {
113 log
.error("Cannot interrupt " + executionThread
);
116 processThreadGroup
.interrupt();
120 * Implementation specific execution. To be overridden in order to deal with
121 * custom process types. Default expects an {@link SlcExecution}.
123 @SuppressWarnings("deprecation")
124 protected void process() throws InterruptedException
{
125 if (!(process
instanceof SlcExecution
))
126 throw new SlcException("Unsupported process type "
127 + process
.getClass());
128 SlcExecution slcExecution
= (SlcExecution
) process
;
129 List
<RealizedFlow
> flowsToProcess
= new ArrayList
<RealizedFlow
>();
130 flowsToProcess
.addAll(slcExecution
.getRealizedFlows());
132 while (flowsToProcess
.size() > 0) {
133 RealizedFlow realizedFlow
= flowsToProcess
.remove(0);
134 execute(realizedFlow
, true);
138 /** @return the (distinct) thread used for this execution */
139 protected final void execute(RealizedFlow realizedFlow
, Boolean synchronous
)
140 throws InterruptedException
{
144 ExecutionThread thread
= new ExecutionThread(this, realizedFlow
);
145 executionThreads
.add(thread
);
154 public void notifyError() {
158 public synchronized void flowCompleted() {
162 public ExecutionProcess
getProcess() {
166 public ProcessThreadGroup
getProcessThreadGroup() {
167 return processThreadGroup
;
170 public ExecutionModulesManager
getExecutionModulesManager() {
171 return executionModulesManager
;