ZeroMQ之REP/REQ模式
package reqrep;import org.zeromq.ZMQ;public class REP { /** * @param args * @throws InterruptedException */ public static void main(String[] args) throws InterruptedException {// TODO Auto-generated method stubZMQ.Context context = ZMQ.context(1);ZMQ.Socket s = context.socket(ZMQ.REP);s.bind("tcp://*:5557");int i = 0;while (true) { i++; String msg = new String(s.recv(0)); s.send((msg + "_rep"+i).getBytes(), 0);} }}
package reqrep;import org.zeromq.ZMQ;public class REQ { /** * @param args * @throws InterruptedException */ public static void main(String[] args) throws InterruptedException {// TODO Auto-generated method stubZMQ.Context context = ZMQ.context(1);ZMQ.Socket s = context.socket(ZMQ.REQ);s.connect("tcp://*:5557");int i = 0;while (true) { Thread.currentThread().sleep(2000); s.send(("msg" + "_req").getBytes(), 0); String rev = new String(s.recv(0)); System.out.println(rev);} }}
msg_req_rep1msg_req_rep2msg_req_rep3msg_req_rep4msg_req_rep5msg_req_rep6msg_req_rep7msg_req_rep8...
?
显然这种应答模式的效率很低!