Skip to content

Commit

Permalink
chapter5 first part of java clone examples
Browse files Browse the repository at this point in the history
  • Loading branch information
miniway committed Apr 9, 2013
1 parent 30f32fa commit 6d71183
Show file tree
Hide file tree
Showing 9 changed files with 397 additions and 256 deletions.
3 changes: 3 additions & 0 deletions examples/Java/build
Expand Up @@ -28,6 +28,9 @@ if [ /$1/ = /all/ ]; then
echo "$MAIN"
javac -cp $CLASSPATH $MAIN
done
elif [ /$1/ = /jzmq/ -a -f $2.java ]; then
echo "Compiling JZMQ $2.java"
javac -cp $JZMQ_CLASSPATH $2.java
elif [ /$1/ = /jzmq/ ]; then
rm -f *.class
echo "Building JZMQ Java examples..."
Expand Down
26 changes: 11 additions & 15 deletions examples/Java/clonecli1.java
Expand Up @@ -2,9 +2,10 @@
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

import org.zeromq.ZContext;
import org.zeromq.ZMQ;
import org.zeromq.ZMQ.Context;
import org.zeromq.ZMQ.Socket;

/**
* Clone client model 1
* @author Danish Shrestha <dshrestha06@gmail.com>
Expand All @@ -15,26 +16,21 @@ public class clonecli1 {
private static AtomicLong sequence = new AtomicLong();

public void run() {
Context ctx = ZMQ.context(1);
Socket subscriber = ctx.socket(ZMQ.SUB);
subscriber.setLinger(0);
ZContext ctx = new ZContext();
Socket subscriber = ctx.createSocket(ZMQ.SUB);
subscriber.connect("tcp://localhost:5556");
subscriber.subscribe("".getBytes());

while (true) {
kvsimple kvMsg = null;
try {
kvMsg = kvsimple.recv(subscriber);
} catch (Throwable t) {
// cant let it die
}
kvsimple kvMsg = kvsimple.recv(subscriber);
if (kvMsg == null)
break;

if (kvMsg != null) {
clonecli1.kvMap.put(kvMsg.getKey(), kvMsg);
System.out.println("receiving " + kvMsg);
sequence.incrementAndGet();
}
clonecli1.kvMap.put(kvMsg.getKey(), kvMsg);
System.out.println("receiving " + kvMsg);
sequence.incrementAndGet();
}
ctx.destroy();
}

public static void main(String[] args) {
Expand Down
33 changes: 16 additions & 17 deletions examples/Java/clonecli2.java
Expand Up @@ -14,25 +14,24 @@
*/
public class clonecli2 {
private static Map<String, kvsimple> kvMap = new HashMap<String, kvsimple>();
private static AtomicLong sequence = new AtomicLong();

public void run() {
Context ctx = ZMQ.context(1);
Socket snapshot = ctx.socket(ZMQ.XREQ);
snapshot.setLinger(0);
Socket snapshot = ctx.socket(ZMQ.DEALER);
snapshot.connect("tcp://localhost:5556");

Socket subscriber = ctx.socket(ZMQ.SUB);
subscriber.setLinger(0);
subscriber.connect("tcp://localhost:5557");
subscriber.subscribe("".getBytes());

// get state snapshot
snapshot.send("ICANHAZ?".getBytes(), 0);
kvsimple kvMsg = null;
long sequence = 0;
while (true) {
kvMsg = kvsimple.recv(snapshot);
sequence.set(kvMsg.getSequence());
kvsimple kvMsg = kvsimple.recv(snapshot);
if (kvMsg == null)
break;
sequence = kvMsg.getSequence();
if ("KTHXBAI".equalsIgnoreCase(kvMsg.getKey())) {
System.out.println("Received snapshot = " + kvMsg.getSequence());
break; // done
Expand All @@ -44,16 +43,16 @@ public void run() {

// now apply pending updates, discard out-of-sequence messages
while (true) {
kvMsg = null;
kvMsg = kvsimple.recv(subscriber);

if (kvMsg != null) {
if (kvMsg.getSequence() > sequence.get()) {
sequence.set(kvMsg.getSequence());
System.out.println("receiving " + sequence);
clonecli2.kvMap.put(kvMsg.getKey(), kvMsg);
}
}
kvsimple kvMsg = kvsimple.recv(subscriber);

if (kvMsg == null)
break;

if (kvMsg.getSequence() > sequence) {
sequence = kvMsg.getSequence();
System.out.println("receiving " + sequence);
clonecli2.kvMap.put(kvMsg.getKey(), kvMsg);
}
}
}

Expand Down
51 changes: 26 additions & 25 deletions examples/Java/clonecli3.java
Expand Up @@ -4,8 +4,8 @@
import java.util.Random;
import java.util.concurrent.atomic.AtomicLong;

import org.zeromq.ZContext;
import org.zeromq.ZMQ;
import org.zeromq.ZMQ.Context;
import org.zeromq.ZMQ.Poller;
import org.zeromq.ZMQ.Socket;
/**
Expand All @@ -15,29 +15,28 @@
*/
public class clonecli3 {
private static Map<String, kvsimple> kvMap = new HashMap<String, kvsimple>();
private static AtomicLong sequence = new AtomicLong();

public void run() {
Context ctx = ZMQ.context(1);
Socket snapshot = ctx.socket(ZMQ.XREQ);
snapshot.setLinger(0);
ZContext ctx = new ZContext();
Socket snapshot = ctx.createSocket(ZMQ.DEALER);
snapshot.connect("tcp://localhost:5556");

Socket subscriber = ctx.socket(ZMQ.SUB);
subscriber.setLinger(0);
Socket subscriber = ctx.createSocket(ZMQ.SUB);
subscriber.connect("tcp://localhost:5557");
subscriber.subscribe("".getBytes());

Socket push = ctx.socket(ZMQ.PUSH);
push.setLinger(0);
Socket push = ctx.createSocket(ZMQ.PUSH);
push.connect("tcp://localhost:5558");

// get state snapshot
snapshot.send("ICANHAZ?".getBytes(), 0);
kvsimple kvMsg = null;
long sequence = 0;
snapshot.send("ICANHAZ?".getBytes(), 0);
while (true) {
kvMsg = kvsimple.recv(snapshot);
sequence.set(kvMsg.getSequence());
kvsimple kvMsg = kvsimple.recv(snapshot);
if (kvMsg == null)
break; // Interrupted

sequence = kvMsg.getSequence();
if ("KTHXBAI".equalsIgnoreCase(kvMsg.getKey())) {
System.out.println("Received snapshot = " + kvMsg.getSequence());
break; // done
Expand All @@ -47,29 +46,30 @@ public void run() {
clonecli3.kvMap.put(kvMsg.getKey(), kvMsg);
}

Poller poller = ctx.poller();
Poller poller = new ZMQ.Poller(1);
poller.register(subscriber);

Random random = new Random();

// now apply pending updates, discard out-of-sequence messages
long alarm = System.currentTimeMillis() + 5000;
while (true) {
poller.poll(Math.max(0, alarm - System.currentTimeMillis()));
int rc = poller.poll(Math.max(0, alarm - System.currentTimeMillis()));
if (rc == -1)
break; // Context has been shut down

if (poller.pollin(0)) {
kvMsg = kvsimple.recv(subscriber);
if (kvMsg != null) {
if (kvMsg.getSequence() > sequence.get()) {
sequence.set(kvMsg.getSequence());
System.out.println("receiving " + sequence);
clonecli3.kvMap.put(kvMsg.getKey(), kvMsg);
}
}
kvsimple kvMsg = kvsimple.recv(subscriber);
if (kvMsg == null)
break; // Interrupted
if (kvMsg.getSequence() > sequence) {
sequence = kvMsg.getSequence();
System.out.println("receiving " + sequence);
clonecli3.kvMap.put(kvMsg.getKey(), kvMsg);
}
}

if (System.currentTimeMillis() >= alarm) {
System.out.println("Alarm time");
int key = random.nextInt(10000);
int body = random.nextInt(1000000);

Expand All @@ -78,9 +78,10 @@ public void run() {

kvsimple kvUpdateMsg = new kvsimple(key + "", 0, b.array());
kvUpdateMsg.send(push);
alarm = System.currentTimeMillis() + 5000;
alarm = System.currentTimeMillis() + 1000;
}
}
ctx.destroy();
}

public static void main(String[] args) {
Expand Down
97 changes: 97 additions & 0 deletions examples/Java/clonecli4.java
@@ -0,0 +1,97 @@
import org.zeromq.ZContext;
import org.zeromq.ZMQ;
import org.zeromq.ZMQ.Poller;
import org.zeromq.ZMQ.Socket;

import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.atomic.AtomicLong;

/**
* Clone client Model Four
*
*/
public class clonecli4
{
// This client is identical to clonecli3 except for where we
// handles subtrees.
private final static String SUBTREE = "/client/";

private static Map<String, kvsimple> kvMap = new HashMap<String, kvsimple>();

public void run() {
ZContext ctx = new ZContext();
Socket snapshot = ctx.createSocket(ZMQ.DEALER);
snapshot.connect("tcp://localhost:5556");

Socket subscriber = ctx.createSocket(ZMQ.SUB);
subscriber.connect("tcp://localhost:5557");
subscriber.subscribe(SUBTREE.getBytes());

Socket push = ctx.createSocket(ZMQ.PUSH);
push.connect("tcp://localhost:5558");

// get state snapshot
snapshot.sendMore("ICANHAZ?");
snapshot.send(SUBTREE);
long sequence = 0;

while (true) {
kvsimple kvMsg = kvsimple.recv(snapshot);
if (kvMsg == null)
break; // Interrupted

sequence = kvMsg.getSequence();
if ("KTHXBAI".equalsIgnoreCase(kvMsg.getKey())) {
System.out.println("Received snapshot = " + kvMsg.getSequence());
break; // done
}

System.out.println("receiving " + kvMsg.getSequence());
clonecli4.kvMap.put(kvMsg.getKey(), kvMsg);
}

Poller poller = new Poller(1);
poller.register(subscriber);

Random random = new Random();

// now apply pending updates, discard out-of-sequence messages
long alarm = System.currentTimeMillis() + 5000;
while (true) {
int rc = poller.poll(Math.max(0, alarm - System.currentTimeMillis()));
if (rc == -1)
break; // Context has been shut down

if (poller.pollin(0)) {
kvsimple kvMsg = kvsimple.recv(subscriber);
if (kvMsg == null)
break; // Interrupted
if (kvMsg.getSequence() > sequence) {
sequence = kvMsg.getSequence();
System.out.println("receiving " + sequence);
clonecli4.kvMap.put(kvMsg.getKey(), kvMsg);
}
}

if (System.currentTimeMillis() >= alarm) {
String key = String.format("%s%d", SUBTREE, random.nextInt(10000));
int body = random.nextInt(1000000);

ByteBuffer b = ByteBuffer.allocate(4);
b.asIntBuffer().put(body);

kvsimple kvUpdateMsg = new kvsimple(key, 0, b.array());
kvUpdateMsg.send(push);
alarm = System.currentTimeMillis() + 1000;
}
}
ctx.destroy();
}

public static void main(String[] args) {
new clonecli4().run();
}
}

0 comments on commit 6d71183

Please sign in to comment.