I had the same problem while deserializing LocalDate
from step execution context.
So I have to make my proper converter :
public class DateConverter implements Converter {
private static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd";
private static final DateTimeFormatter DEFAULT_DATE_FORMATTER = DateTimeFormatter.ofPattern(DEFAULT_DATE_PATTERN);
public DateConverter() {
super();
}
public boolean canConvert(Class clazz) {
return LocalDate.class.isAssignableFrom(clazz);
}
/**
* Convert LocalDate to String
*/
public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext context) {
LocalDate date = (LocalDate) value;
String result = date.format(DEFAULT_DATE_FORMATTER);
writer.setValue(result);
}
/**
* convert Xml to LocalDate
*/
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
LocalDate result = LocalDate.parse(reader.getValue(), DEFAULT_DATE_FORMATTER);
return result;
}
}
After that i have to create my proper XStreamExecutionContextStringSerializer
for using my converter
/**
* My XStreamExecutionContextStringSerializer
* @since 1.0
*/
public class MyXStreamExecutionContextStringSerializer implements ExecutionContextSerializer, InitializingBean {
private ReflectionProvider reflectionProvider = null;
private HierarchicalStreamDriver hierarchicalStreamDriver;
private XStream xstream;
public void setReflectionProvider(ReflectionProvider reflectionProvider) {
this.reflectionProvider = reflectionProvider;
}
public void setHierarchicalStreamDriver(HierarchicalStreamDriver hierarchicalStreamDriver) {
this.hierarchicalStreamDriver = hierarchicalStreamDriver;
}
@Override
public void afterPropertiesSet() throws Exception {
init();
}
public synchronized void init() throws Exception {
if (hierarchicalStreamDriver == null) {
this.hierarchicalStreamDriver = new JettisonMappedXmlDriver();
}
if (reflectionProvider == null) {
xstream = new XStream(hierarchicalStreamDriver);
}
else {
xstream = new XStream(reflectionProvider, hierarchicalStreamDriver);
}
// Convert LocalDate
xstream.registerConverter(new DateConverter());
}
/**
* Serializes the passed execution context to the supplied OutputStream.
*
* @param context
* @param out
* @see Serializer#serialize(Object, OutputStream)
*/
@Override
public void serialize(Map<String, Object> context, OutputStream out) throws IOException {
Assert.notNull(context);
Assert.notNull(out);
out.write(xstream.toXML(context).getBytes());
}
/**
* Deserializes the supplied input stream into a new execution context.
*
* @param in
* @return a reconstructed execution context
* @see Deserializer#deserialize(InputStream)
*/
@SuppressWarnings("unchecked")
@Override
public Map<String, Object> deserialize(InputStream in) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(in));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
return (Map<String, Object>) xstream.fromXML(sb.toString());
}
}
The last step is to register MyXStreamExecutionContextStringSerializer in the file execution-context.xml that register the bean jobRepository
<bean id="jobRepository"
class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="transactionManager" ref="transactionManager" />
<property name="tablePrefix" value="${batch.table.prefix:BATCH.BATCH_}" />
<property name="serializer"> <bean class="com.batch.config.MyXStreamExecutionContextStringSerializer"/> </property>
</bean>