EMMA Coverage Report (generated Thu Jan 24 13:37:04 CST 2013)
[all classes][org.springframework.batch.core.job.flow.support.state]

COVERAGE SUMMARY FOR SOURCE FILE [SplitState.java]

nameclass, %method, %block, %line, %
SplitState.java100% (2/2)100% (7/7)88%  (115/131)91%  (26.3/29)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class SplitState100% (1/1)100% (5/5)86%  (97/113)90%  (25.3/28)
handle (FlowExecutor): FlowExecutionStatus 100% (1/1)82%  (71/87)86%  (16.3/19)
SplitState (Collection, String): void 100% (1/1)100% (17/17)100% (5/5)
getFlows (): Collection 100% (1/1)100% (3/3)100% (1/1)
isEndState (): boolean 100% (1/1)100% (2/2)100% (1/1)
setTaskExecutor (TaskExecutor): void 100% (1/1)100% (4/4)100% (2/2)
     
class SplitState$1100% (1/1)100% (2/2)100% (18/18)100% (2/2)
SplitState$1 (SplitState, Flow, FlowExecutor): void 100% (1/1)100% (12/12)100% (1/1)
call (): FlowExecution 100% (1/1)100% (6/6)100% (1/1)

1/*
2 * Copyright 2006-2007 the original author or authors.
3 *
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
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16package org.springframework.batch.core.job.flow.support.state;
17 
18import java.util.ArrayList;
19import java.util.Collection;
20import java.util.concurrent.Callable;
21import java.util.concurrent.ExecutionException;
22import java.util.concurrent.Future;
23import java.util.concurrent.FutureTask;
24 
25import org.springframework.batch.core.job.flow.Flow;
26import org.springframework.batch.core.job.flow.FlowExecution;
27import org.springframework.batch.core.job.flow.FlowExecutionException;
28import org.springframework.batch.core.job.flow.FlowExecutionStatus;
29import org.springframework.batch.core.job.flow.FlowExecutor;
30import org.springframework.batch.core.job.flow.FlowHolder;
31import org.springframework.batch.core.job.flow.State;
32import org.springframework.core.task.SyncTaskExecutor;
33import org.springframework.core.task.TaskExecutor;
34import org.springframework.core.task.TaskRejectedException;
35 
36/**
37 * A {@link State} implementation that splits a {@link Flow} into multiple
38 * parallel subflows.
39 * 
40 * @author Dave Syer
41 * @since 2.0
42 */
43public class SplitState extends AbstractState implements FlowHolder {
44 
45        private final Collection<Flow> flows;
46 
47        private TaskExecutor taskExecutor = new SyncTaskExecutor();
48 
49        private FlowExecutionAggregator aggregator = new MaxValueFlowExecutionAggregator();
50 
51        /**
52         * @param name
53         */
54        public SplitState(Collection<Flow> flows, String name) {
55                super(name);
56                this.flows = flows;
57        }
58 
59        /**
60         * Public setter for the taskExecutor.
61         * @param taskExecutor the taskExecutor to set
62         */
63        public void setTaskExecutor(TaskExecutor taskExecutor) {
64                this.taskExecutor = taskExecutor;
65        }
66        
67        /**
68         * @return the flows
69         */
70        public Collection<Flow> getFlows() {
71                return flows;
72        }
73 
74        /**
75         * Execute the flows in parallel by passing them to the {@link TaskExecutor}
76         * and wait for all of them to finish before proceeding.
77         * 
78         * @see State#handle(FlowExecutor)
79         */
80        @Override
81        public FlowExecutionStatus handle(final FlowExecutor executor) throws Exception {
82 
83                // TODO: collect the last StepExecution from the flows as well, so they
84                // can be abandoned if necessary
85                Collection<Future<FlowExecution>> tasks = new ArrayList<Future<FlowExecution>>();
86 
87                for (final Flow flow : flows) {
88 
89                        final FutureTask<FlowExecution> task = new FutureTask<FlowExecution>(new Callable<FlowExecution>() {
90                                public FlowExecution call() throws Exception {
91                                        return flow.start(executor);
92                                }
93                        });
94 
95                        tasks.add(task);
96 
97                        try {
98                                taskExecutor.execute(task);
99                        }
100                        catch (TaskRejectedException e) {
101                                throw new FlowExecutionException("TaskExecutor rejected task for flow=" + flow.getName());
102                        }
103 
104                }
105 
106                Collection<FlowExecution> results = new ArrayList<FlowExecution>();
107 
108                // Could use a CompletionService here?
109                for (Future<FlowExecution> task : tasks) {
110                        try {
111                                results.add(task.get());
112                        }
113                        catch (ExecutionException e) {
114                                // Unwrap the expected exceptions
115                                Throwable cause = e.getCause();
116                                if (cause instanceof Exception) {
117                                        throw (Exception) cause;
118                                } else {
119                                        throw e;
120                                }
121                        }
122                }
123 
124                return aggregator.aggregate(results);
125 
126        }
127 
128        /*
129         * (non-Javadoc)
130         * 
131         * @see org.springframework.batch.core.job.flow.State#isEndState()
132         */
133        public boolean isEndState() {
134                return false;
135        }
136}

[all classes][org.springframework.batch.core.job.flow.support.state]
EMMA 2.0.5312 (C) Vladimir Roubtsov