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

COVERAGE SUMMARY FOR SOURCE FILE [SimpleJobLauncher.java]

nameclass, %method, %block, %line, %
SimpleJobLauncher.java100% (2/2)100% (9/9)86%  (176/204)90%  (36/40)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class SimpleJobLauncher$1100% (1/1)100% (3/3)73%  (76/104)71%  (10/14)
run (): void 100% (1/1)68%  (49/72)62%  (5/8)
rethrow (Throwable): void 100% (1/1)71%  (12/17)80%  (4/5)
SimpleJobLauncher$1 (SimpleJobLauncher, Job, JobParameters, JobExecution): void 100% (1/1)100% (15/15)100% (1/1)
     
class SimpleJobLauncher100% (1/1)100% (6/6)100% (100/100)100% (27/27)
<static initializer> 100% (1/1)100% (4/4)100% (1/1)
SimpleJobLauncher (): void 100% (1/1)100% (3/3)100% (1/1)
afterPropertiesSet (): void 100% (1/1)100% (20/20)100% (5/5)
run (Job, JobParameters): JobExecution 100% (1/1)100% (65/65)100% (16/16)
setJobRepository (JobRepository): void 100% (1/1)100% (4/4)100% (2/2)
setTaskExecutor (TaskExecutor): void 100% (1/1)100% (4/4)100% (2/2)

