ZMQ PUSH/PULL模型:当节点PUSH消息并退出时消息丢失
我需要 PHP Web 应用程序将消息写入非阻塞队列,并且其他进程将它们出队。我当前的设计是 PHP 应用程序创建一个 ZMQ.PUSH
套接字,对目标地址进行 连接
并发送
消息。在目的地时,进程 (Java) 创建一个 ZMQ.PULL
套接字,在同一地址上执行 bind
并接收
消息。但是,当出队进程关闭(或未启动)时,PHP 应用程序在此期间发送的消息将丢失(当进程在同一地址上再次启动时,不会传递到出队进程)。这是 PHP 应用程序每次创建新的 ZMQ.Context
的问题吗?
作为示例,我创建了另一个 Java 进程来将消息写入队列。
public static void main(String[] args) {
ZMQ.Context context = ZMQ.context(1);
ZMQ.Socket socket = context.socket(ZMQ.PUSH);
socket.connect("tcp://localhost:5557");
for(int i = 0; i < 10; i++) {
socket.send(("Sending : " + message).getBytes(), 0);
}
}
如果在上述过程完成时出列过程没有开始,这会存在丢失消息的相同问题。
但是在上述方法体的末尾添加 while(true) {}
不会导致任何消息丢失 - 所有消息都会在出列程序启动时传递。那么,我关于被垃圾收集的 ZMQ.Context 对象导致这里问题的假设是否正确?如果是,那么如何在 PHP Web 应用程序中解决这个问题?因为我无法持久保存 ZMQ.Context
对象。
I have a requirement that a PHP web application write messages to a non-blocking queue and other process(es) dequeue them. My current design is PHP app create a ZMQ.PUSH
socket, do a connect
to the destination address and send
the message. While at the destination, a process (Java) create a ZMQ.PULL
socket, do a bind
on the same address and receive
the message. However, when the dequeuer process is down (or not started), the messages that the PHP app sent during that time are lost (not delivered to the dequeuer when the process starts again on the same address). Is this an issue with the PHP application creating a new ZMQ.Context
everytime?
As an example, I created another Java process to write messages to the queue.
public static void main(String[] args) {
ZMQ.Context context = ZMQ.context(1);
ZMQ.Socket socket = context.socket(ZMQ.PUSH);
socket.connect("tcp://localhost:5557");
for(int i = 0; i < 10; i++) {
socket.send(("Sending : " + message).getBytes(), 0);
}
}
This has the same problem of lost messages if the dequeuing process does not start by the time the above processes finishes.
But adding a while(true) {}
to the end of the above method body does not result in any lost messages - all the messages are delivered when the dequeuer starts. So am I correct in the assumption that the ZMQ.Context
object being garbage collected causes the problem here? If yes, then how to solve this problem in a PHP web application? Since I cannot persist the ZMQ.Context
object.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在寻找所谓的耐用 消息。
查看 zmq_setsockopt 特别是 ZMQ_IDENTITY 选项您的解决方案的第一步。
请务必阅读手册中有关 的部分高水位标记,以便在读者离线一段时间后不会落后太远。
祝你好运
You are looking for so-called durable messages.
Look at the zmq_setsockopt in particular the ZMQ_IDENTITY option as a first step in your solution.
Make sure you read the sections in the manual about High-Water Marks in order to not get too far behind if the reader is offline for a while.
Good Luck