Getting started with GWPA: $batch with Toolkit for Java SE
In the meantime, as of July 2014, GWPA has been retired.
Please find more information here: SAP Gateway Developer Tools – Bridging the Past, Present and Future
This document is a follow-up of Getting started with GWPA: Toolkit for Java SE
Please refer to that document for an introduction about the Java SE Toolkit, which is provided by SAP NetWeaver Gateway Accelerator (GWPA).
That document is not a prerequisite for following this $batch-tutorial, however it makes sense to read that before.
Note: Sample code.
For your convenience, I’ve attached the result of this tutorial, the Java source, to this document.
Please find it below.
It is a text file that you can use for your convenience.
Instead of copying code snippets, you can view and copy the full coding to your IDE.
There’s only one thing you have to do: enter your user/pw for the sample service.
In order to follow this tutorial you need the following prerequisites
Install JavaSE toolkit along with GWPA
Furthermore you need to have access to an OData service.
In this tutorial we use the service ZGWSample_SRV, which is located on the Gateway Demo System, such that everybody can access it.
Please refer to the following document, where all links and details about all prerequisites are collected: Getting started with GWPA: Prerequisites
This tutorial consists of the following 3 steps:
1) Create project
2) Adapt the generated code
3) Write the $batch code
1) Create the project
Start your Eclipse and create a Starter Application Project:
Eclipse main menu -> File -> New -> Project -> OData Development -> Starter Application Project
(Alternative: press Ctrl + N in order to open the “New” wizard)
Enter a project name, e.g. BatchTest
In the drop down box, select Java SE (If the drop down is empty, install the Java SE toolkit, see Prerequisites)
Enter a package name, e.g. com.my.company.tests.batch (This package will be generated)
Enter a name for the Java class which will be generated and which will contain the main-method, e.g. BatchTest
Press Next, select the template Basic Application and press Finish.
2) Adapt the generated code
After successful generation, the class “BatchTest.java” is opened in the Java editor.
In my previous document, I’ve explained the meaning of the generated code.
In this tutorial, we don’t need that and delete most of it.
delete some code
– In detail, delete the following methods:
– Delete the usage of these methods in the main() method
– Delete the imports
– Delete the following 2 catch-blocks: catch (ParserException) and catch (MarshalerException)
Uncomment some code
Uncomment all the rest, which are the main() and the initializeRestClient() method.
Now, there are lots of compile errors, due to missing import statements.
Thus, we have to add the required imports:
press Ctrl + Shift + O,
Alternatively, click with right mouse button into the editor and from the context menu choose Source -> Organize Imports
Add required details
Still, there are compile errors.
Within the InitializeRestClient() method, we have to specify host, port, credentials, client, and (optionally) we have to add proxy settings (if working from corporate network).
if you’re working from a corporate network, it might be required to add the proxy settings.
This would be done inside this method.
It is not enough, to enter the proxy information in the Eclipse “Network Settings” preference page, it has to be set in the Java code.
Add the following lines at the end of the method initializeRestClient():
In case you need it, the snippet for proxy bypass is the following:
– Service: In our example, we use a sample service from the SAP Netweaver Gateway Demo System.
– Service host: sapes1.sapdevcenter.com
– Port: 443
– The user and password, which are provided after registration, have to be entered in the code as well.
– Furthermore, please don’t forget to set the parameter “USE_SSL” to true.
– SAP client: In our example, when using this connection, we don’t need to specify the SAP_CLIENT, since the default client will be used.
Finally, after adjustments, our class looks as follows (click to enlarge):
There’s still one compile error left, because RestClientException is not declared to be thrown, but don’t mind, it will be thrown after the code which we will write in the following section.
3) Writing the $batch code
We create a method called e.g. executeBatchRequest() and invoke it in the main() method.
Basic usage of the $batch
The content of the executeBatchRequest() method looks as follows:
And the main() method:
Behind the curtain
The design of the $batch is nice and simple and follows the paradigm of the HTTP request:
There’s a separate implementation of the IRestRequest interface:
the class BatchRequest, which itself inherits from the RestRequest implementation
The BatchRequest object is configured with the URL (which is expected to contain the “$batch” token)
The BatchRequest functions like an outer frame, which itself hosts the actual requests as inner requests.
These inner requests are the normal implementations of IRestRequest that we’ve already used in our first Java SE tutorial.
These inner requests contain the URL of the actual request, e.g. a READ operation on a collection, or any other request.
They are added to the (outer) BatchRequest by calling the method
So, the structure of the objects in the code could be depicted as follows:
As for the response of the request, the design is the same:
The response to a BatchRequest is a BatchResponse.
The instance of BatchResponse contains the “normal” RestResponse instances and provides an access method;
Change operation with $batch
When doing a modifying operation, like PUT, POST or DELETE, we have to use a ChangeSet instead of ReadOperation.
As the name insinuates, the ChangeSet can carry several requests (only such modifying request, no READ is allowed)
You have to explicitly invoke the default constructor for the ChangeSet, then add the requests to it.
Don’t try to use the method which does both in one step, as it will fail (probably a bug).
Don’t forget to configure the Content Type of the modifying request (the one that is inside the ChangeSet. See helper method) otherwise the operation will fail.
The result to such BatchRequest is again a BatchResponse, but the inner response type is a ChangeSetResponse.
Let’s do an example where we create a product in a BatchRequest.
In order to verify the creation, we add a read request before and after the product creation, which prints the amount of products.
So we have 3 requests in our batch, each of it prints an info to the console.
Regarding the creation, we reuse the code snippet from our basic Java SE Toolkit tutorial.
The sample code for the BatchRequest with ChangeSet:
The sample code for the helper methods:
And the resulting console output:
This example has described the very basic usage of the $batch request.
The intention was to show that the Java SE Toolkit in GWPA provides support for $batch requests.
Of course, there’s much more that can be done with it.
If you have code-snippets you would like to share, please go ahead and add them to the comment-section.
Thanks in advance!
The official documentation for the batch request can be found here:
Or navigate to it as follows:
https://help.neo.ondemand.com -> Platform Solutions -> SAP NetWeaver Gateway Productivity Accelerator -> Developer Guide -> Service Consumption -> OData Toolkit for Java Platform -> Generating Proxy for Java SE