View Javadoc

1   /*
2    * Copyright 2012-2013 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  package org.springframework.batch.core.configuration.annotation;
17  
18  import javax.annotation.PostConstruct;
19  import javax.sql.DataSource;
20  
21  import org.springframework.batch.core.launch.JobLauncher;
22  import org.springframework.batch.core.launch.support.SimpleJobLauncher;
23  import org.springframework.batch.core.repository.JobRepository;
24  import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean;
25  import org.springframework.beans.factory.annotation.Autowired;
26  import org.springframework.jdbc.datasource.DataSourceTransactionManager;
27  import org.springframework.stereotype.Component;
28  import org.springframework.transaction.PlatformTransactionManager;
29  
30  @Component
31  public class DefaultBatchConfigurer implements BatchConfigurer {
32  
33  	private DataSource dataSource;
34  	private PlatformTransactionManager transactionManager;
35  	private JobRepository jobRepository;
36  	private JobLauncher jobLauncher;
37  
38  	@Autowired
39  	public void setDataSource(DataSource dataSource) {
40  		this.dataSource = dataSource;
41  		this.transactionManager = new DataSourceTransactionManager(dataSource);
42  	}
43  
44  	protected DefaultBatchConfigurer() {}
45  
46  	public DefaultBatchConfigurer(DataSource dataSource) {
47  		setDataSource(dataSource);
48  	}
49  
50  	@Override
51  	public JobRepository getJobRepository() {
52  		return jobRepository;
53  	}
54  
55  	@Override
56  	public PlatformTransactionManager getTransactionManager() {
57  		return transactionManager;
58  	}
59  
60  	@Override
61  	public JobLauncher getJobLauncher() {
62  		return jobLauncher;
63  	}
64  
65  	@PostConstruct
66  	public void initialize() throws Exception {
67  		this.jobRepository = createJobRepository();
68  		this.jobLauncher = createJobLauncher();
69  	}
70  
71  	private JobLauncher createJobLauncher() throws Exception {
72  		SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
73  		jobLauncher.setJobRepository(jobRepository);
74  		jobLauncher.afterPropertiesSet();
75  		return jobLauncher;
76  	}
77  
78  	protected JobRepository createJobRepository() throws Exception {
79  		JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
80  		factory.setDataSource(dataSource);
81  		factory.setTransactionManager(transactionManager);
82  		factory.afterPropertiesSet();
83  		return  (JobRepository) factory.getObject();
84  	}
85  
86  }