Commit 8b95c740 authored by legoc's avatar legoc

added JSON example in the publisher subscriber example.

parent a8631545
......@@ -66,6 +66,11 @@
<artifactId>cameo-api-java</artifactId>
<version>0.1.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
<build>
......
......@@ -16,11 +16,23 @@
package fr.ill.ics.cameo.examples.publishersubscriber;
import org.json.simple.JSONObject;
import fr.ill.ics.cameo.Application;
import fr.ill.ics.cameo.RemoteException;
public class PublisherApplication {
public static String serializeToJSON(String message, int i) {
JSONObject object = new JSONObject();
object.put("message", message);
object.put("value", i);
return object.toJSONString();
}
public static void main(String[] args) {
Application.This.init(args);
......@@ -45,7 +57,7 @@ public class PublisherApplication {
while (true) {
// Send a message.
publisher.send("message " + i);
publisher.send(serializeToJSON("a message", i));
i++;
// Sleep for 1s.
......
......@@ -16,12 +16,28 @@
package fr.ill.ics.cameo.examples.publishersubscriber;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import fr.ill.ics.cameo.Application;
import fr.ill.ics.cameo.RemoteException;
import fr.ill.ics.cameo.Server;
public class SubscriberApplication {
public static JSONObject deserializeToJSON(String jsonMessage) {
JSONParser parser = new JSONParser();
JSONObject object = null;
try {
object = (JSONObject)parser.parse(jsonMessage);
} catch (ParseException e) {
System.err.println("Parse error");
}
return object;
}
public static void main(String[] args) {
Application.This.init(args);
......@@ -49,6 +65,12 @@ public class SubscriberApplication {
String message = subscriber.receiveString();
if (message != null) {
System.out.println("Received: " + message);
JSONObject object = deserializeToJSON(message);
System.out.println("\tmessage : " + (String)object.get("message"));
System.out.println("\tvalue : " + (Long)object.get("value"));
} else {
break;
}
......
<?xml version="1.0" encoding="UTF-8"?>
<config port="8000" max_applications="20" debug="ON" polling_time="100">
<config port="9000" max_applications="20" debug="ON" polling_time="100">
<applications>
<application name="publisher-app" starting_time="0" retries="0" stopping_time="1" stream="yes" multiple="no" restart="no">
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment