zeromq / jeromq

Pure Java ZeroMQ
Mozilla Public License 2.0
2.34k stars 484 forks source link

How to split `ZMsg` in reply envelope and request? #955

Closed siiky closed 1 year ago

siiky commented 1 year ago

Hello,

Is there an easy way to split a full ZMsg message (envelope + request) into the envelope and the request? Right now I'm using a loop popping from the message until reaching the empty frame, but I was hoping there's an easier way.

Context: I'm working on a project using ZeroMQ where one of the programs, written in Java with JeroMQ, has a ROUTER frontend and a few REP backend threads. The ROUTER, upon receiving a message, decides depending on its contents which backend REP to redirect to. To redirect a message the ROUTER thread connects with a REQ to each of the backend threads.

However, since the ROUTER returns the full message (envelope + request, i.e. [IDs, "", X, Y, Z]), the REQ will send ["", IDs, "", X, Y, Z], and the REP will receive [IDs, "", X, Y, Z]. I figure the easiest way to deal with this is for the REP threads to split the message in two.

siiky commented 1 year ago

I just realized I should be using a DEALER in the router thread instead of a REQ for each of the backend workers! The DEALER doesn't prepend an empty reply envelope like the REQ does... Feels so obvious now.