1 | /* |
2 | * Copyright 2006-2007 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.repository.support; |
18 | |
19 | import org.aopalliance.intercept.MethodInterceptor; |
20 | import org.aopalliance.intercept.MethodInvocation; |
21 | import org.springframework.aop.framework.ProxyFactory; |
22 | import org.springframework.aop.support.DefaultPointcutAdvisor; |
23 | import org.springframework.aop.support.NameMatchMethodPointcut; |
24 | import org.springframework.batch.core.repository.JobRepository; |
25 | import org.springframework.batch.core.repository.dao.ExecutionContextDao; |
26 | import org.springframework.batch.core.repository.dao.JobExecutionDao; |
27 | import org.springframework.batch.core.repository.dao.JobInstanceDao; |
28 | import org.springframework.batch.core.repository.dao.StepExecutionDao; |
29 | import org.springframework.batch.support.PropertiesConverter; |
30 | import org.springframework.beans.factory.FactoryBean; |
31 | import org.springframework.beans.factory.InitializingBean; |
32 | import org.springframework.transaction.PlatformTransactionManager; |
33 | import org.springframework.transaction.interceptor.TransactionInterceptor; |
34 | import org.springframework.transaction.support.TransactionSynchronizationManager; |
35 | import org.springframework.util.Assert; |
36 | |
37 | /** |
38 | * A {@link FactoryBean} that automates the creation of a |
39 | * {@link SimpleJobRepository}. Declares abstract methods for providing DAO |
40 | * object implementations. |
41 | * |
42 | * @see JobRepositoryFactoryBean |
43 | * @see MapJobRepositoryFactoryBean |
44 | * |
45 | * @author Ben Hale |
46 | * @author Lucas Ward |
47 | * @author Robert Kasanicky |
48 | */ |
49 | public abstract class AbstractJobRepositoryFactoryBean implements FactoryBean, InitializingBean { |
50 | |
51 | private PlatformTransactionManager transactionManager; |
52 | |
53 | private ProxyFactory proxyFactory; |
54 | |
55 | private String isolationLevelForCreate = DEFAULT_ISOLATION_LEVEL; |
56 | |
57 | private boolean validateTransactionState = true; |
58 | |
59 | /** |
60 | * Default value for isolation level in create* method. |
61 | */ |
62 | private static final String DEFAULT_ISOLATION_LEVEL = "ISOLATION_SERIALIZABLE"; |
63 | |
64 | /** |
65 | * @return fully configured {@link JobInstanceDao} implementation. |
66 | */ |
67 | protected abstract JobInstanceDao createJobInstanceDao() throws Exception; |
68 | |
69 | /** |
70 | * @return fully configured {@link JobExecutionDao} implementation. |
71 | */ |
72 | protected abstract JobExecutionDao createJobExecutionDao() throws Exception; |
73 | |
74 | /** |
75 | * @return fully configured {@link StepExecutionDao} implementation. |
76 | */ |
77 | protected abstract StepExecutionDao createStepExecutionDao() throws Exception; |
78 | |
79 | /** |
80 | * @return fully configured {@link ExecutionContextDao} implementation. |
81 | */ |
82 | protected abstract ExecutionContextDao createExecutionContextDao() throws Exception; |
83 | |
84 | /** |
85 | * The type of object to be returned from {@link #getObject()}. |
86 | * |
87 | * @return JobRepository.class |
88 | * @see org.springframework.beans.factory.FactoryBean#getObjectType() |
89 | */ |
90 | public Class<JobRepository> getObjectType() { |
91 | return JobRepository.class; |
92 | } |
93 | |
94 | public boolean isSingleton() { |
95 | return true; |
96 | } |
97 | |
98 | /** |
99 | * Flag to determine whether to check for an existing transaction when a |
100 | * JobExecution is created. Defaults to true because it is usually a |
101 | * mistake, and leads to problems with restartability and also to deadlocks |
102 | * in multi-threaded steps. |
103 | * |
104 | * @param validateTransactionState the flag to set |
105 | */ |
106 | public void setValidateTransactionState(boolean validateTransactionState) { |
107 | this.validateTransactionState = validateTransactionState; |
108 | } |
109 | |
110 | /** |
111 | * public setter for the isolation level to be used for the transaction when |
112 | * job execution entities are initially created. The default is |
113 | * ISOLATION_SERIALIZABLE, which prevents accidental concurrent execution of |
114 | * the same job (ISOLATION_REPEATABLE_READ would work as well). |
115 | * |
116 | * @param isolationLevelForCreate the isolation level name to set |
117 | * |
118 | * @see SimpleJobRepository#createJobExecution(String, |
119 | * org.springframework.batch.core.JobParameters) |
120 | */ |
121 | public void setIsolationLevelForCreate(String isolationLevelForCreate) { |
122 | this.isolationLevelForCreate = isolationLevelForCreate; |
123 | } |
124 | |
125 | /** |
126 | * Public setter for the {@link PlatformTransactionManager}. |
127 | * @param transactionManager the transactionManager to set |
128 | */ |
129 | public void setTransactionManager(PlatformTransactionManager transactionManager) { |
130 | this.transactionManager = transactionManager; |
131 | } |
132 | |
133 | /** |
134 | * The transaction manager used in this factory. Useful to inject into steps |
135 | * and jobs, to ensure that they are using the same instance. |
136 | * |
137 | * @return the transactionManager |
138 | */ |
139 | public PlatformTransactionManager getTransactionManager() { |
140 | return transactionManager; |
141 | } |
142 | |
143 | /** |
144 | * Convenience method for clients to grab the {@link JobRepository} without |
145 | * a cast. |
146 | * @return the {@link JobRepository} from {@link #getObject()} |
147 | * @throws Exception if the repository could not be created |
148 | */ |
149 | public JobRepository getJobRepository() throws Exception { |
150 | return (JobRepository) getObject(); |
151 | } |
152 | |
153 | private void initializeProxy() throws Exception { |
154 | if (proxyFactory == null) { |
155 | proxyFactory = new ProxyFactory(); |
156 | TransactionInterceptor advice = new TransactionInterceptor(transactionManager, |
157 | PropertiesConverter.stringToProperties("create*=PROPAGATION_REQUIRES_NEW," |
158 | + isolationLevelForCreate + "\ngetLastJobExecution*=PROPAGATION_REQUIRES_NEW," |
159 | + isolationLevelForCreate + "\n*=PROPAGATION_REQUIRED")); |
160 | if (validateTransactionState) { |
161 | DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor(new MethodInterceptor() { |
162 | public Object invoke(MethodInvocation invocation) throws Throwable { |
163 | if (TransactionSynchronizationManager.isActualTransactionActive()) { |
164 | throw new IllegalStateException( |
165 | "Existing transaction detected in JobRepository. " |
166 | + "Please fix this and try again (e.g. remove @Transactional annotations from client)."); |
167 | } |
168 | return invocation.proceed(); |
169 | } |
170 | }); |
171 | NameMatchMethodPointcut pointcut = new NameMatchMethodPointcut(); |
172 | pointcut.addMethodName("create*"); |
173 | advisor.setPointcut(pointcut); |
174 | proxyFactory.addAdvisor(advisor); |
175 | } |
176 | proxyFactory.addAdvice(advice); |
177 | proxyFactory.setProxyTargetClass(false); |
178 | proxyFactory.addInterface(JobRepository.class); |
179 | proxyFactory.setTarget(getTarget()); |
180 | } |
181 | } |
182 | |
183 | public void afterPropertiesSet() throws Exception { |
184 | Assert.notNull(transactionManager, "TransactionManager must not be null."); |
185 | |
186 | initializeProxy(); |
187 | } |
188 | |
189 | private Object getTarget() throws Exception { |
190 | return new SimpleJobRepository(createJobInstanceDao(), createJobExecutionDao(), createStepExecutionDao(), |
191 | createExecutionContextDao()); |
192 | } |
193 | |
194 | public Object getObject() throws Exception { |
195 | if (proxyFactory == null) { |
196 | afterPropertiesSet(); |
197 | } |
198 | return proxyFactory.getProxy(); |
199 | } |
200 | |
201 | } |