use of org.zeromq.ZMsg in project jeromq by zeromq.
the class clone method set.
// .split set method
// Set a new value in the shared hashmap. Sends a [SET][key][value][ttl]
// command through to the agent which does the actual work:
public void set(String key, String value, int ttl) {
ZMsg msg = new ZMsg();
msg.add("SET");
msg.add(key);
msg.add(value);
msg.add(String.format("%d", ttl));
msg.send(pipe);
}
use of org.zeromq.ZMsg in project jeromq by zeromq.
the class ticlient method serviceCall.
static ZMsg serviceCall(mdcliapi session, String service, ZMsg request) {
ZMsg reply = session.send(service, request);
if (reply != null) {
ZFrame status = reply.pop();
if (status.streq("200")) {
status.destroy();
return reply;
} else if (status.streq("400")) {
System.out.println("E: client fatal error, aborting");
} else if (status.streq("500")) {
System.out.println("E: server fatal error, aborting");
}
reply.destroy();
}
// Didn't succeed; don't care why not
return null;
}
use of org.zeromq.ZMsg in project jeromq by zeromq.
the class ticlient method main.
public static void main(String[] args) throws Exception {
boolean verbose = (args.length > 0 && args[0].equals("-v"));
mdcliapi session = new mdcliapi("tcp://localhost:5555", verbose);
// 1. Send 'echo' request to Titanic
ZMsg request = new ZMsg();
request.add("echo");
request.add("Hello world");
ZMsg reply = serviceCall(session, "titanic.request", request);
ZFrame uuid = null;
if (reply != null) {
uuid = reply.pop();
reply.destroy();
uuid.print("I: request UUID ");
}
// 2. Wait until we get a reply
while (!Thread.currentThread().isInterrupted()) {
Thread.sleep(100);
request = new ZMsg();
request.add(uuid.duplicate());
reply = serviceCall(session, "titanic.reply", request);
if (reply != null) {
String replyString = reply.getLast().toString();
System.out.printf("Reply: %s\n", replyString);
reply.destroy();
// 3. Close request
request = new ZMsg();
request.add(uuid.duplicate());
reply = serviceCall(session, "titanic.close", request);
reply.destroy();
break;
} else {
System.out.println("I: no reply yet, trying again...");
// Try again in 5 seconds
Thread.sleep(5000);
}
}
uuid.destroy();
session.destroy();
}
use of org.zeromq.ZMsg in project jeromq by zeromq.
the class titanic method serviceSuccess.
// .split try to call a service
// Here, we first check if the requested MDP service is defined or not,
// using a MMI lookup to the Majordomo broker. If the service exists,
// we send a request and wait for a reply using the conventional MDP
// client API. This is not meant to be fast, just very simple:
static boolean serviceSuccess(String uuid) {
// Load request message, service will be first frame
String filename = requestFilename(uuid);
// If the client already closed request, treat as successful
if (!new File(filename).exists())
return true;
DataInputStream file = null;
ZMsg request;
try {
file = new DataInputStream(new FileInputStream(filename));
request = ZMsg.load(file);
} catch (IOException e) {
e.printStackTrace();
return true;
} finally {
try {
if (file != null)
file.close();
} catch (IOException e) {
}
}
ZFrame service = request.pop();
String serviceName = service.toString();
// Create MDP client session with short timeout
mdcliapi client = new mdcliapi("tcp://localhost:5555", false);
// 1 sec
client.setTimeout(1000);
// only 1 retry
client.setRetries(1);
// Use MMI protocol to check if service is available
ZMsg mmiRequest = new ZMsg();
mmiRequest.add(service);
ZMsg mmiReply = client.send("mmi.service", mmiRequest);
boolean serviceOK = (mmiReply != null && mmiReply.getFirst().toString().equals("200"));
mmiReply.destroy();
boolean result = false;
if (serviceOK) {
ZMsg reply = client.send(serviceName, request);
if (reply != null) {
filename = replyFilename(uuid);
DataOutputStream ofile = null;
try {
ofile = new DataOutputStream(new FileOutputStream(filename));
ZMsg.save(reply, ofile);
} catch (IOException e) {
e.printStackTrace();
return true;
} finally {
try {
if (file != null)
file.close();
} catch (IOException e) {
}
}
result = true;
}
reply.destroy();
;
} else
request.destroy();
client.destroy();
return result;
}
use of org.zeromq.ZMsg in project jeromq by zeromq.
the class titanic method main.
// .split worker task
// This is the main thread for the Titanic worker. It starts three child
// threads; for the request, reply, and close services. It then dispatches
// requests to workers using a simple brute force disk queue. It receives
// request UUIDs from the {{titanic.request}} service, saves these to a disk
// file, and then throws each request at MDP workers until it gets a
// response.
public static void main(String[] args) {
boolean verbose = (args.length > 0 && "-v".equals(args[0]));
ZContext ctx = new ZContext();
Socket requestPipe = ZThread.fork(ctx, new TitanicRequest());
ZThread.start(new TitanicReply());
ZThread.start(new TitanicClose());
Poller poller = ctx.createPoller(1);
poller.register(requestPipe, ZMQ.Poller.POLLIN);
// Main dispatcher loop
while (true) {
// We'll dispatch once per second, if there's no activity
int rc = poller.poll(1000);
if (rc == -1)
// Interrupted
break;
if (poller.pollin(0)) {
// Ensure message directory exists
new File(TITANIC_DIR).mkdirs();
// Append UUID to queue, prefixed with '-' for pending
ZMsg msg = ZMsg.recvMsg(requestPipe);
if (msg == null)
// Interrupted
break;
String uuid = msg.popString();
BufferedWriter wfile = null;
try {
wfile = new BufferedWriter(new FileWriter(TITANIC_DIR + "/queue", true));
wfile.write("-" + uuid + "\n");
} catch (IOException e) {
e.printStackTrace();
break;
} finally {
try {
if (wfile != null)
wfile.close();
} catch (IOException e) {
}
}
msg.destroy();
}
// Brute force dispatcher
//"?........:....:....:....:............:";
byte[] entry = new byte[37];
RandomAccessFile file = null;
try {
file = new RandomAccessFile(TITANIC_DIR + "/queue", "rw");
while (file.read(entry) > 0) {
// UUID is prefixed with '-' if still waiting
if (entry[0] == '-') {
if (verbose)
System.out.printf("I: processing request %s\n", new String(entry, 1, entry.length - 1, ZMQ.CHARSET));
if (serviceSuccess(new String(entry, 1, entry.length - 1, ZMQ.CHARSET))) {
// Mark queue entry as processed
file.seek(file.getFilePointer() - 37);
file.writeBytes("+");
file.seek(file.getFilePointer() + 36);
}
}
// Skip end of line, LF or CRLF
if (file.readByte() == '\r')
file.readByte();
if (Thread.currentThread().isInterrupted())
break;
}
} catch (FileNotFoundException e) {
} catch (IOException e) {
e.printStackTrace();
} finally {
if (file != null) {
try {
file.close();
} catch (IOException e) {
}
}
}
}
}
Aggregations