Skip to content

Commit c99fa10

Browse files
author
trisberg
committed
BATCH-63: added additional attributes to <process-task>
1 parent 5274b55 commit c99fa10

File tree

5 files changed

+73
-18
lines changed

5 files changed

+73
-18
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/configuration/xml/StepParser.java

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,12 @@ protected RootBeanDefinition parseProcessTask(Element element, ParserContext par
229229
bd.getPropertyValues().addPropertyValue("itemWriter", writerRef);
230230
}
231231

232+
String taskExecutorBeanId = element.getAttribute("task-executor");
233+
if (StringUtils.hasText(taskExecutorBeanId)) {
234+
RuntimeBeanReference taskExecutorRef = new RuntimeBeanReference(taskExecutorBeanId);
235+
bd.getPropertyValues().addPropertyValue("taskExecutor", taskExecutorRef);
236+
}
237+
232238
String jobRepository = element.getAttribute("job-repository");
233239
RuntimeBeanReference jobRepositoryRef = new RuntimeBeanReference(jobRepository);
234240
bd.getPropertyValues().addPropertyValue("jobRepository", jobRepositoryRef);
@@ -245,19 +251,43 @@ protected RootBeanDefinition parseProcessTask(Element element, ParserContext par
245251
String skipLimit = element.getAttribute("skip-limit");
246252
if (StringUtils.hasText(skipLimit)) {
247253
if (!isFaultTolerant) {
248-
throw new BeanCreationException("skip-limit can only be specified if fault-tolerant is set to 'true'");
254+
throw new BeanCreationException("skip-limit can only be specified if fault-tolerant is set to \"true\"");
249255
}
250256
bd.getPropertyValues().addPropertyValue("skipLimit", skipLimit);
251257
}
252258

253259
String retryLimit = element.getAttribute("retry-limit");
254260
if (StringUtils.hasText(retryLimit)) {
255261
if (!isFaultTolerant) {
256-
throw new BeanCreationException("retry-limit can only be specified if fault-tolerant is set to 'true'");
262+
throw new BeanCreationException("retry-limit can only be specified if fault-tolerant is set to \"true\"");
257263
}
258264
bd.getPropertyValues().addPropertyValue("retryLimit", retryLimit);
259265
}
260266

267+
String cacheCapacity = element.getAttribute("cache-capacity");
268+
if (StringUtils.hasText(cacheCapacity)) {
269+
if (!isFaultTolerant) {
270+
throw new BeanCreationException("cache-capacity can only be specified if fault-tolerant is set to \"true\"");
271+
}
272+
bd.getPropertyValues().addPropertyValue("cacheCapacity", cacheCapacity);
273+
}
274+
275+
String transactionAttribute = element.getAttribute("transaction-attribute");
276+
if (StringUtils.hasText(transactionAttribute)) {
277+
handleTransactionAttributesElement(element, bd);
278+
bd.getPropertyValues().addPropertyValue("transactionAttribute", transactionAttribute);
279+
}
280+
281+
String isReaderTransactionalQueue = element.getAttribute("is-reader-transactional-queue");
282+
if (StringUtils.hasText(isReaderTransactionalQueue)) {
283+
if (!isFaultTolerant && "true".equals(isReaderTransactionalQueue)) {
284+
throw new BeanCreationException("is-reader-transactional-queue=\"true\" can only be specified if fault-tolerant is set to \"true\"");
285+
}
286+
if (isFaultTolerant) {
287+
bd.getPropertyValues().addPropertyValue("isReaderTransactionalQueue", isReaderTransactionalQueue);
288+
}
289+
}
290+
261291
handleExceptionElement(element, bd, "skippable-exception-classes", "skippableExceptionClasses", isFaultTolerant);
262292

263293
handleExceptionElement(element, bd, "retryable-exception-classes", "retryableExceptionClasses",isFaultTolerant);
@@ -279,13 +309,16 @@ protected RootBeanDefinition parseProcessTask(Element element, ParserContext par
279309

280310
}
281311

312+
private void handleTransactionAttributesElement(Element element, RootBeanDefinition bd) {
313+
}
314+
282315
private void handleExceptionElement(Element element, RootBeanDefinition bd,
283316
String attributeName, String propertyName, boolean isFaultTolerant) {
284317
String exceptions =
285318
DomUtils.getChildElementValueByTagName(element, attributeName);
286319
if (StringUtils.hasLength(exceptions)) {
287320
if (!isFaultTolerant) {
288-
throw new BeanCreationException(attributeName + " can only be specified if fault-tolerant is set to 'true'");
321+
throw new BeanCreationException(attributeName + " can only be specified if fault-tolerant is set to \"true\"");
289322
}
290323
String[] exceptionArray = StringUtils.tokenizeToStringArray(
291324
StringUtils.delete(exceptions, ","), "\n");

spring-batch-core/src/main/resources/org/springframework/batch/core/configuration/xml/spring-batch-2.0.xsd

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -281,18 +281,6 @@
281281
</xsd:sequence>
282282
</xsd:complexType>
283283
</xsd:element>
284-
<xsd:element name="transaction-attributes" minOccurs="0" maxOccurs="1">
285-
<xsd:annotation>
286-
<xsd:documentation><![CDATA[
287-
The transaction attributes to be used for the transaction used during the execution
288-
of the task within the step.
289-
]]>
290-
</xsd:documentation>
291-
</xsd:annotation>
292-
<xsd:simpleType>
293-
<xsd:restriction base="xsd:string"/>
294-
</xsd:simpleType>
295-
</xsd:element>
296284
<xsd:element name="skippable-exception-classes" minOccurs="0" maxOccurs="1">
297285
<xsd:annotation>
298286
<xsd:documentation><![CDATA[
@@ -384,6 +372,14 @@
384372
]]></xsd:documentation>
385373
</xsd:annotation>
386374
</xsd:attribute>
375+
<xsd:attribute name="transaction-attribute" type="xsd:string" use="optional">
376+
<xsd:annotation>
377+
<xsd:documentation><![CDATA[
378+
The transaction attributes to be used for the transaction used during the execution
379+
of the task within the step.
380+
]]></xsd:documentation>
381+
</xsd:annotation>
382+
</xsd:attribute>
387383
<xsd:attribute name="is-reader-transactional-queue" type="xsd:boolean" default="false" use="optional">
388384
<xsd:annotation>
389385
<xsd:documentation><![CDATA[

spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/StepWithFaultTolerantProcessTaskJobParserTests.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,13 @@
3434
import org.springframework.batch.retry.RetryListener;
3535
import org.springframework.beans.factory.annotation.Autowired;
3636
import org.springframework.beans.factory.annotation.Qualifier;
37+
import org.springframework.scheduling.concurrent.ConcurrentTaskExecutor;
3738
import org.springframework.test.context.ContextConfiguration;
3839
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
3940
import org.springframework.test.util.ReflectionTestUtils;
41+
import org.springframework.transaction.TransactionDefinition;
42+
import org.springframework.transaction.interceptor.RollbackRuleAttribute;
43+
import org.springframework.transaction.interceptor.RuleBasedTransactionAttribute;
4044

4145

4246
/**
@@ -87,6 +91,23 @@ public void testStepWithTask() throws Exception {
8791
assertEquals("wrong skip-limit:", 20, sl);
8892
Object rl = ReflectionTestUtils.getField(factory, "retryLimit");
8993
assertEquals("wrong retry-limit:", 3, rl);
94+
Object cc = ReflectionTestUtils.getField(factory, "cacheCapacity");
95+
assertEquals("wrong cache-capacity:", 100, cc);
96+
Object txa = ReflectionTestUtils.getField(factory, "transactionAttribute");
97+
assertEquals("wrong transaction-attribute:", TransactionDefinition.PROPAGATION_REQUIRED,
98+
((RuleBasedTransactionAttribute)txa).getPropagationBehavior());
99+
assertEquals("wrong transaction-attribute:", TransactionDefinition.ISOLATION_DEFAULT,
100+
((RuleBasedTransactionAttribute)txa).getIsolationLevel());
101+
assertEquals("wrong transaction-attribute:", 10,
102+
((RuleBasedTransactionAttribute)txa).getTimeout());
103+
RollbackRuleAttribute rra =
104+
(RollbackRuleAttribute) ((RuleBasedTransactionAttribute)txa).getRollbackRules().get(0);
105+
assertEquals("wrong transaction-attribute:",
106+
"org.springframework.dao.DataIntegrityViolationException", rra.getExceptionName());
107+
Object txq = ReflectionTestUtils.getField(factory, "isReaderTransactionalQueue");
108+
assertEquals("wrong is-reader-transactional-queue:", true, txq);
109+
Object te = ReflectionTestUtils.getField(factory, "taskExecutor");
110+
assertEquals("wrong task-executor:", ConcurrentTaskExecutor.class, te.getClass());
90111
Object listeners = ReflectionTestUtils.getField(factory, "listeners");
91112
assertEquals("wrong number of listeners:", 2, ((StepListener[])listeners).length);
92113
Object retryListeners = ReflectionTestUtils.getField(factory, "retryListeners");

spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/TestRetryListener.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ public <T> void onError(RetryContext context, RetryCallback<T> callback,
1515
}
1616

1717
public <T> boolean open(RetryContext context, RetryCallback<T> callback) {
18-
System.out.println("RETRY RETRY RETRY RETRY");
1918
executed = true;
2019
return true;
2120
}

spring-batch-core/src/test/resources/org/springframework/batch/core/configuration/xml/StepWithFaultTolerantProcessTaskJobParserTests-context.xml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@
77
<beans:import resource="common-context.xml" />
88

99
<job id="job">
10-
<step name="ft-step">
10+
<step name="step">
1111
<process-task reader="reader" processor="processor" writer="writer"
12-
fault-tolerant="true" commit-interval="10" skip-limit="20" retry-limit="3">
12+
fault-tolerant="true" commit-interval="10" skip-limit="20"
13+
retry-limit="3" cache-capacity="100"
14+
transaction-attribute="PROPAGATION_REQUIRED,ISOLATION_DEFAULT,timeout_10,-org.springframework.dao.DataIntegrityViolationException"
15+
is-reader-transactional-queue="true"
16+
task-executor="taskExecutor">
1317
<listeners>
1418
<listener class="org.springframework.batch.core.configuration.xml.TestListener"/>
1519
<listener ref="listener"/>
@@ -31,5 +35,7 @@
3135
<beans:bean id="writer" class="org.springframework.batch.core.configuration.xml.TestWriter"/>
3236

3337
<beans:bean id="listener" class="org.springframework.batch.core.configuration.xml.TestListener"/>
38+
39+
<beans:bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ConcurrentTaskExecutor"/>
3440

3541
</beans:beans>

0 commit comments

Comments
 (0)