5

jeromq is a Java implementation of libzmq. I have a .jar file created from the jeromq source. However, I'm unable to call a class in jeromq from MATLAB. I've used addjavaclasspath and addjavalibrarypath but am still not able to get it working. Does anyone have a simple working example in MATLAB?

kdheepak
  • 1,274
  • 11
  • 22
  • 1
    Can you show us the code that you actually used? – Suever Jun 27 '16 at 17:59
  • Hi @Suever. I was able to run the following code `import org.zeromq.ZMQ;` in MATLAB, and not get any errors. I placed the `jeromq-0.3.5.jar` file that I downloaded in the directory I was working in, and the import statement works. Can you point me to an example of creating a simple client in MATLAB? – kdheepak Jun 28 '16 at 15:06
  • You imported it, did you try to create an instance? – Suever Jun 28 '16 at 15:10
  • Hi @Suever, I was able to get it to work. I've attached it below if anyone else is interested in doing something similar. https://gist.github.com/kdheepak/dca9d20f6bea93de8756068f79dc60a2 . If you have comments or suggestion to improve this, let me know. Edit - I'm unable to add code formatting here. I've pasted it in a gist. – kdheepak Jun 29 '16 at 14:30

3 Answers3

7

I've added the answer here as for reference in case anyone else is interested.

% Author : Dheepak Krishnamurthy
% License : BSD 3 Clause

import org.zeromq.ZMQ;

ctx = zmq.Ctx();

socket = ctx.createSocket(ZMQ.REP);

socket.bind('tcp://127.0.0.1:7575');
message = socket.recv(0);
json_data = native2unicode(message.data)';

message = zmq.Msg(8);
message.put(unicode2native('Received'));
socket.send(message, 0);

socket.close()
kdheepak
  • 1,274
  • 11
  • 22
3

My Matlab 9.0.0.341360 (R2016a) wanted the following code instead of import above:

javaclasspath('/path/to/jar/jeromq-0.4.3-SNAPSHOT.jar')
import org.zeromq.*

The rest worked fine.

Ivan
  • 599
  • 4
  • 18
3

Here is what I had to do to get things working.


    javaclasspath('jeromq-0.5.1.jar')
    import org.zeromq.*;

    %subscribe to ZMQ feed
    context = ZContext();
    socket = context.createSocket(ZMQ.SUB); 
    success = false;
    while(~success)
        success = socket.connect('tcp://127.0.0.1:5996');
    end
    socket.subscribe("");
    socket.setTCPKeepAlive(1);

    %receive a message
    message = socket.recv(0); %nonblocking receive uses argument (1)

    %when done
    socket.close();

  • Not sure why, but I can't seem to access the socket.subscribe() method from jeromq. Not sure if there is a version mismatch or if I have a compile issue. Java: jdk1.8.0_321, MATLAB 2021B, jeromq-0.5.3-SNAPSHOT.jar commit 2a3645dbe6161774faa5011c132907c1be3c7687 – Brian Mar 22 '22 at 21:56