1 | /* |
2 | * Copyright 2006-2012 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.step; |
18 | |
19 | import org.apache.commons.logging.Log; |
20 | import org.apache.commons.logging.LogFactory; |
21 | import org.springframework.batch.core.JobInterruptedException; |
22 | import org.springframework.batch.core.StepExecution; |
23 | |
24 | /** |
25 | * Policy that checks the current thread to see if it has been interrupted. |
26 | * |
27 | * @author Lucas Ward |
28 | * @author Dave Syer |
29 | * |
30 | */ |
31 | public class ThreadStepInterruptionPolicy implements StepInterruptionPolicy { |
32 | |
33 | protected static final Log logger = LogFactory.getLog(ThreadStepInterruptionPolicy.class); |
34 | |
35 | /** |
36 | * Returns if the current job lifecycle has been interrupted by checking if |
37 | * the current thread is interrupted. |
38 | */ |
39 | @Override |
40 | public void checkInterrupted(StepExecution stepExecution) throws JobInterruptedException { |
41 | |
42 | if (isInterrupted(stepExecution)) { |
43 | throw new JobInterruptedException("Job interrupted status detected."); |
44 | } |
45 | |
46 | } |
47 | |
48 | /** |
49 | * @param stepExecution the current context |
50 | * @return true if the job has been interrupted |
51 | */ |
52 | private boolean isInterrupted(StepExecution stepExecution) { |
53 | boolean interrupted = Thread.currentThread().isInterrupted(); |
54 | if (interrupted) { |
55 | logger.info("Step interrupted through Thread API"); |
56 | } |
57 | else { |
58 | interrupted = stepExecution.isTerminateOnly(); |
59 | if (interrupted) { |
60 | logger.info("Step interrupted through StepExecution"); |
61 | } |
62 | } |
63 | return interrupted; |
64 | } |
65 | |
66 | } |