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...
--
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.
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();
}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 }
To view this discussion on the web visit https://groups.google.com/d/msgid/hazelcast/df6675e3-b84a-47f8-8fcd-e634ad85ecc7%40googlegroups.com.