OW2 Consortium joram

Compare Revisions

Ignore whitespace Rev 6457 → Rev 6458

/trunk/joram/samples/src/joram/rest/PerfProducer.java
1,20 → 1,7
package rest;
 
import java.net.URI;
import java.util.Set;
import java.util.HashMap;
 
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation.Builder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Link;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;
 
import org.glassfish.jersey.client.ClientConfig;
 
/**
* MessageProducer sending messages on queue or topic for performance statistics.
*/
51,47 → 38,31
}
 
public void run() {
ClientConfig config = new ClientConfig();
Client client = ClientBuilder.newClient(config);
WebTarget target = client.target(getBaseURI());
System.out.println(target.getUri());
RestProducer prod = new RestProducer("http://localhost:8989/joram/", "queue");
 
// lookup the destination
Builder builder = target.path("jndi").path("queue").request();
Response response = builder.accept(MediaType.TEXT_PLAIN).head();
System.out.println("== lookup \"queue\" = " + response.getStatus());
print(response.getLinks());
// byte[] content = new byte[MsgSize];
// for (int i = 0; i< MsgSize; i++)
// content[i] = (byte) (i & 0xFF);
 
URI uriCreateProd = client.target(response.getLink("create-producer"))
// .queryParam("name", "prod1")
.getUri();
 
// Create the producer
response = client.target(uriCreateProd)
.request()
.accept(MediaType.TEXT_PLAIN).post(null);
 
URI uriCloseProd = response.getLink("close-context").getUri();
URI uriSendNextMsg = response.getLink("send-next-message").getUri();
 
byte[] content = new byte[MsgSize];
StringBuffer strbuf = new StringBuffer();
for (int i = 0; i< MsgSize; i++)
content[i] = (byte) (i & 0xFF);
 
strbuf.append('0');
String content = strbuf.toString();
long dtx = 0;
long start = System.currentTimeMillis();
 
for (int i=0; i<(Round*NbMsgPerRound); i++) {
// Send next message
// msg.setLongProperty("time", System.currentTimeMillis());
// msg.setIntProperty("index", i);
HashMap<String, Object> header = new HashMap<String, Object>();
header.put("CorrelationID", "0123456789");
HashMap<String, Object> props = new HashMap<String, Object>();
props.put("time", new String[]{"" + System.currentTimeMillis(), Long.class.getName()});
props.put("index", new String[]{"" + i, Integer.class.getName()});
response = client.target(uriSendNextMsg).request()
.accept(MediaType.TEXT_PLAIN)
.post(Entity.entity(new String(content), MediaType.TEXT_PLAIN));
// prod.sendBytesMessage(content, header, props);
prod.sendTextMessage(content, header, props);
uriSendNextMsg = response.getLink("send-next-message").getUri();
 
if ((i%NbMsgPerRound) == (NbMsgPerRound-1)) {
long dtx1 = (i * 1000L) / mps;
long dtx2 = System.currentTimeMillis() - start;
116,18 → 87,6
System.out.println("| sender wait=" + dtx + "ms");
 
// close the producer
response = client.target(uriCloseProd).request().accept(MediaType.TEXT_PLAIN).delete();
System.out.println("== close-producer = " + response.getStatus());
print(response.getLinks());
prod.close();
}
private static URI getBaseURI() {
return UriBuilder.fromUri("http://localhost:8989/joram/").build();
}
 
private static void print(Set<Link> links) {
System.out.println(" link :");
for (Link link : links)
System.out.println("\t" + link.getRel() + " : " + link.getUri());
}
}
/trunk/joram/samples/src/joram/rest/RestProducer.java
New file
0,0 → 1,141
package rest;
 
import java.net.URI;
import java.util.HashMap;
import java.util.Properties;
import java.util.Set;
 
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation.Builder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Link;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;
 
import org.glassfish.jersey.client.ClientConfig;
 
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
 
 
public class RestProducer {
Client client = null;
URI uriCloseProd = null;
URI uriSendNextMsg = null;
boolean debug = false;
 
RestProducer(String uri, String dest) {
URI base = UriBuilder.fromUri(uri).build();
ClientConfig config = new ClientConfig();
client = ClientBuilder.newClient(config);
WebTarget target = client.target(base);
System.out.println("Use Rest/JMS interface: " + target.getUri());
 
// lookup the destination
Builder builder = target.path("jndi").path(dest).request();
Response response = builder.accept(MediaType.TEXT_PLAIN).head();
System.out.println("Lookup \"" + dest + "\" -> " + response.getStatus());
if (debug) print(response.getLinks());
URI uriCreateProd = client.target(response.getLink("create-producer"))
// .queryParam("name", "prod1")
.getUri();
 
// Create the producer
response = client.target(uriCreateProd)
.request()
.accept(MediaType.TEXT_PLAIN).post(null);
 
uriCloseProd = response.getLink("close-context").getUri();
uriSendNextMsg = response.getLink("send-next-message").getUri();
}
void sendStringMessage(String content) {
Response response = client.target(uriSendNextMsg).request()
.accept(MediaType.TEXT_PLAIN)
.post(Entity.entity(content, MediaType.TEXT_PLAIN));
 
if (debug) {
System.out.println("== send-next-message = " + response.getStatus());
print(response.getLinks());
}
uriSendNextMsg = response.getLink("send-next-message").getUri();
}
public static final String BytesMessage = "BytesMessage";
public static final String MapMessage = "MapMessage";
public static final String TextMessage = "TextMessage";
void sendBytesMessage(byte[] content, HashMap<String, Object> header, HashMap<String, Object> props) {
HashMap<String, Object> msg = new HashMap<String, Object>();
 
msg.put("type", BytesMessage);
if ((header != null) && (header.size() > 0)) msg.put("header", header);
if ((props != null) && (props.size() > 0)) msg.put("properties", props);
msg.put("body", content);
Gson gson = new GsonBuilder().create();
String json = gson.toJson(msg);
 
if (debug) System.out.println("send json = " + json);
 
// Send next message
Response response = client.target(uriSendNextMsg).request()
.accept(MediaType.TEXT_PLAIN)
.post(Entity.entity(json, MediaType.APPLICATION_JSON));
 
if (debug) {
System.out.println("== send-next-message = " + response.getStatus());
print(response.getLinks());
}
uriSendNextMsg = response.getLink("send-next-message").getUri();
}
void sendTextMessage(String content, HashMap<String, Object> header, HashMap<String, Object> props) {
HashMap<String, Object> msg = new HashMap<String, Object>();
 
msg.put("type", TextMessage);
if ((header != null) && (header.size() > 0)) msg.put("header", header);
if ((props != null) && (props.size() > 0)) msg.put("properties", props);
msg.put("body", content);
Gson gson = new GsonBuilder().create();
String json = gson.toJson(msg);
 
if (debug) System.out.println("send json = " + json);
 
// Send next message
Response response = client.target(uriSendNextMsg).request()
.accept(MediaType.TEXT_PLAIN)
.post(Entity.entity(json, MediaType.APPLICATION_JSON));
 
if (debug) {
System.out.println("== send-next-message = " + response.getStatus());
print(response.getLinks());
}
uriSendNextMsg = response.getLink("send-next-message").getUri();
}
 
void close() {
Response response = client.target(uriCloseProd).request().accept(MediaType.TEXT_PLAIN).delete();
System.out.println("Close consumer -> " + response.getStatus());
if (debug) print(response.getLinks());
}
 
private static void print(Set<Link> links) {
System.out.println(" link :");
for (Link link : links)
System.out.println("\t" + link.getRel() + " : " + link.getUri());
}
}
Property changes:
Added: svn:mime-type
+ text/plain
/trunk/joram/samples/src/joram/rest/Producer.java
1,70 → 1,53
package rest;
 
import java.net.URI;
import java.util.Set;
import java.util.HashMap;
 
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation.Builder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Link;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;
 
import org.glassfish.jersey.client.ClientConfig;
 
public class Producer {
 
public static void main(String[] args) {
ClientConfig config = new ClientConfig();
Client client = ClientBuilder.newClient(config);
WebTarget target = client.target(getBaseURI());
System.out.println(target.getUri());
RestProducer prod = new RestProducer("http://localhost:8989/joram/", "queue");
// prod.debug = true;
testJSonMessage(prod);
// testStringMessage(prod);
// close the producer
prod.close();
}
 
// lookup the destination
Builder builder = target.path("jndi").path("queue").request();
Response response = builder.accept(MediaType.TEXT_PLAIN).head();
System.out.println("== lookup \"queue\" = " + response.getStatus());
print(response.getLinks());
URI uriCreateProd = client.target(response.getLink("create-producer"))
// .queryParam("name", "prod1")
.getUri();
static void testStringMessage(RestProducer prod) {
for (int i=0; i<10; i++) {
// Send next message
byte[] content = ("Test message#" + i).getBytes();
// prod.sendNextMessage("Test message#" + i);
HashMap<String, Object> header = new HashMap<String, Object>();
header.put("CorrelationID", "0123456789");
HashMap<String, Object> props = new HashMap<String, Object>();
props.put("time", new String[]{"" + System.currentTimeMillis(), Long.class.getName()});
props.put("index", new String[]{"" + i, Integer.class.getName()});
// prod.sendNextMessage(new String(content));
// prod.sendBytesMessage(content, header, props);
prod.sendTextMessage("Test message#" + i, header, props);
 
// Create the producer
response = client.target(uriCreateProd)
.request()
.accept(MediaType.TEXT_PLAIN).post(null);
System.out.println("Send message -> " + "Test message#" + i);
}
}
 
URI uriCloseProd = response.getLink("close-context").getUri();
URI uriSendNextMsg = response.getLink("send-next-message").getUri();
 
static void testJSonMessage(RestProducer prod) {
for (int i=0; i<10; i++) {
// Send next message
response = client.target(uriSendNextMsg).request()
.accept(MediaType.TEXT_PLAIN)
.post(Entity.entity("Test message#" + i, MediaType.TEXT_PLAIN));
byte[] content = ("Test message#" + i).getBytes();
// prod.sendNextMessage("Test message#" + i);
HashMap<String, Object> header = new HashMap<String, Object>();
header.put("CorrelationID", "0123456789");
HashMap<String, Object> props = new HashMap<String, Object>();
props.put("time", new String[]{"" + System.currentTimeMillis(), Long.class.getName()});
props.put("index", new String[]{"" + i, Integer.class.getName()});
// prod.sendNextMessage(new String(content));
// prod.sendBytesMessage(content, header, props);
prod.sendTextMessage("Test message#" + i, header, props);
 
System.out.println("== send-next-message = " + response.getStatus());
print(response.getLinks());
uriSendNextMsg = response.getLink("send-next-message").getUri();
System.out.println("Send message -> " + "Test message#" + i);
}
// close the producer
response = client.target(uriCloseProd).request().accept(MediaType.TEXT_PLAIN).delete();
System.out.println("== close-producer = " + response.getStatus());
print(response.getLinks());
}
private static URI getBaseURI() {
return UriBuilder.fromUri("http://localhost:8989/joram/").build();
}
 
private static void print(Set<Link> links) {
System.out.println(" link :");
for (Link link : links)
System.out.println("\t" + link.getRel() + " : " + link.getUri());
}
}
/trunk/joram/samples/src/joram/rest/PerfConsumer.java
1,20 → 1,12
package rest;
 
import java.net.URI;
import java.util.Set;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
 
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation.Builder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Link;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
 
import org.glassfish.jersey.client.ClientConfig;
 
public class PerfConsumer implements Runnable {
static int NbClient = 1;
static int Round = 50;
40,80 → 32,46
}
 
public void run() {
ClientConfig config = new ClientConfig();
Client client = ClientBuilder.newClient(config);
WebTarget target = client.target(getBaseURI());
System.out.println(target.getUri());
RestConsumer cons = new RestConsumer("http://localhost:8989/joram/", "queue");
 
// lookup the destination
Builder builder = target.path("jndi").path("queue").request();
Response response = builder.accept(MediaType.TEXT_PLAIN).head();
System.out.println("== lookup \"queue\" = " + response.getStatus());
print(response.getLinks());
for (int i=0; i<(Round*NbMsgPerRound); i++) {
// String msg = cons.receiveNextMsg();
HashMap<String, Object> msg = cons.receiveJSonMsg();
 
URI uriCreateCons = client.target(response.getLink("create-consumer"))
// .queryParam("name", "cons1")
.queryParam("idle-timeout", "120000")
.getUri();
response = client.target(uriCreateCons)
.request()
.accept(MediaType.TEXT_PLAIN).post(null);
String type = (String) msg.get("type");
Map header = (Map) msg.get("header");
Map props = (Map) msg.get("properties");
 
System.out.println("== create-consumer = " + response.getStatus());
print(response.getLinks());
Gson gson = new GsonBuilder().create();
if (RestConsumer.BytesMessage.equals(type)) {
byte[] body = gson.fromJson(msg.get("body").toString(), byte[].class);
} else if (RestConsumer.MapMessage.equals(type)) {
Map body = (Map) msg.get("body");
} else if (RestConsumer.TextMessage.equals(type)) {
String body = (String) msg.get("body");
} else {
System.out.println("Error receiving message");
break;
}
 
URI uriCloseCons = response.getLink("close-context").getUri();
URI uriReceiveNextMsg = response.getLink("receive-next-message").getUri();
for (int i=0; i<(Round*NbMsgPerRound); i++) {
response = client.target(uriReceiveNextMsg)
.queryParam("timeout", "30000")
.request()
.accept(MediaType.TEXT_PLAIN)
.get();
String msg = response.readEntity(String.class);
 
last = System.currentTimeMillis();
// int index = msg.getIntProperty("index");
int index = Integer.parseInt((String) ((ArrayList) props.get("index")).get(0));
if (i == 0) start = t1 = last;
 
// long dt = (last - msg.getLongProperty("time"));
// travel += dt;
long time = Long.parseLong((String) ((ArrayList) props.get("time")).get(0));
long dt = (last - time);
travel += dt;
 
if ((i%NbMsgPerRound) == (NbMsgPerRound -1)) {
long x = (NbMsgPerRound * 1000L) / (last - t1);
t1 = last;
System.out.println("#" + ((i+1)/NbMsgPerRound) + " x " + NbMsgPerRound + " msg -> " + x + " msg/s " + (travel/i));
}
if (response.getStatus() == Response.Status.OK.getStatusCode() && msg != null) {
// System.out.println("== receive-next-message = " + response.getStatus() + ", msg = " + msg);
} else {
System.out.println("ERROR consume msg = " + msg + ", response = " + response);
}
}
 
long x = (Round * NbMsgPerRound * 1000L) / (last - start);
System.out.println("Moy -> " + x + " msg/s ");
response = client.target(uriCloseCons)
.request()
.accept(MediaType.TEXT_PLAIN)
.delete();
 
System.out.println("== close-consumer = " + response.getStatus());
print(response.getLinks());
cons.close();
}
private static URI getBaseURI() {
return UriBuilder.fromUri("http://localhost:8989/joram/").build();
}
 
private static void print(Set<Link> links) {
System.out.println(" link :");
for (Link link : links)
System.out.println("\t" + link.getRel() + " : " + link.getUri());
}
 
 
}
/trunk/joram/samples/src/joram/rest/RestConsumer.java
New file
0,0 → 1,127
package rest;
 
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
 
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation.Builder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Link;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;
 
import org.glassfish.jersey.client.ClientConfig;
 
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
 
public class RestConsumer {
Client client = null;
 
URI uriCloseCons = null;
URI uriReceiveNextMsg = null;
boolean debug = false;
RestConsumer(String uri, String dest) {
URI base = UriBuilder.fromUri(uri).build();
ClientConfig config = new ClientConfig();
client = ClientBuilder.newClient(config);
WebTarget target = client.target(base);
System.out.println("Use Rest/JMS interface: " + target.getUri());
 
// lookup the destination
Builder builder = target.path("jndi").path("queue").request();
Response response = builder.accept(MediaType.TEXT_PLAIN).head();
System.out.println("Lookup \"" + dest + "\" -> " + response.getStatus());
if (debug) print(response.getLinks());
 
URI uriCreateCons = client.target(response.getLink("create-consumer"))
// .queryParam("name", "cons1")
.queryParam("idle-timeout", "120")
.getUri();
response = client.target(uriCreateCons)
.request()
.accept(MediaType.TEXT_PLAIN).post(null);
 
System.out.println("Create consumer -> " + response.getStatus());
if (debug) print(response.getLinks());
 
uriCloseCons = response.getLink("close-context").getUri();
uriReceiveNextMsg = response.getLink("receive-next-message").getUri();
}
String receiveStringMsg() {
Response response = client.target(uriReceiveNextMsg)
.queryParam("timeout", "30000")
.request()
.accept(MediaType.TEXT_PLAIN)
.get();
 
String msg = response.readEntity(String.class);
if (response.getStatus() == Response.Status.OK.getStatusCode() && msg != null) {
if (debug) System.out.println("== receive-next-message = " + response.getStatus() + ", msg = " + msg);
} else {
System.out.println("ERROR consume msg = " + msg + ", response = " + response);
}
return msg;
}
public static final String BytesMessage = "BytesMessage";
public static final String MapMessage = "MapMessage";
public static final String TextMessage = "TextMessage";
 
HashMap<String, Object> receiveJSonMsg() {
Response response = client.target(uriReceiveNextMsg)
.queryParam("timeout", "30000")
.request()
.accept(MediaType.APPLICATION_JSON)
.get();
String json = response.readEntity(String.class);
if (debug) System.out.println("Receive json = " + json);
HashMap<String, Object> msg = null;
if (response.getStatus() == Response.Status.OK.getStatusCode() && json != null) {
Gson gson = new GsonBuilder().create();
msg = gson.fromJson(json, HashMap.class);
 
if (debug) {
Map header = (Map) msg.get("header");
Map props = (Map) msg.get("properties");
System.out.println("*** header " + header);
System.out.println("*** properties " + props);
System.out.println("== receive-next-message = " + response.getStatus() + ", msg = " + msg);
 
print(response.getLinks());
}
} else {
System.out.println("ERROR receive-next-message = " + response.getStatus() + ", msg = " + json);
}
return msg;
}
void close() {
Response response = client.target(uriCloseCons).request().accept(MediaType.TEXT_PLAIN).delete();
 
System.out.println("Close consumer -> " + response.getStatus());
if (debug) print(response.getLinks());
}
 
private static void print(Set<Link> links) {
System.out.println(" link :");
for (Link link : links)
System.out.println("\t" + link.getRel() + " : " + link.getUri());
}
}
Property changes:
Added: svn:mime-type
+ text/plain
/trunk/joram/samples/src/joram/rest/Consumer.java
1,80 → 1,54
package rest;
 
import java.net.URI;
import java.util.Set;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
 
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation.Builder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Link;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
 
import org.glassfish.jersey.client.ClientConfig;
 
public class Consumer {
 
public static void main(String[] args) {
ClientConfig config = new ClientConfig();
Client client = ClientBuilder.newClient(config);
WebTarget target = client.target(getBaseURI());
System.out.println(target.getUri());
RestConsumer cons = new RestConsumer("http://localhost:8989/joram/", "queue");
// cons.debug = true;
testJSonMessage(cons);
// testStringMessage(cons);
cons.close();
}
static void testStringMessage(RestConsumer cons) {
for (int i=0; i<10; i++) {
String msg = cons.receiveStringMsg();
System.out.println(msg);
}
}
 
// lookup the destination
Builder builder = target.path("jndi").path("queue").request();
Response response = builder.accept(MediaType.TEXT_PLAIN).head();
System.out.println("== lookup \"queue\" = " + response.getStatus());
print(response.getLinks());
static void testJSonMessage(RestConsumer cons) {
for (int i=0; i<10; i++) {
HashMap<String, Object> msg = cons.receiveJSonMsg();
System.out.println("Receive message -> " + msg);
 
URI uriCreateCons = client.target(response.getLink("create-consumer"))
// .queryParam("name", "cons1")
.queryParam("idle-timeout", "120")
.getUri();
response = client.target(uriCreateCons)
.request()
.accept(MediaType.TEXT_PLAIN).post(null);
String type = (String) msg.get("type");
 
System.out.println("== create-consumer = " + response.getStatus());
print(response.getLinks());
Gson gson = new GsonBuilder().create();
if (RestConsumer.BytesMessage.equals(type)) {
byte[] body = null;
 
URI uriCloseCons = response.getLink("close-context").getUri();
URI uriReceiveNextMsg = response.getLink("receive-next-message").getUri();
body = gson.fromJson(msg.get("body").toString(), byte[].class);
} else if ("TextMessage".equals(type)) {
String body = null;
 
for (int i=0; i<10; i++) {
response = client.target(uriReceiveNextMsg)
.queryParam("timeout", "30000")
.request()
.accept(MediaType.TEXT_PLAIN)
.get();
 
String msg = response.readEntity(String.class);
if (response.getStatus() == Response.Status.OK.getStatusCode() && msg != null) {
System.out.println("== receive-next-message = " + response.getStatus() + ", msg = " + msg);
} else {
System.out.println("ERROR consume msg = " + msg + ", response = " + response);
body = (String) msg.get("body");
}
}
 
response = client.target(uriCloseCons)
.request()
.accept(MediaType.TEXT_PLAIN)
.delete();
Map header = (Map) msg.get("header");
Map props = (Map) msg.get("properties");
 
System.out.println("== close-consumer = " + response.getStatus());
print(response.getLinks());
}
private static URI getBaseURI() {
return UriBuilder.fromUri("http://localhost:8989/joram/").build();
}
int index = Integer.parseInt((String) ((ArrayList) props.get("index")).get(0));
long time = Long.parseLong((String) ((ArrayList) props.get("time")).get(0));
 
private static void print(Set<Link> links) {
System.out.println(" link :");
for (Link link : links)
System.out.println("\t" + link.getRel() + " : " + link.getUri());
System.out.println("Receive msg#" + index + " at " + time);
}
}
}