我有以下“hello world” jeromq PUSH-PULL 客户端和服务器。只有在我设置了高水位标记值之后,我才能在不丢失消息的情况下进行传输。
import org.jeromq.ZMQ;
public class TestTcpServer {
public static void main(String[] args) {
ZMQ.Context context = ZMQ.context(1);
ZMQ.Socket socket = context.socket(ZMQ.PULL);
System.out.println("Binding TCP server on port 5555");
//socket.setRcvHWM(100_000);
socket.bind("tcp://*:5555");
int x;
x = 0;
while (true) {
x++;
byte[] raw = socket.recv(0);
String rawMessage = new String(raw);
if (x > 99_997) {
System.out.println(x);
System.out.println(rawMessage);
}
}
}
}
//client
import java.io.IOException;
import org.jeromq.ZMQ;
public class TestTcpClient {
/**
* @param args
* @throws InterruptedException
* @throws IOException
*/
public static void main(String[] args) throws InterruptedException,
IOException {
ZMQ.Context context = ZMQ.context(1);
ZMQ.Socket socket = context.socket(ZMQ.PUSH);
socket.connect("tcp://localhost:5555");
//socket.setRcvHWM(100_000);
System.out.println("Sending 100 000 transactions over TCP..."); long start = System.currentTimeMillis();
for (int request_nbr = 0; request_nbr != 100_000; request_nbr++) {
String requestString = "message";
byte[] request = requestString.getBytes();
boolean success = socket.send(request, 0);
if (!success) {
System.out.println("sending message failed!");
}
}
long end = System.currentTimeMillis();
System.out.print("Time: ");
System.out.print(end - start);
System.out.println(" ms");
socket.close();
context.term();
}
}
根据 0Mq文档,当达到高水位线时,只有 PUB 套接字会丢弃消息。由于我使用的是 PUSH-PULL 套接字,为什么会丢弃消息?
在我看来,HWM 是系统的动态属性,所以虽然我已经能够解决在这个 hello world 示例中丢弃消息的问题,但我想知道我是否可以指望 jeromq 在现实世界的情况下不会丢弃消息?