Skip to content

Commit 190b14b

Browse files
author
trisberg
committed
BATCH-63: added <listeners> element to <simple-task>
1 parent c11cb3f commit 190b14b

File tree

4 files changed

+61
-33
lines changed

4 files changed

+61
-33
lines changed

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

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,37 @@ public static RuntimeBeanReference getStateTransitionReference(ParserContext par
191191

192192
}
193193

194+
/**
195+
* @param element
196+
* @param parserContext
197+
* @return the TaskletStep bean
198+
*/
199+
protected RootBeanDefinition parseSimpleTask(Element element, ParserContext parserContext) {
200+
201+
RootBeanDefinition bd = new RootBeanDefinition("org.springframework.batch.core.step.tasklet.TaskletStep", null, null);
202+
203+
String taskletBeanId = element.getAttribute("tasklet");
204+
if (StringUtils.hasText(taskletBeanId)) {
205+
RuntimeBeanReference taskletRef = new RuntimeBeanReference(taskletBeanId);
206+
bd.getPropertyValues().addPropertyValue("tasklet", taskletRef);
207+
}
208+
209+
String jobRepository = element.getAttribute("job-repository");
210+
RuntimeBeanReference jobRepositoryRef = new RuntimeBeanReference(jobRepository);
211+
bd.getPropertyValues().addPropertyValue("jobRepository", jobRepositoryRef);
212+
213+
String transactionManager = element.getAttribute("transaction-manager");
214+
RuntimeBeanReference tx = new RuntimeBeanReference(transactionManager);
215+
bd.getPropertyValues().addPropertyValue("transactionManager", tx);
216+
217+
handleListenersElement(element, bd, parserContext, "stepExecutionListeners");
218+
219+
bd.setRole(BeanDefinition.ROLE_SUPPORT);
220+
221+
return bd;
222+
223+
}
224+
194225
/**
195226
* @param element
196227
* @param parserContext
@@ -301,7 +332,7 @@ protected RootBeanDefinition parseProcessTask(Element element, ParserContext par
301332

302333
handleExceptionElement(element, bd, "fatal-exception-classes", "fatalExceptionClasses", isFaultTolerant);
303334

304-
handleListenersElement(element, bd, parserContext);
335+
handleListenersElement(element, bd, parserContext, "listeners");
305336

306337
handleRetryListenersElement(element, bd, parserContext);
307338

@@ -352,7 +383,7 @@ private void handleExceptionElement(Element element, RootBeanDefinition bd,
352383
}
353384

354385
@SuppressWarnings("unchecked")
355-
private void handleListenersElement(Element element, RootBeanDefinition bd, ParserContext parserContext) {
386+
private void handleListenersElement(Element element, RootBeanDefinition bd, ParserContext parserContext, String property) {
356387
Element listenersElement =
357388
DomUtils.getChildElementByTagName(element, "listeners");
358389
if (listenersElement != null) {
@@ -361,7 +392,7 @@ private void handleListenersElement(Element element, RootBeanDefinition bd, Pars
361392
listenerBeans);
362393
ManagedList arguments = new ManagedList();
363394
arguments.addAll(listenerBeans);
364-
bd.getPropertyValues().addPropertyValue("listeners", arguments);
395+
bd.getPropertyValues().addPropertyValue(property, arguments);
365396
}
366397
}
367398

@@ -448,33 +479,4 @@ private void handleStreamsElement(Element element, RootBeanDefinition bd, Parser
448479
}
449480
}
450481

451-
/**
452-
* @param element
453-
* @param parserContext
454-
* @return the TaskletStep bean
455-
*/
456-
protected RootBeanDefinition parseSimpleTask(Element element, ParserContext parserContext) {
457-
458-
RootBeanDefinition bd = new RootBeanDefinition("org.springframework.batch.core.step.tasklet.TaskletStep", null, null);
459-
460-
String taskletBeanId = element.getAttribute("tasklet");
461-
if (StringUtils.hasText(taskletBeanId)) {
462-
RuntimeBeanReference taskletRef = new RuntimeBeanReference(taskletBeanId);
463-
bd.getPropertyValues().addPropertyValue("tasklet", taskletRef);
464-
}
465-
466-
String jobRepository = element.getAttribute("job-repository");
467-
RuntimeBeanReference jobRepositoryRef = new RuntimeBeanReference(jobRepository);
468-
bd.getPropertyValues().addPropertyValue("jobRepository", jobRepositoryRef);
469-
470-
String transactionManager = element.getAttribute("transaction-manager");
471-
RuntimeBeanReference tx = new RuntimeBeanReference(transactionManager);
472-
bd.getPropertyValues().addPropertyValue("transactionManager", tx);
473-
474-
bd.setRole(BeanDefinition.ROLE_SUPPORT);
475-
476-
return bd;
477-
478-
}
479-
480482
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,21 @@
232232
<xsd:complexType name="simpleTaskType">
233233
<xsd:complexContent>
234234
<xsd:extension base="stepDefType">
235+
<xsd:sequence>
236+
<xsd:element name="listeners" minOccurs="0" maxOccurs="1">
237+
<xsd:annotation>
238+
<xsd:documentation><![CDATA[
239+
List of all listeners for the step definition
240+
]]>
241+
</xsd:documentation>
242+
</xsd:annotation>
243+
<xsd:complexType>
244+
<xsd:sequence>
245+
<xsd:element name="listener" type="listenerType" minOccurs="1" maxOccurs="unbounded"/>
246+
</xsd:sequence>
247+
</xsd:complexType>
248+
</xsd:element>
249+
</xsd:sequence>
235250
<xsd:attribute name="tasklet" type="xsd:string" use="required">
236251
<xsd:annotation>
237252
<xsd:documentation><![CDATA[

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.springframework.batch.core.repository.JobRepository;
3030
import org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean;
3131
import org.springframework.beans.factory.annotation.Autowired;
32+
import org.springframework.beans.factory.annotation.Qualifier;
3233
import org.springframework.test.context.ContextConfiguration;
3334
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
3435

@@ -48,8 +49,13 @@ public class StepWithSimpleTaskJobParserTests {
4849
private JobRepository jobRepository;
4950

5051
@Autowired
52+
@Qualifier("tasklet")
5153
private AbstractTestComponent tasklet;
5254

55+
@Autowired
56+
@Qualifier("listener")
57+
private TestListener listener;
58+
5359
@Before
5460
public void setUp() {
5561
MapJobRepositoryFactoryBean.clear();
@@ -63,5 +69,6 @@ public void testStepWithTask() throws Exception {
6369
assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
6470
assertEquals(2, jobExecution.getStepExecutions().size());
6571
assertTrue(tasklet.isExecuted());
72+
assertTrue(listener.isExecuted());
6673
}
6774
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
<next on="*" to="step2" />
1313
</step>
1414
<step name="step2">
15-
<simple-task tasklet="tasklet"/>
15+
<simple-task tasklet="tasklet">
16+
<listeners>
17+
<listener id="listener" class="org.springframework.batch.core.configuration.xml.TestListener"/>
18+
</listeners>
19+
</simple-task>
1620
</step>
1721
</job>
1822

0 commit comments

Comments
 (0)