Batch Process in SAP Net Weaver Gateway
Business Scenario:
There may be certain scenarios wherein instances may logically bind together and requires to be handled or processed in conjunction with the same logical unit of work.
Example:
Take for instance of sales order, wherein update of two or more related entities may be required and must be processed together in a single request.SAP Net Weaver Gateway can be used to process such scenarios with its capability to execute multiple operations in a single request.
Gateway provides an OData API into a SAP system. This uniform interface supports a given set of operations like Create, Read, Update and Delete. However, based on the business requirements, it may become relevant for clients to send multiple operations in a single HTTP request.Begin with SP04, SAP Net Weaver Gateway offers the capability for client operations to batch multiple operations into a single HTTP request.
Pre-requisites:
- Access to SAP Net Weaver ABAP 7.02 SP7 or system with higher configuration, having SAP Net Weaver Gateway ABAP add-ons installed.
- Installation of back-end SAP ECC 6.0 or higher version along with the IW_BEP gateway add-on(Optional)
- Google chrome with Advanced Rest Client (ARC) extension or other compatible REST Client that can send RAW HTTP requests.
- Basic understanding of ABAP Development.
Batch request consists of two components
- Batch request header
- Batch request body
Batch request header
Batch request are sent as a single HTTP POST request to the batch end point of the service.
E.g.: https://:base service URL/sap/opu/odata/iwfnd/Servicename/$batch
The batch request must consists of a Content-Type header that specifies a content type of “multipart/mixed” and a “boundary” specification.
Content-Type:multipart/mixed boundary=batch
For reference please check the below screenshot.
Batch request body:
The body of a batch request is comprised of an ordered series of retrieve operations and/or change sets. Retrieve operations are generally Query or Read operations executed with the HTTP GET method.
When modify such operations, they are referred as “Change Sets” in OData batch processing terms. Change sets can consists of Create, Update or Delete operations executed using the POST, PUT and DELETE methods.
Handling of a Batch Request in Gateway
Retrieve and change set operations are handled differently by Gateway.
Retrieve operations:
Each retrieve operations such as a Query or Read operation within a $batch request will be transferred separately form gateway to the data provider application in the back-end system for processing.
Change Set Operations:
Every change set is to be treated as one Logical Unit of Work (LUW), all operations of a change set will be sent at once from gateway to the data provider application in the backend system for processing.
Result:
Results from all operations will be collected at the Gateway system and then sent as one HTTP response to the OData Consumer.
Batch Process: In $batch functionality delivers two additional methods to the /IWBEP/IF_MGW_APPL_SRV_RUNTIME data provider interface.
- CHANGESET_BEGIN
- CHANGESET_END
In SAP Gateway by default, only one operation per change set is allowed. If multiple operations are continued in the change set, the default implementation will throw an exception.
In Order to process multiple operations in a change set, the default implementation must be overwritten by the application using these methods (Changeset_begin and Changeset_end)
In SAP we don’t have to write any additional logic to implement batch, just have to redefine the methods as shown in below.
/IWBEP/IF_MGW_APPL_SRV_RUNTIME~CHANGESET_BEGIN and
/IWBEP/IF_MGW_APPL_SRV_RUNTIME~CHANGE SET_END
Create a Project and then right click on the Data Model folder and select import DDIC structure and give the entity type name as ….. and select the required properties for the entity type.
Then click on Generate Run time Objects. It displayed “Generate Objects Successfully“, that time generated all classes automatically.
Right click on class ends with DPS_EXT and select go to ABAP workbench, then redefine the following methods.
/IWBEP/IF_MGW_APPL_SRV_RUNTIME~ CHANGESET_BEGIN
/IWBEP/IF_MGW_APPL_SRV_RUNTIME~CHANGESET_END
Read Operation with BATCH:
In this below process I am getting two entity sets of data by using single request through Batch request.
URL: /sap/opu/odata/sap/ZHK_BATCH_REQUEST1_SRV/$batch
HEADER:
Authorization: SAP ID & PASSWORD
x-csrf-token: Enter Token
Content-Type: multipart/mixed; boundary=batch
Request Body:
–batch
Content-Type: application/http
Content-Transfer-Encoding: binary
Accept: application/xml
GET EmpDSet(EmpId=’1′) HTTP/1.1 “Read Operation based on Employee ID
–batch
Content-Type: application/http
Content-Transfer-Encoding: binary
Accept: application/xml
GET EmpDSet HTTP/1.1 “Getting All Employees Data(Set Data)
–batch—
Create Operation with BATCH:
Example of executing a batch request containing multiple create operations.
URL: /sap/opu/odata/sap/ZHK_BATCH_REQUEST1_SRV/$batch
HEADER :
Authorization: SAP ID & PASSWORD
x-csrf-token: Enter Token
Content-Type: multipart/mixed; boundary=batch
Request Body:
–batch
Content-Type: multipart/mixed; boundary=changeset
–changeset
Content-Type: application/http
Content-Transfer-Encoding: binary
POST EmpDSet HTTP/1.1
Content-Type: application/atom+xml
Content-Length: 588
https://sapserver.mouritech.in:8000/sap/opu/odata/sap/ZHK_BATCH_REQUEST1_SRV/EmpDSet(‘1’)
2015-08-31T05:52:12Z
7
TEST7
KKD
1234
–changeset
Content-Type: application/http
Content-Transfer-Encoding: binary
POST EmpDSet HTTP/1.1
Content-Type: application/atom+xml
Content-Length: 588
https://sapserver.mouritech.in:8000/sap/opu/odata/sap/ZHK_BATCH_REQUEST1_SRV/EmpDSet(‘1’)
2015-08-31T05:52:12Z
8
TEST8
KKD
1234
–changeset–
–batch–
Output Screen Shot:
Result in back-end:
Update Operation with BATCH:
Example of executing a batch request containing multiple Update operations (Change names).
URL: /sap/opu/odata/sap/ZHK_BATCH_REQUEST1_SRV/$batch
HEADER :
Authorization: SAP ID & PASSWORD
x-csrf-token: Enter Token
Content-Type: multipart/mixed; boundary=batch
Before Update:
Response Body:
–batch
Content-Type: multipart/mixed; boundary=changeset
–changeset
Content-Type: application/http
Content-Transfer-Encoding: binary
PUT EmpDSet(‘8’) HTTP/1.1
Content-Type: application/atom+xml
Content-Length: 588
https://sapserver.mouritech.in:8000/sap/opu/odata/sap/ZHK_BATCH_REQUEST1_SRV/EmpDSet(‘1’)
2015-08-31T05:52:12Z
8
uPDATED
rjy
56
–changeset
Content-Type: application/http
Content-Transfer-Encoding: binary
PUT EmpDSet(‘9’) HTTP/1.1
Content-Type: application/atom+xml
Content-Length: 588
https://sapserver.mouritech.in:8000/sap/opu/odata/sap/ZHK_BATCH_REQUEST1_SRV/EmpDSet(‘1’)
2015-08-31T05:52:12Z
9
UPDATED
TEST
78
–changeset–
–batch–
Output Screen Shot:
Delete Operation with BATCH:
URL: /sap/opu/odata/sap/ZHK_BATCH_REQUEST1_SRV/$batch
HEADER :
Authorization: SAP ID & PASSWORD
x-csrf-token: Enter Token
Content-Type: multipart/mixed; boundary=batch
Before:
Response Body:
–batch
Content-Type: multipart/mixed; boundary=changeset
–changeset
Content-Type: application/http
Content-Transfer-Encoding: binary
DELETE EmpDSet(EmpId=’7′) HTTP/1.1
–changeset
Content-Type: application/http
Content-Transfer-Encoding: binary
DELETE EmpDSet(EmpId=’8′) HTTP/1.1
–changeset–
–batch–
Output Screen Shot:
Hope it’s useful ???? Harikrishna
New NetWeaver Information at SAP.com
Very Helpfull