Netweaver Gateway Development

OData $batch processing in SAP Gateway Service

                      In some business scenarios multiple entity instances need to be handled together as a single logical unit of work.This is where $batch processing come into picture.A $batch request means multiple operations bundled as a single request.Batch processing simple batches several independent OData service call to a single big OData call.
                Important thing to remember is 'Do not use batch processing if individual service calls depend on each other(Result of another OData service call)'.Batch calls can combine read and write requests.For write requests we need to define related change-sets in the request.
                  OData batch request requires setting the Content-Type request header according to the boundary defined.We will see in the tutorial.
                  We will be batching multiple create operations on our FlightSet.
Pre-requisites

  1. OData Service Created.
  2. Create operation in sap gateway OData service implemented.


Steps

  1. For batching read operations we dont have to do anything in gateway service side.Since we are going to batch write operations we need to redefine CHANGESET_BEGIN and CHANGESET_END methods of our **DPC_EXT class.Either we can implement our own logic or just comment the code and activate it.Right click on both methods and click Redefine.
  2. Comment the code inside both methods and activate both methods.
  3. Prepare the batch request.See sample batch request for the FlightSet entity here.Spaces are important in request

  4. --batch
    Content-Type: multipart/mixed; boundary=changeset
    
    --changeset
    Content-Type: application/http
    Content-Transfer-Encoding: binary
    
    POST FlightSet HTTP/1.1
    Content-Type: application/atom+xml
    Content-Length: 588
    
    <?xml version="1.0" encoding="utf-8" standalone="yes"?>
    <atom:entry xmlns:atom="http://www.w3.org/2005/Atom"
    xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"
    xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
    <atom:content type="application/xml">
    <m:properties>
    <d:Carrid>AA</d:Carrid>
    <d:Carrname>American Airlines</d:Carrname>
    <d:Currcode>USD</d:Currcode>
    <d:Url>http://americanairlines.com</d:Url>
    </m:properties>
    </atom:content>
    </atom:entry>
    
    
    --changeset
    Content-Type: application/http
    Content-Transfer-Encoding: binary
    
    POST FlightSet HTTP/1.1
    Content-Type: application/atom+xml
    Content-Length: 588
    
    <?xml version="1.0" encoding="utf-8" standalone="yes"?>
    <atom:entry xmlns:atom="http://www.w3.org/2005/Atom"
    xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"
    xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
    <atom:content type="application/xml">
    <m:properties>
    <d:Carrid>UA</d:Carrid>
    <d:Carrname>United Airlines</d:Carrname>
    <d:Currcode>USD</d:Currcode>
    <d:Url>http://unitedairlines.com</d:Url>
    </m:properties>
    </atom:content>
    </atom:entry>
    
    --changeset--
    --batch-- 
    

  5. Open SAP Gateway Client().Give the service url and select POST method(Create operation).Url is /sap/opu/odata/SAP/ZTEST_ODATA_SRV_01/$batch
  6. Add http header Content-Type and give multipart/mixed; boundary=batch as header value.You can see batch is my boundary defined in my request in step 3.If yours is different change it accordingly.(You can add header by selecting '+' button seen in the lefthand side).
  7. Paste the batch request prepared in step 3 in request section and press execute button.I have pasted screenshots before and after batch processing.
  8. Go to se11 and check our scarr table for new entries.Batch process success😌
How to call batch operation from SAPUI5 application.
Note:A sample read request will look like this.See batch boundary is different.Dont forget it when adding header
--batch_zmybatch
Content-Type: application/http
Content-Transfer-Encoding: binary

GET ProductCollection('HT-1000') HTTP/1.1


--batch_zmybatch 
Content-Type: application/http
Content-Transfer-Encoding: binary

GET ProductCollection('HT-1001') HTTP/1.1

 
--batch_zmybatch--

If you enjoyed this post, Please Share!!

5 comments :

  1. Thanks for sharing this blog...

    ReplyDelete
  2. Please explain when to use(redefine) Update_entity and Changesetend-changesetbegin methods.

    ReplyDelete
    Replies
    1. Update_entity is to use when you have to change a single entry. Change set is to use when you need multiple operations in single call like two update operation, update-read operation,multiple delete operation etc.

      Delete
  3. Thhis was a lovely blog post

    ReplyDelete

Powered by Blogger.