How to use Hazelcast with restful?

1,105 views
Skip to first unread message

Stacked Stacked

unread,
Jan 20, 2014, 3:14:26 AM1/20/14
to haze...@googlegroups.com

i want to u use Hazelcast in my Java applications but i also have .net applications which needs to get/set data from/to hazel cache. I tought to use rest-way. I have 2 questions...

1) How can i post, get a complex type? If i have a Person object with fields name(String), age(Integer), birthDate(Date), sex (Enum), how should i post those info and how should i parse person info?

2) I have a cached IMap. After i post data "three" with key "3" from a Poster plugin, at java side map.get("3") returns something "RestValue{contentType='text/plain;charset=utf-8', value="three"}". I expect this code to return just "three" without any cast operation.

I will be pleased if you give information about this issues.

Thanks in advance...

M. Sancar Koyunlu

unread,
Jan 20, 2014, 3:44:33 AM1/20/14
to haze...@googlegroups.com
Hi, 
Currently our rest support is limited to String and byte[]. You can use your own serialization methods on top of them.
With memcache you don't need to use your own serialization but there is a limitation there too. If you set form Memcache client get from hazelcast you will see the RestValue, as you already did. But if you use just Memcache for all your put/get operations, you will get what you put.  

And, we will have a c# client for 3.2 which will solve all your problems. 





--
You received this message because you are subscribed to the Google Groups "Hazelcast" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hazelcast+...@googlegroups.com.
To post to this group, send email to haze...@googlegroups.com.
Visit this group at http://groups.google.com/group/hazelcast.
To view this discussion on the web visit https://groups.google.com/d/msgid/hazelcast/8b649c37-334d-426b-b2a2-e8855e5152a9%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

dieg...@gmail.com

unread,
Feb 9, 2014, 11:27:06 PM2/9/14
to haze...@googlegroups.com
Hi mustafa, please can you point us to an example to achieve that?

I'm trying this, without success:
 
    Stock stock = new Stock("C2B2","C2B2",Math.random() * 100.0);
   
HttpURLConnection urlCon;
   
ObjectOutputStream out;
        URL url
;
       
try {
            url
= new URL("http://127.0.0.1:5701/hazelcast/rest/maps/C2B2/C2B2");
            urlCon
= (HttpURLConnection) url.openConnection();
            urlCon
.setRequestMethod("POST");
            urlCon
.setRequestProperty("Content-Type", "application/x-java-serialized-object");
            urlCon
.setDoOutput(true);
           
out = new ObjectOutputStream(urlCon.getOutputStream());
           
out.writeObject(stock);
            urlCon
.connect();
           
out.flush();
           
out.close();
            urlCon
.disconnect();
       
} catch (IOException e) {
            e
.printStackTrace();
       
}

M. Sancar Koyunlu

unread,
Feb 10, 2014, 5:39:04 AM2/10/14
to haze...@googlegroups.com
Hi, I wrote a sample code, take a look:


  1 import com.hazelcast.core.Hazelcast;
  2 import com.hazelcast.core.HazelcastInstance;
  3 import com.hazelcast.core.IMap;
  4 
  5 import java.io.*;
  6 import java.net.HttpURLConnection;
  7 import java.net.URL;
  8 
  9 
 10 class Stock implements Serializable {
 11     int a;
 12     String b;
 13 
 14     public Stock() {
 15 
 16     }
 17 
 18     public Stock(int a, String b) {
 19         this.a = a;
 20         this.b = b;
 21     }
 22 
 23 }
 24 
 25 public class XMath {
 26     public static void main(String[] args) throws FileNotFoundException {
 27         final HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance();
 28 
 29         Stock stock = new Stock(1, "mystock");
 30         final IMap<Object, Object> c2B2 = hazelcastInstance.getMap("C2B2");
 31         System.out.println(c2B2.size());//size should print 0 here
 32         try {
 33             HttpURLConnection urlCon;
 34             URL url = new URL("http://192.168.2.201:5701/hazelcast/rest/maps/C2B2/C2B2");
 35             urlCon = (HttpURLConnection) url.openConnection();
 36             urlCon.setRequestMethod("POST");
 37             urlCon.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
 38             urlCon.setDoOutput(true);
 39             urlCon.setDoInput(true);
 40 
 41 
 42             ByteArrayOutputStream baos = new ByteArrayOutputStream();
 43             ObjectOutputStream oos = new ObjectOutputStream(baos);
 44             oos.writeObject(stock);
 45 
 46             final OutputStream outputStream = urlCon.getOutputStream();
 47             final DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
 48             final byte[] b = baos.toByteArray();
 49             dataOutputStream.writeInt(b.length); //writeLength of serialized object
 50             dataOutputStream.write(b); //writeBytes
 51 
 52             oos.close();
 53             baos.close();
 54             urlCon.disconnect();
 55 
 56             //read response to make the operation sync
 57             BufferedReader reader = new BufferedReader(new InputStreamReader(urlCon.getInputStream()));
 58             StringBuilder builder = new StringBuilder(100);
 59             String line;
 60             while ((line = reader.readLine()) != null) {
 61                 builder.append(line);
 62             }
 63             reader.close();
 64 
 65         } catch (IOException e) {
 66             e.printStackTrace();
 67         }
 68 
 69         System.out.println(c2B2.size());//size should print 1 here
 70 
 71         try {
 72             String getURL = "http://192.168.2.201:5701/hazelcast/rest/maps/C2B2/C2B2";
 73             HttpURLConnection urlCon = (HttpURLConnection) (new URL(getURL)).openConnection();
 74             urlCon.setRequestMethod("GET");
 75             urlCon.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
 76             urlCon.setDoInput(true);
 77 
 78             // Get the response
 79             final InputStream inputStream = urlCon.getInputStream();
 80             final DataInputStream dataInputStream = new DataInputStream(inputStream);
 81             final int byteLength = dataInputStream.readInt(); //readLength of serialized objects
 82             final byte[] bytes = new byte[byteLength];
 83             dataInputStream.readFully(bytes);  //read bytes
 84 
 85             //byte array convert to object
 86             final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
 87             final ObjectInputStream in = new ObjectInputStream(byteArrayInputStream);
 88             final Stock s = (Stock) in.readObject();
 89 
 90             System.out.println(s.a + " " + s.b);//printing my stock class fields to check
 91             //should print 1 mystock
 92             in.close();
 93             byteArrayInputStream.close();
 94 
 95             urlCon.disconnect();
 96         } catch (Exception e) {
 97             e.printStackTrace();
 98         }
 99 
100     }
101 }
 


dieg...@gmail.com

unread,
Feb 10, 2014, 11:00:14 AM2/10/14
to haze...@googlegroups.com
Thank you very much mustafa, this code works very well, but now I realized that the event doesn't trigger, and my listeners only hear what the native clients do, but not the REST client activity. Is there a setting to enable this feature? maybe through http header.  thank you!

dieg...@gmail.com

unread,
Feb 10, 2014, 12:48:01 PM2/10/14
to haze...@googlegroups.com, dieg...@gmail.com
Hi mustafa, I see what happens, when post through REST client, the map saves a RestValue object instead of a Stock object, and the clients with the listeners have to be aware of it, that is why it was crashing. again, thanks!
Reply all
Reply to author
Forward
0 new messages