I have to write a batch (read/process/write) that call a web service (SOAP) as input and then process the result (list of items) to finally write them in a database. How can i call a web service
1 Answer
We did similar thing and here is our approach:
SOAP part:
- We used WebServiceTemplate to communicate with SOAP server and method
marshalSendAndReceive
which basically sends xml request to some url and returns xml response - We use Spring Retry mechanism since SOAP communication is not always reliable so we did setup where we would do each SOAP call at least 5 times until we give up and fail job execution
- We use
Jaxb2Marshaller
for serialization and deserialization and POJO generating from wsdl
Spring batch part:
- We implement our own
ItemReader
where in@BeforeStep
we fetch list of items to process from SOAP server (I am not sure if this is best approach but with retry mechanism in place this is robust enough), our@Override
of read method is nothing special, it is walking over list until exhausted - In processor we translate SOAP item to DB entity
- In writer we do save of items to our own DB
Example:
Item reader is using SoapClient
which is my wrapper around web template and it is doing soap call, unmarshalling response and returning list of items.
@Component
@StepScope
public class CustomItemReader implements ItemReader<SoapItem> {
private List<SoapItem> soapItems;
@Autowired
private SoapClient soapClient;
@BeforeStep
public void beforeStep(final StepExecution stepExecution) throws Exception {
soapItems = soapClient.getItems();
}
@Override
public SoapItem read() {
if (!soapItems.isEmpty()) {
return soapItems.remove(0);
}
return null;
}
}