user1744446
user1744446

Reputation: 119

How does Spring Batch work with web services and how can I read web service input?

i`m new to Spring Batch. I want to know how Spring batch works with web services and how can i get spring batch to read an input from a web service such as transaction_id = 125

to make it clear i want spring batch to read input from a web service (ID = 125), query the input then do some calculations on that record and finaly update the records

Upvotes: 2

Views: 7017

Answers (1)

incomplete-co.de
incomplete-co.de

Reputation: 2137

based on your question, your core job would look something like this;

essentially, you're going to read from a service a single record (webServiceReader - retrieved by id), the process it (webServiceProcessor), then, finally, persist it somewhere (databaseWriter).

if you've already got an existing Web Service Client already setup, you can reuse that component inside an org.springframework.batch.item.adapter.ItemReaderAdapter. This will allow you to save some coding and get back your object. you can then focus on the processor, again, look at using the org.springframework.batch.item.adapter.ItemProcessorAdapter to enable some reuse of existing non-Batch code.

finally, for the persistence, if you're looking to update a database, look at the org.springframework.batch.item.database.JdbcBatchItemWriter.

the only 'trick' to this implementation is how to specify which id to request from the webservice. for that, you may want to look at a queue/stack where you can poll() an id for each successive call.

this might look something like this

<batch:job id="webServiceJob">
    <batch:step id="webServiceJob.step1">
        <batch:tasklet>
            <batch:chunk reader="webServiceReader" processor="webServiceProcessor" writer="databaseWriter" commit-interval="10"/>
        </batch:tasklet>
    </batch:step>
</batch:job>    

<bean id="webServiceReader" class="org.springframework.batch.item.adapter.ItemWriterAdapter">
    <property name="targetObject" ref="myWebService"/>
    <property name="targetMethod" value="getById"/>
    <property name="arguments" value="#{@idQueue.poll()}"/>
</bean>

<bean id="idQueue" class="java.util.concurrent.LinkedBlockingQueue">
    <constructor-arg>
        <list>
            <value>1</value>
            <value>2</value>
            <value>3</value>
        </list>
    </constructor-arg>
</bean>

Upvotes: 3

Related Questions