Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send byte array to socket using ZeroMQ?

I am working with ZeroMQ for the first time and I wrote a simple utility to send the data to sockets. Below is my simple class by which I am sending data to sockets using ZeroMQ. As of now I am playing with ZeroMQ so wrote a simple class as shown below and after I get more familiar then I need to put the same code in multithreading code.

public static void main(String[] args) {

    ZMQ.Context context = ZMQ.context(1);

    // as of now sending only 10 data
    for (int i = 1; i <= 10; i++) {
        ZMQ.Socket socket = context.socket(ZMQ.PUSH);
        socket.connect("tcp://localhost:10000");

        byte[] packByteArray = generateRandomStringAsBytes();

        ZMsg req = new ZMsg();
        req.add(packByteArray);
        req.send(socket);

        socket.close();
    }
    context.term();
}

Since this is my first time so I don't know whether this is the right and efficient way of using ZeroMQ to send the data as byte array? I have heard of various thing like ZFrame and ZMsg, not sure when do we need to use both and what is the best way of sending byte array to a socket.

In our case, we need to send random string as the bytes to the socket so not sure what is the best and efficient way of using ZeroMQ?

like image 547
vader Avatar asked Oct 18 '25 15:10

vader


1 Answers

No one will ever enjoy the passion of a climb to the Mount Everest from just reading a list of GPS-waypoints, be the list short or long ...

Thus rather do not read code-examples without prior understanding the underlying concept.


The Right way

ZeroMQ was designed with a certain set of maxims - Zero-Blocking, Zero-Sharing, Zero-Copy, Zero-Broker(s), Minimum-latency, messaging framework.

One shall rather consider it a Framework to rely on for a setup of a fast, semi-persistent Formal-Scaleable-Communication-Pattern Layer ( not a suite of consumable disposables )

While no one may restrict your use of a mix of Msgs and Frames and for-loop-ed setup/discard of the messaging sockets, this is not the way, the ZeroMQ strongest powers could be achieved.

A good understanding of the behaviour ( not the code ) and the reasons for each particular style of extending the basic ZeroMQ-primitives into a robust message passing layer of lower level Finite-State-Automata, is worth spending a week or two.

The best step one may do in a situation you describe is to forget about coding and take a principal view of the system so as to get the full-scale picture of the ZeroMQ architecture.

an extended approach with SIG_KILL add-on

Missing the key points will result just in misconcepted design approaches and a much bigger waste of time.

  • design of a distributed messaging layer behaviours / abstractions to be implemented
  • resources setup in each process/thread/node and arranging the correct order of .bind()/.connect() methods
  • non-blocking mode of operations where possible
  • performance tuning ( watermarks settings, timeouts, sub-sampling / super-sampling )
  • proper resources release
  • exception handling as an integral habit of the design
  • enforced gracefull termination of the Context in each process/thread/node to avoid leaks

The Efficient way

This may have different meanings - a TimeDOMAIN efficiency ( i.e. fast ), a MemoryFOOTPRINT efficiency ( i.e. not wasting MEM & avoiding Garbage Collection(s) ), I/O resources efficiency or coding/re-use efficiency.

ZeroMQ has done a big & great work on internal efficiency and users can benefit from what have Masters of the Masters put inside the library.

The very same way users of the library may spoil all the benefits by an inefficient way ( not speaking about a principally wrong way ) how to use the ZeroMQ tools for the respective tasks.

The best next step

IMHO if you are serious about professional messaging, get the great book and source both the elementary setups knowledge, a bit more complex multi-socket messaging layer designs with soft signalling & also the further thoughts about the great powers of concurrent, heterogenous, distributed processing to advance your learning curve.

Pieter Hintjens' book "Code Connected, Volume 1" ( available in PDF ) is more than a recommended source for your issue.

There you will get grounds for your further use of ZeroMQ.

ZeroMQ is a great tool, not just for the messaging layer itself. Worth time & efforts. multi-socket messaging layer with soft signalling

like image 80
user3666197 Avatar answered Oct 21 '25 04:10

user3666197



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!