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