Java Reference
In-Depth Information
import org.springframework.batch.item.ItemWriter;
public class CustomerClassifier implements
Classifier<Customer, ItemWriter<Customer>> {
private ItemWriter<Customer> fileItemWriter;
private ItemWriter<Customer> jdbcItemWriter;
@Override
public ItemWriter<Customer> classify(Customer customer) {
if(customer.getState().matches("^[A-M].*")) {
return fileItemWriter;
} else {
return jdbcItemWriter;
}
}
public void setFileItemWriter(ItemWriter<Customer> fileItemWriter) {
this.fileItemWriter = fileItemWriter;
}
public void setJdbcItemWriter(ItemWriter<Customer> jdbcItemWriter) {
this.jdbcItemWriter = jdbcItemWriter;
}
}
With the
CustomerClassifier
coded, you can configure the job and ItemWriters. You reuse the same
input and individual ItemWriters you used in the
CompositeItemWriter
example in the previous section,
leaving only
ClassifierCompositeItemWriter
to configure. The configuration for
ClassifierCompositeItemWriter
and
CustomerClassifier
is shown in Listing 9-78.
Listing 9-78.
Configuration of the
ClassifierCompositeItemWriter
and Dependencies
...
<beans:bean id="customerClassifier"
class="com.apress.springbatch.chapter9.CustomerClassifier">
<beans:property name="fileItemWriter" ref="xmlOutputWriter"/>
<beans:property name="jdbcItemWriter" ref="jdbcBatchWriter"/>
</beans:bean>
<beans:bean id="classifierWriter" class="org.springframework.batch.item.
support.ClassifierCompositeItemWriter">
<beans:property name="classifier" ref="customerClassifier"/>
</beans:bean>
<step id="formatFileStep">
<tasklet>
<chunk reader="customerFileReader" writer="classifierWriter"
commit-interval="10"/>
</tasklet>
</step>