The current arangodb-java-driver supports the usage of ArangoDB’s batch and asynchronous interface. This post will guide you through the usage of these features.

The batch interface

The batch interface enables the user to stack a series of calls and execute them in a batch request. Each stacked request returns a request id that can be used to retrieve the single results from the batch response. So how do you use this feature in the java driver ?

First we create an instance of the java driver:

Now we start the batch mode …

So now every driver method will return an instance of the BaseEntity class which basically represents the response of a request to ArangoDB. The return code will always be 206 (Partial Content) and the requestId attribute will contain an unique request id.

As an example we will create a view collection, provoke an error in one of these requests and read all collections from the database:

Now as there are five calls on our call stack we can execute them in one single batch request.
Please note, that the batch request returns a DefaultEntity as an implementation of the abstract BaseEntity.

The method executeBatch sets the driver back to normal execution mode before executing the batch request. Hence the RequestId is now null.

Now lets analyze the single results. Note that we have to wait for each job to finish …

If for any reason one wants to cancel the batch mode the method cancelBatchMode will do the trick. Please note that every stacked request is lost when the batch mode is canceled prematurely.

The async interface

ArangoDB provides two methods of executing client requests asynchronously. Fire and Forget will execute requests without a chance for the user to retrieve any result. Async Execution and later Result Retrieval will execute requests asynchronously too but returns a jobId in the response that can be used to request the result.

We implemented this quite similar to the batch interface by enabling the user to “turn on/turn off” the desired asynchronous mode.

In our example we will explain the Async Execution and later Result Retrieval mode, there is not much to show for Fire and Forget.
So lets reuse the driver from the batch example and activate the async mode…

The boolean parameter tells that we do not want to start the async Fire and Forget mode.
So until we stop the async mode every request will return a status code 202 (Accepted). To actually get the results the driver keeps the job ids and offers the method getLastJobId to retrieve the id of the last request. To get all JobIds the method getJobIds can be used.

So lets replay the example from the batch interface section above in async mode …

Now we load all jobIds :

So how can we use these JobIds? Well, primarily we need them to request the result of an async job. Furthermore one can request the execution state for a job from the server or even delete it.
We will now load the result from our previous example.

Note that we will have to check first that the asynchronous jobs have been executed on the server:

Both interfaces can really help to speed up your application. Give it a try and enjoy.

More Java related articles and tutorials: