BATCH-63:added listeners and exceptions to <chunk-oriented>

This commit is contained in:
trisberg
2008-11-07 16:25:32 +00:00
parent 1270254608
commit 97ce100ff9
5 changed files with 145 additions and 7 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.batch.core.configuration.xml;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
@@ -26,10 +27,12 @@ import org.springframework.batch.core.job.flow.support.state.EndState;
import org.springframework.batch.core.job.flow.support.state.StepState;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanReference;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.parsing.BeanComponentDefinition;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.ManagedList;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.StringUtils;
@@ -194,11 +197,17 @@ public class StepParser {
* @return the TaskletStep bean
*/
protected RootBeanDefinition parseChunkOriented(Element element, ParserContext parserContext) {
System.out.println("PARSING PROCESS!!!");
RootBeanDefinition bd = new RootBeanDefinition("org.springframework.batch.core.step.item.SimpleStepFactoryBean", null, null);
RootBeanDefinition bd;
String faultTolerant = element.getAttribute("fault-tolerant");
if ("true".equals(faultTolerant)) {
bd = new RootBeanDefinition("org.springframework.batch.core.step.item.FaultTolerantStepFactoryBean", null, null);
}
else {
bd = new RootBeanDefinition("org.springframework.batch.core.step.item.SimpleStepFactoryBean", null, null);
}
String readerBeanId = element.getAttribute("reader");
if (StringUtils.hasText(readerBeanId)) {
RuntimeBeanReference readerRef = new RuntimeBeanReference(readerBeanId);
@@ -224,13 +233,61 @@ public class StepParser {
String transactionManager = element.getAttribute("transaction-manager");
RuntimeBeanReference tx = new RuntimeBeanReference(transactionManager);
bd.getPropertyValues().addPropertyValue("transactionManager", tx);
handleExceptionElement(element, bd, "skippable-exception-classes", "skippableExceptionClasses");
handleExceptionElement(element, bd, "retryable-exception-classes", "retryableExceptionClasses");
handleExceptionElement(element, bd, "fatal-exception-classes", "fatalExceptionClasses");
handleListenersElement(element, bd, parserContext);
bd.setRole(BeanDefinition.ROLE_SUPPORT);
return bd;
}
private void handleExceptionElement(Element element, RootBeanDefinition bd,
String attributeName, String propertyName) {
String exceptions =
DomUtils.getChildElementValueByTagName(element, attributeName);
if (StringUtils.hasLength(exceptions)) {
String[] exceptionArray = StringUtils.tokenizeToStringArray(
StringUtils.delete(exceptions, ","), "\n");
if (exceptionArray.length > 0) {
bd.getPropertyValues().addPropertyValue(propertyName, exceptionArray);
}
}
}
@SuppressWarnings("unchecked")
private void handleListenersElement(Element element, RootBeanDefinition bd, ParserContext parserContext) {
Element listenersElement =
DomUtils.getChildElementByTagName(element, "listeners");
if (listenersElement != null) {
List<String> listenerRefs = new ArrayList<String>();
List<BeanReference> listenerBeans = new ArrayList<BeanReference>();
List<Element> listenerElements =
DomUtils.getChildElementsByTagName(listenersElement, "listener");
if (listenerElements != null) {
for (Element listenerElement : listenerElements) {
String listenerRef = listenerElement.getAttribute("ref");
if (StringUtils.hasText(listenerRef)) {
listenerRefs.add(listenerRef);
RuntimeBeanReference bean = new RuntimeBeanReference(listenerRef);
if (bean != null) {
listenerBeans.add(bean);
}
}
}
}
ManagedList arguments = new ManagedList();
arguments.addAll(listenerBeans);
bd.getPropertyValues().addPropertyValue("listeners", arguments);
}
}
/**
* @param element
* @param parserContext

View File

@@ -215,6 +215,60 @@
<xsd:complexType name="chunkOrientedType">
<xsd:complexContent>
<xsd:extension base="stepDefType">
<xsd:sequence>
<xsd:element name="listeners" minOccurs="0" maxOccurs="1">
<xsd:annotation>
<xsd:documentation><![CDATA[
List of all listeners for the step definition
]]>
</xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:sequence>
<xsd:element name="listener">
<xsd:complexType>
<xsd:attribute name="ref" type="xsd:string"/>
<xsd:attribute name="class" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="skippable-exception-classes" minOccurs="0" maxOccurs="1">
<xsd:annotation>
<xsd:documentation><![CDATA[
The newline-separated, list of exception classes that are skippable
]]>
</xsd:documentation>
</xsd:annotation>
<xsd:simpleType>
<xsd:restriction base="xsd:string"/>
</xsd:simpleType>
</xsd:element>
<xsd:element name="retryable-exception-classes" minOccurs="0" maxOccurs="1">
<xsd:annotation>
<xsd:documentation><![CDATA[
The newline-separated, list of exception classes that are skippable
]]>
</xsd:documentation>
</xsd:annotation>
<xsd:simpleType>
<xsd:restriction base="xsd:string"/>
</xsd:simpleType>
</xsd:element>
<xsd:element name="fatal-exception-classes" minOccurs="0" maxOccurs="1">
<xsd:annotation>
<xsd:documentation><![CDATA[
The newline-separated, list of exception classes that are skippable
]]>
</xsd:documentation>
</xsd:annotation>
<xsd:simpleType>
<xsd:restriction base="xsd:string"/>
</xsd:simpleType>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="fault-tolerant" type="xsd:boolean" default="false" use="optional"/>
<xsd:attribute name="reader" type="xsd:string" use="required">
<xsd:annotation>
<xsd:documentation><![CDATA[

View File

@@ -50,6 +50,9 @@ public class StepWithChunkOrientedJobParserTests {
@Autowired
private TestReader reader;
@Autowired
private TestListener listener;
@Autowired
private TestProcessor processor;
@@ -71,5 +74,6 @@ public class StepWithChunkOrientedJobParserTests {
assertTrue(reader.isExecuted());
assertTrue(processor.isExecuted());
assertTrue(writer.isExecuted());
assertTrue(listener.isExecuted());
}
}

View File

@@ -0,0 +1,17 @@
package org.springframework.batch.core.configuration.xml;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.StepExecutionListener;
import org.springframework.batch.repeat.ExitStatus;
public class TestListener extends AbstractTestComponent implements StepExecutionListener {
public ExitStatus afterStep(StepExecution stepExecution) {
return null;
}
public void beforeStep(StepExecution stepExecution) {
executed = true;
}
}

View File

@@ -8,7 +8,11 @@
<job id="job">
<step name="step1">
<chunk-oriented reader="reader" processor="processor" writer="writer"/>
<chunk-oriented reader="reader" processor="processor" writer="writer">
<listeners>
<listener ref="listener"/>
</listeners>
</chunk-oriented>
</step>
</job>
@@ -18,4 +22,6 @@
<beans:bean id="writer" class="org.springframework.batch.core.configuration.xml.TestWriter"/>
<beans:bean id="listener" class="org.springframework.batch.core.configuration.xml.TestListener"/>
</beans:beans>