1 | /* |
2 | * Copyright 2006-2009 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 | */ |
16 | |
17 | package org.springframework.batch.core.job.flow; |
18 | |
19 | import org.springframework.batch.core.BatchStatus; |
20 | import org.springframework.batch.core.ExitStatus; |
21 | import org.springframework.batch.core.JobExecution; |
22 | import org.springframework.batch.core.JobInterruptedException; |
23 | import org.springframework.batch.core.StartLimitExceededException; |
24 | import org.springframework.batch.core.Step; |
25 | import org.springframework.batch.core.StepExecution; |
26 | import org.springframework.batch.core.job.StepHandler; |
27 | import org.springframework.batch.core.repository.JobRepository; |
28 | import org.springframework.batch.core.repository.JobRestartException; |
29 | |
30 | /** |
31 | * Implementation of {@link FlowExecutor} for use in components that need to |
32 | * execute a flow related to a {@link JobExecution}. |
33 | * |
34 | * @author Dave Syer |
35 | * |
36 | */ |
37 | public class JobFlowExecutor implements FlowExecutor { |
38 | |
39 | private final ThreadLocal<StepExecution> stepExecutionHolder = new ThreadLocal<StepExecution>(); |
40 | |
41 | private final JobExecution execution; |
42 | |
43 | private ExitStatus exitStatus = ExitStatus.EXECUTING; |
44 | |
45 | private final StepHandler stepHandler; |
46 | |
47 | private final JobRepository jobRepository; |
48 | |
49 | /** |
50 | * @param execution |
51 | */ |
52 | public JobFlowExecutor(JobRepository jobRepository, StepHandler stepHandler, JobExecution execution) { |
53 | this.jobRepository = jobRepository; |
54 | this.stepHandler = stepHandler; |
55 | this.execution = execution; |
56 | stepExecutionHolder.set(null); |
57 | } |
58 | |
59 | public String executeStep(Step step) throws JobInterruptedException, JobRestartException, |
60 | StartLimitExceededException { |
61 | StepExecution stepExecution = stepHandler.handleStep(step, execution); |
62 | stepExecutionHolder.set(stepExecution); |
63 | if (stepExecution == null) { |
64 | return ExitStatus.COMPLETED.getExitCode(); |
65 | } |
66 | if (stepExecution.isTerminateOnly()) { |
67 | throw new JobInterruptedException("Step requested termination: "+stepExecution, stepExecution.getStatus()); |
68 | } |
69 | return stepExecution.getExitStatus().getExitCode(); |
70 | } |
71 | |
72 | public void abandonStepExecution() { |
73 | StepExecution lastStepExecution = stepExecutionHolder.get(); |
74 | if (lastStepExecution != null && lastStepExecution.getStatus().isGreaterThan(BatchStatus.STOPPING)) { |
75 | lastStepExecution.upgradeStatus(BatchStatus.ABANDONED); |
76 | jobRepository.update(lastStepExecution); |
77 | } |
78 | } |
79 | |
80 | public void updateJobExecutionStatus(FlowExecutionStatus status) { |
81 | execution.setStatus(findBatchStatus(status)); |
82 | exitStatus = exitStatus.and(new ExitStatus(status.getName())); |
83 | execution.setExitStatus(exitStatus); |
84 | } |
85 | |
86 | public JobExecution getJobExecution() { |
87 | return execution; |
88 | } |
89 | |
90 | public StepExecution getStepExecution() { |
91 | return stepExecutionHolder.get(); |
92 | } |
93 | |
94 | public void close(FlowExecution result) { |
95 | stepExecutionHolder.set(null); |
96 | } |
97 | |
98 | public boolean isRestart() { |
99 | if (getStepExecution() != null && getStepExecution().getStatus() == BatchStatus.ABANDONED) { |
100 | /* |
101 | * This is assumed to be the last step execution and it was marked |
102 | * abandoned, so we are in a restart of a stopped step. |
103 | */ |
104 | // TODO: mark the step execution in some more definitive way? |
105 | return true; |
106 | } |
107 | return execution.getStepExecutions().isEmpty(); |
108 | } |
109 | |
110 | public void addExitStatus(String code) { |
111 | exitStatus = exitStatus.and(new ExitStatus(code)); |
112 | } |
113 | |
114 | /** |
115 | * @param status |
116 | * @return |
117 | */ |
118 | private BatchStatus findBatchStatus(FlowExecutionStatus status) { |
119 | for (BatchStatus batchStatus : BatchStatus.values()) { |
120 | if (status.getName().startsWith(batchStatus.toString())) { |
121 | return batchStatus; |
122 | } |
123 | } |
124 | return BatchStatus.UNKNOWN; |
125 | } |
126 | |
127 | } |