RAJ
RAJ

Reputation: 31

I need to write a spring batch to call the web service is there any example

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

Upvotes: 2

Views: 11230

Answers (1)

Nenad Bozic
Nenad Bozic

Reputation: 3784

We did similar thing and here is our approach:

SOAP part:

  1. We used WebServiceTemplate to communicate with SOAP server and method marshalSendAndReceive which basically sends xml request to some url and returns xml response
  2. 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
  3. We use Jaxb2Marshaller for serialization and deserialization and POJO generating from wsdl

Spring batch part:

  1. 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
  2. In processor we translate SOAP item to DB entity
  3. 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;
    }
}

Upvotes: 7

Related Questions