Skip to content

Commit b27ee64

Browse files
committed
Use reflection-based method lookup in transaction attribute source of JobOperatorFactoryBean
This commit replaces the method name matching transaction attribute source with a reflection-based one for safe refactoring. Note that the previous transaction attributes (Propagation.REQUIRED and Isolation.DEFAULT) are the default values in DefaultTransactionAttribute introduced by this commit.
1 parent 3433b02 commit b27ee64

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/JobOperatorFactoryBean.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package org.springframework.batch.core.launch.support;
1717

18-
import java.util.Properties;
18+
import java.lang.reflect.Method;
1919

2020
import io.micrometer.core.instrument.MeterRegistry;
2121
import io.micrometer.core.instrument.Metrics;
@@ -25,6 +25,7 @@
2525
import org.springframework.batch.core.configuration.JobRegistry;
2626
import org.springframework.batch.core.converter.DefaultJobParametersConverter;
2727
import org.springframework.batch.core.converter.JobParametersConverter;
28+
import org.springframework.batch.core.job.JobExecution;
2829
import org.springframework.batch.core.launch.JobOperator;
2930
import org.springframework.batch.core.repository.JobRepository;
3031
import org.springframework.beans.factory.FactoryBean;
@@ -33,9 +34,8 @@
3334
import org.springframework.core.task.TaskExecutor;
3435
import org.springframework.transaction.PlatformTransactionManager;
3536
import org.springframework.transaction.TransactionManager;
36-
import org.springframework.transaction.annotation.Isolation;
37-
import org.springframework.transaction.annotation.Propagation;
38-
import org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource;
37+
import org.springframework.transaction.interceptor.DefaultTransactionAttribute;
38+
import org.springframework.transaction.interceptor.MethodMapTransactionAttributeSource;
3939
import org.springframework.transaction.interceptor.TransactionAttributeSource;
4040
import org.springframework.transaction.interceptor.TransactionInterceptor;
4141
import org.springframework.util.Assert;
@@ -53,10 +53,6 @@ public class JobOperatorFactoryBean implements FactoryBean<JobOperator>, Initial
5353

5454
protected static final Log logger = LogFactory.getLog(JobOperatorFactoryBean.class);
5555

56-
private static final String TRANSACTION_ISOLATION_LEVEL_PREFIX = "ISOLATION_";
57-
58-
private static final String TRANSACTION_PROPAGATION_PREFIX = "PROPAGATION_";
59-
6056
private PlatformTransactionManager transactionManager;
6157

6258
private TransactionAttributeSource transactionAttributeSource;
@@ -83,12 +79,14 @@ public void afterPropertiesSet() throws Exception {
8379
this.taskExecutor = new SyncTaskExecutor();
8480
}
8581
if (this.transactionAttributeSource == null) {
86-
Properties transactionAttributes = new Properties();
87-
String transactionProperties = String.join(",", TRANSACTION_PROPAGATION_PREFIX + Propagation.REQUIRED,
88-
TRANSACTION_ISOLATION_LEVEL_PREFIX + Isolation.DEFAULT);
89-
transactionAttributes.setProperty("stop*", transactionProperties);
90-
this.transactionAttributeSource = new NameMatchTransactionAttributeSource();
91-
((NameMatchTransactionAttributeSource) transactionAttributeSource).setProperties(transactionAttributes);
82+
this.transactionAttributeSource = new MethodMapTransactionAttributeSource();
83+
DefaultTransactionAttribute transactionAttribute = new DefaultTransactionAttribute();
84+
Method stopMethod = TaskExecutorJobOperator.class.getMethod("stop", JobExecution.class);
85+
Method abandonMethod = TaskExecutorJobOperator.class.getMethod("abandon", JobExecution.class);
86+
((MethodMapTransactionAttributeSource) this.transactionAttributeSource).addTransactionalMethod(stopMethod,
87+
transactionAttribute);
88+
((MethodMapTransactionAttributeSource) this.transactionAttributeSource)
89+
.addTransactionalMethod(abandonMethod, transactionAttribute);
9290
}
9391
}
9492

0 commit comments

Comments
 (0)