How do I send a byte array to a socket using ZeroMQ?

This is my first time working with ZeroMQ and I wrote a simple utility to send data to sockets. Below is my simple class that I use to send data to sockets using ZeroMQ. At the moment I am playing with ZeroMQ so I wrote a simple class like below and after I get more familiar I need to put the same code in multi-threaded 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 if this is the correct and efficient way to use ZeroMQ to send data as a byte array? I've heard about various things like ZFrame and ZMsg, not sure when we need to use both and the best way to send a byte array to a socket.

In our case, we need to send a random string as bytes to the socket, not to be sure what is the best and most efficient way to use ZeroMQ?

+3


source to share


1 answer


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 ...

So do n't read .


The right way

ZeroMQ was designed with a specific set of maxims - Zero- Lock , Zero- Sharing , Zero- Copy , Zero-Broker (s), minimal latency , messaging frameworks .

It should be considered as the basis for the installation of a fast, semi-permanent F ormal- S caleable- C ommunication- P attern Layer (not a set of consumables)

While no one can restrict the use of combinations of Msg

and Frame

and -specific settings / drop messaging sockets, this is not the case, the strongest powers of ZeroMQ can be achieved. for

Good understanding of behavior ( not code) and the reasons for each particular style of extending the underlying ZeroMQ primitives into a robust Finite-State-Automata low-level messaging layer is worth spending a week or two.

The best step you can take in the situation you are describing is to forget about coding and take a look at the main system view to get a complete picture of the ZeroMQ architecture.

an extended approach with SIG_KILL add-on

The lack of key points will only lead to "strong" design flaws and a much more "strong" waste of time.



  • building distributed messaging layer behavior / abstractions to be implemented
  • setting up resources in each process / thread / node and ordering order .bind()

    / .connect()

    methods
  • non-blocking mode of operations where possible
  • performance tuning (watermark settings, timeouts, sub-sampling / super-sampling)
  • correct resource release
  • exception handling as an integral design habit
  • force fine completion of the context in every process / thread / node to avoid leaks

Effective method

This can have different meanings - TimeDOMAIN efficiency (i.e. fast), MemoryFOOTPRINT efficiency (i.e. not wasting MEM and avoiding garbage collection), I / O resource efficiency, or encoding / reuse efficiency.

ZeroMQ has done a lot and a lot of internal efficiency work and users can benefit from the wizards placed in the library.

Likewise, library users can mess up all the benefits in an inefficient way (not to mention a fundamentally wrong way) of how to use the ZeroMQ tools for their respective tasks.

Best next step

IMHO if you are serious about professional messaging get an excellent book and source of both rudimentary setups and more complex layered soft signaling messaging schemes as well as further thoughts on the great powers of parallel heterogeneous, distributed processing to advance your learning curve ...

Pieter Hintjens' book "Included Code Volume 1 " (available in PDF format) is more than a recommended source for your problem.

There you will receive the reasons for further use of ZeroMQ.

ZeroMQ is a great tool, not just for the messaging layer itself. Well worth the time and effort. multi-socket messaging layer with soft signaling

+4


source







All Articles