Infinispan RESTful interface

User Rating: 5 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Active
 

In this article, we will show you how to deploy Infinispan on JBoss AS 6. The application deployed is the RESTful interface to Infinispan which can be used by every kind of client to get/put elements in the cache. 

If you need to access the Infinispan data grid from any kind of client (which is able to consume a RESTful service), then Infinispan REST server is what you need !

Download Infinispan server-rest application from here

This tutorial has been tested with the release 4.2.1 of Infinispan and JBoss AS 6

Now unzip the file infinispan-4.2.1.FINAL-server-rest.zip and extract the file infinispan.war which will be deployed on JBoss AS 6.

Please note, due to issues with the VFS of JBoss AS 6, some libraries (namely scala-library-2.8.1.jar) cause the application deployment to fail. In order to solve this issue you can add to your WEB-INF folder of infinispan.war a JBoss AS metadata file (jboss-scanning.xml) which excludes scala library from the deployment.

Here's jboss-scanning.xml

<scanning xmlns="urn:jboss:scanning:1.0">

   <path name="WEB-INF/lib/scala-library-2.8.1.jar">
     <exclude name="scala" recurse="true"/>
   </path>

</scanning>

This is how your infinispan.war archive will look like:

infinispan.war

--WEB-INF

   lib

   classes

   --- web.xml

   --- jboss-scanning.xml

Ok. Now launch JBoss AS 6 and point your browser to the infinispan REST server:

http://localhost:8080/inifinispan

infinispan rest

You will hit a welcome page which describes how basically the REST server works:

In order to insert elements in the cache, you can issue an HTTP PUT and POST request.

PUT /{cacheName}/{cacheKey}

POST /{cacheName}/{cacheKey}

In order to retrieve elements from the cache you can use HTTP GET request:

GET /{cacheName}/{cacheKey}

In order to delete elements from the cache you can issue an HTTP DELETE request:

DELETE /{cacheName}/{cacheKey}

By default infinispan has just the "___defaultcache" registered, so if you want to try out of the box the REST server, just create a REST client.

Following here is a jQuery client which puts an entry in the cache:

<html>
<head>

<title>jQuery RESTful client performing PUT</title>
 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
 
 <script type="text/javascript">

$.ajax({
type: "PUT",
url: '/infinispan/rest/___defaultcache/bucket1',
data: "Data in cache",
success: function(response){
$("#mydiv").html(response);
}
});

 
 </script>
 
</head>
<body>
<div id="mydiv"><p> </p></div>
 </body>
</html>

And here's the corresponding page which performs a get on the bucket1 key:

<html>
<head>
 <title>jQuery RESTful client performing GET</title>
 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
 
 <script type="text/javascript">

$.ajax({
type: "GET",
url: '/infinispan/rest/___defaultcache/bucket1',
data: "",
success: function(response){
 $("#mydiv").html(response);

}
});

 
 </script>
 
</head>
<body>
<div id="mydiv"><p> </p></div>
 </body>
</html>

Do I need to download jQuery to run this sample ? no at all. jQuery is accessed on the cloud via <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>. If you cannot reach the web from there, of course download jquery.js and place it on the server's path.

 

Follow us on Twitter