REST Client
Hazelcast provides a REST interface: it provides an HTTP service in each cluster member so that you can access your map
and queue
using HTTP protocol. Assuming mapName
and queueName
are already configured in your Hazelcast, its structure is shown below:
http://member IP address:port/hazelcast/rest/maps/mapName/key
http://member IP address:port/hazelcast/rest/queues/queueName
For the operations to be performed, standard REST conventions for HTTP calls are used.
REST client request listener service is not enabled by default. You should enable it on your cluster members to use REST client. It can be enabled using the DATA endpoint group; see the Using the REST Endpoint Groups section.The previously used hazelcast.rest.enabled system property is deprecated.
|
All parameters that are used in REST API URLs, such as the distributed data structure and key names, must be
URL encoded when composing a call. As an example, name.with/special@chars
parameter value would be encoded as name.with%2Fspecial%40chars .
|
REST Client GET/POST/DELETE Examples
In the following GET
, POST
and DELETE
examples, assume that your cluster members are as shown below.
All of the requests below can return one of the following responses in case of a failure. |
Creating/Updating Entries in a Map for REST Client
You can put a new key1/value1
entry into a map by using POST
call to
http://<member IP address>:<port>/hazelcast/rest/maps/mapName/key1
URL.
This call’s content body should contain the value of the key.
Also, if the call contains the MIME type, Hazelcast stores this information, too.
An example POST
call is shown below.
It returns the following response if successful:
Retrieving Entries from a Map for REST Client
If you want to retrieve an entry, you can use a GET
call
to http://<member IP address>:<port>/hazelcast/rest/maps/mapName/key1
.
You can also retrieve this entry from another member of your cluster, such as
http://<another member IP address>:<port>/hazelcast/rest/maps/mapName/key1
.
An example of a GET
call is shown below.
It returns the following response if there is a corresponding value:
This GET
call returned a value, its length and also the MIME type
(text/plain
) since the POST call example shown above included the MIME type.
It returns the following if there is no mapping for the given key:
Removing Entries from a Map for REST Client
You can use a DELETE
call to remove an entry. An example DELETE
call is shown below with its response.
If you leave the key empty as follows, the DELETE
call deletes all entries from the map.
Offering Items on a Queue for REST Client
You can use a POST
call to create an item on the queue. An example is shown below.
The above call is equivalent to HazelcastInstance.getQueue("myEvents").offer("foo");
.
It returns the following if successful:
It returns the following if the queue is full and the item is not able to be offered to the queue:
Retrieving Items from a Queue for REST Client
You can use a DELETE
call for retrieving items from a queue. Note that you should state the poll timeout while polling for queue events by an extra path parameter.
An example is shown below (10 being the timeout value).
The above call is equivalent to HazelcastInstance.getQueue("myEvents").poll(10, SECONDS);
. Below is the response.
When the timeout is reached, the response is No Content
success, i.e., there is no item on the queue to be returned.
Checking the Status of the Cluster for REST Client
Besides the above operations, you can check the status of your cluster, an example of which is shown below.
The response is as follows:
RESTful access is provided through any member of your cluster. You can even put an HTTP load-balancer in front of your cluster members for load balancing and fault tolerance.
You need to handle the failures on REST polls as there is no transactional guarantee. |