Tuesday, August 9, 2016

WSO2 DSS - Batch Request Support

WSO2 Data Services Server provides the capability to support batch requests for operations, which contain multiple parameter sets for a single request. When a data service is created with the batch request mode set, for all the in-only operations (operations which does not have any return value), a corresponding batch operation will also be automatically created. This batch operation will taken in an array of parameters lists, compared to the single parameter list the non-batch operation require.

Batch request support can be enabled by

enableBatchRequests="true"


A sample data service with batch support is given below.

<data enableBatchRequests="true" name="TestBatch" transports="http https local">
   <config id="TestOra">
      <property name="driverClassName">oracle.jdbc.driver.OracleDriver</property>
      <property name="url">jdbc:oracle:thin:@localhost:1521/xe</property>
      <property name="username">testwso2</property>
      <property name="password">testwso2</property>
   </config>
   <query id="AddDataQuery" useConfig="TestOra">
      <sql>call adddata(:userid,:colsize)</sql>
      <param name="userid" sqlType="STRING"/>
      <param name="colsize" sqlType="STRING"/>
   </query>
   <operation name="TestBatchOperation" returnRequestStatus="true">
      <call-query href="AddDataQuery">
         <with-param name="userid" query-param="userid"/>
         <with-param name="colsize" query-param="colsize"/>
      </call-query>
   </operation>
</data>


The table creation and adddata procedure sql used for above sample is as follows.

create table testtable(userid int ,colsize int,lut TIMESTAMP);


CREATE OR REPLACE PROCEDURE addData (  
    p_userid testtable.userid%TYPE,  
    p_colsize testtable.colsize%TYPE)    
    IS  
    BEGIN  
    INSERT INTO testtable (userid, colsize, lut)  
    VALUES (p_userid, p_colsize,  current_timestamp );  
    COMMIT;  
    END;  
    / 



After creating the above service you can test it by using the tryit tool. Once the operations are created and the service is deployed, corresponding batch operation will be created according to the given operation. For this example "TestBatchOperation_batch_req" is created and this will give you a request SOAP body for inserting one data set as shown below. You can change the request SOAP body to handle multiple requests by repeating the SOAP body use in single request.

 


Also you can generate a Axis2Client for the service. You can use the WSDL2Java tool given in the Tools page to generate the client. It will generate the pom.xml file and build it using the command "mvn clean install". Once we get the client stub generated then we can call the methods in the stub as follows.

package org.wso2.ws.dataservice;

import org.wso2.ws.dataservice.TestBatchStub;

public class TestClient {
    public static void main(String[] args) throws Exception {
        addDataBatch(0, 100);
        addDataBatch(100, 200);
        addDataBatch(200, 300);
        addDataBatch(300, 400);
        System.out.println("Completed..!");
    }

    private static TestBatchStub.TestBatchOperation_type0 createRecord(String id, String size) {

        TestBatchStub.TestBatchOperation_type0 val = new TestBatchStub.TestBatchOperation_type0();
        val.setUserid(id);
        val.setColsize(size);
        return val;
    }

    private static void addDataBatch(int iStart, int iEnd) throws Exception {
        String epr = "http://localhost:9763" + "/services/TestBatch";
        TestBatchStub stubtest = new TestBatchStub(epr);
        TestBatchStub.TestBatchOperation_batch_req req = new TestBatchStub.TestBatchOperation_batch_req();

        for (int i = iStart; i < iEnd; i++) {
            String id = Integer.toString(i);
            req.addTestBatchOperation(createRecord(id, "0"));
        }

        try {
            System.out.println("Executing Add Data..");
            stubtest.testBatchOperation_batch_req(req);
        } catch (Exception e) {
            System.out.println("Error in Add Data!");
        }
        System.out.println("Exiting..!");
    }
}






References : DSS 3.5.0 - Batch Processing Sample

1 comment:

  1. Hi Anupama, the blog is very helpful. needed a small information how to insert more than 10k records to a DB using WSO2DSS. we have tried Batch request option provided by DSS

    ReplyDelete