1/*
2 * Copyright 2006-2008 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.launch.support;
17 
18import org.apache.commons.logging.Log;
19import org.apache.commons.logging.LogFactory;
20import org.springframework.batch.core.BatchStatus;
21import org.springframework.batch.core.ExitStatus;
22import org.springframework.batch.core.Job;
23import org.springframework.batch.core.JobExecution;
24import org.springframework.batch.core.JobInstance;
25import org.springframework.batch.core.JobParameters;
26import org.springframework.batch.core.JobParametersInvalidException;
27import org.springframework.batch.core.launch.JobLauncher;
28import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
29import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
30import org.springframework.batch.core.repository.JobRepository;
31import org.springframework.batch.core.repository.JobRestartException;
32import org.springframework.beans.factory.InitializingBean;
33import org.springframework.core.task.SyncTaskExecutor;
34import org.springframework.core.task.TaskExecutor;
35import org.springframework.core.task.TaskRejectedException;
36import org.springframework.util.Assert;
37 
38/**
39 * Simple implementation of the {@link JobLauncher} interface. The Spring Core
40 * {@link TaskExecutor} interface is used to launch a {@link Job}. This means
41 * that the type of executor set is very important. If a
42 * {@link SyncTaskExecutor} is used, then the job will be processed
43 * <strong>within the same thread that called the launcher.</strong> Care should
44 * be taken to ensure any users of this class understand fully whether or not
45 * the implementation of TaskExecutor used will start tasks synchronously or
46 * asynchronously. The default setting uses a synchronous task executor.
47 * 
48 * There is only one required dependency of this Launcher, a
49 * {@link JobRepository}. The JobRepository is used to obtain a valid
50 * JobExecution. The Repository must be used because the provided {@link Job}
51 * could be a restart of an existing {@link JobInstance}, and only the
52 * Repository can reliably recreate it.
53 * 
54 * @author Lucas Ward
55 * @Author Dave Syer
56 * 
57 * @since 1.0
58 * 
59 * @see JobRepository
60 * @see TaskExecutor
61 */
62public class SimpleJobLauncher implements JobLauncher, InitializingBean {
63 
64        protected static final Log logger = LogFactory.getLog(SimpleJobLauncher.class);
65 
66        private JobRepository jobRepository;
67 
68        private TaskExecutor taskExecutor;
69 
70        /**
71         * Run the provided job with the given {@link JobParameters}. The
72         * {@link JobParameters} will be used to determine if this is an execution
73         * of an existing job instance, or if a new one should be created.
74         * 
75         * @param job the job to be run.
76         * @param jobParameters the {@link JobParameters} for this particular
77         * execution.
78         * @return JobExecutionAlreadyRunningException if the JobInstance already
79         * exists and has an execution already running.
80         * @throws JobRestartException if the execution would be a re-start, but a
81         * re-start is either not allowed or not needed.
82         * @throws JobInstanceAlreadyCompleteException if this instance has already
83         * completed successfully
84         * @throws JobParametersInvalidException
85         */
86        public JobExecution run(final Job job, final JobParameters jobParameters)
87                        throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException,
88                        JobParametersInvalidException {
89 
90                Assert.notNull(job, "The Job must not be null.");
91                Assert.notNull(jobParameters, "The JobParameters must not be null.");
92 
93                final JobExecution jobExecution;
94                JobExecution lastExecution = jobRepository.getLastJobExecution(job.getName(), jobParameters);
95                if (lastExecution != null) {
96                        if (!job.isRestartable()) {
97                                throw new JobRestartException("JobInstance already exists and is not restartable");
98                        }
99                }
100 
101                // Check the validity of the parameters before doing creating anything
102                // in the repository...
103                job.getJobParametersValidator().validate(jobParameters);
104 
105                /*
106                 * There is a very small probability that a non-restartable job can be
107                 * restarted, but only if another process or thread manages to launch
108                 * <i>and</i> fail a job execution for this instance between the last
109                 * assertion and the next method returning successfully.
110                 */
111                jobExecution = jobRepository.createJobExecution(job.getName(), jobParameters);
112 
113                try {
114                        taskExecutor.execute(new Runnable() {
115 
116                                public void run() {
117                                        try {
118                                                logger.info("Job: [" + job + "] launched with the following parameters: [" + jobParameters
119                                                                + "]");
120                                                job.execute(jobExecution);
121                                                logger.info("Job: [" + job + "] completed with the following parameters: [" + jobParameters
122                                                                + "] and the following status: [" + jobExecution.getStatus() + "]");
123                                        }
124                                        catch (Throwable t) {
125                                                logger.info("Job: [" + job
126                                                                + "] failed unexpectedly and fatally with the following parameters: [" + jobParameters
127                                                                + "]", t);
128                                                rethrow(t);
129                                        }
130                                }
131 
132                                private void rethrow(Throwable t) {
133                                        if (t instanceof RuntimeException) {
134                                                throw (RuntimeException) t;
135                                        }
136                                        else if (t instanceof Error) {
137                                                throw (Error) t;
138                                        }
139                                        throw new IllegalStateException(t);
140                                }
141                        });
142                }
143                catch (TaskRejectedException e) {
144                        jobExecution.upgradeStatus(BatchStatus.FAILED);
145                        if (jobExecution.getExitStatus().equals(ExitStatus.UNKNOWN)) {
146                                jobExecution.setExitStatus(ExitStatus.FAILED.addExitDescription(e));
147                        }
148                        jobRepository.update(jobExecution);
149                }
150 
151                return jobExecution;
152        }
153 
154        /**
155         * Set the JobRepsitory.
156         * 
157         * @param jobRepository
158         */
159        public void setJobRepository(JobRepository jobRepository) {
160                this.jobRepository = jobRepository;
161        }
162 
163        /**
164         * Set the TaskExecutor. (Optional)
165         * 
166         * @param taskExecutor
167         */
168        public void setTaskExecutor(TaskExecutor taskExecutor) {
169                this.taskExecutor = taskExecutor;
170        }
171 
172        /**
173         * Ensure the required dependencies of a {@link JobRepository} have been
174         * set.
175         */
176        public void afterPropertiesSet() throws Exception {
177                Assert.state(jobRepository != null, "A JobRepository has not been set.");
178                if (taskExecutor == null) {
179                        logger.info("No TaskExecutor has been set, defaulting to synchronous executor.");
180                        taskExecutor = new SyncTaskExecutor();
181                }
182        }
183 
184}

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