question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Issue with ZContext in the mtrelay guide example

See original GitHub issue

I ported the mtrelay guide example over to use the ZContext class. When I run it, I get:

Step 1 ready, signaling step 2 Exception in thread “reaper-1” java.lang.NullPointerException at zmq.Ctx.destroy_socket(Ctx.java:328) at zmq.ZObject.destroy_socket(ZObject.java:144) at zmq.SocketBase.check_destroy(SocketBase.java:942) Test successful! at zmq.SocketBase.start_reaping(SocketBase.java:758) at zmq.Reaper.process_reap(Reaper.java:133) at zmq.ZObject.process_command(ZObject.java:114) at zmq.Reaper.in_event(Reaper.java:90) at zmq.Poller.run(Poller.java:231) at java.lang.Thread.run(Thread.java:722)

I’m using the ZContext.destroy() method to clean everything up.

testcase code:

package guide;

import org.zeromq.ZContext;
import org.zeromq.ZMQ;
import org.zeromq.ZMQ.Context;
import org.zeromq.ZMQ.Socket;

/**
 * Multithreaded relay
 */
public class mtrelay_bugtestcase {

    private static class Step1 extends Thread
    {
        private ZContext context;

        private Step1 (ZContext context)
        {
            this.context = context;
        }

        @Override
        public void run(){
            //  Signal downstream to step 2
            Socket xmitter = context.createSocket(ZMQ.PAIR);
            xmitter.connect("inproc://step2");
            System.out.println ("Step 1 ready, signaling step 2");
            xmitter.send("READY", 0);
            xmitter.close ();
        }

    }
    private static class Step2 extends Thread
    {
        private ZContext context;

        private Step2 (ZContext context)
        {
            this.context = context;
        }

        @Override
        public void run(){
            //  Bind to inproc: endpoint, then start upstream thread
            Socket receiver = context.createSocket(ZMQ.PAIR);
            receiver.bind("inproc://step2");
            Thread step1 = new Step1 (context);
            step1.start();

            //  Wait for signal
            receiver.recv(0);
            receiver.close ();

            //  Connect to step3 and tell it we're ready
            Socket xmitter = context.createSocket(ZMQ.PAIR);
            xmitter.connect("inproc://step3");
            xmitter.send("READY", 0);

            xmitter.close ();
        }

    }
    public static void main (String[] args) {

        ZContext context = new ZContext();

        //  Bind to inproc: endpoint, then start upstream thread
        Socket receiver = context.createSocket(ZMQ.PAIR);
        receiver.bind("inproc://step3");

        //  Step 2 relays the signal to step 3
        Thread step2 = new Step2 (context);
        step2.start();

        //  Wait for signal
        receiver.recv(0);
        receiver.close ();

        System.out.println ("Test successful!");
        context.destroy();
    }
}

Issue Analytics

  • State:closed
  • Created 11 years ago
  • Comments:11 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
jkwatsoncommented, Feb 4, 2013

At the moment, however, the “higher level API” (ZContext) doesn’t stop you from shooting yourself in the foot and mixing the two paradigms. I’m suggesting that we make it so you can’t make the mistake of mixing the two paradigms, rather than just assume the user will know better.

I think we’re in agreement. In the new API, are you suggesting we should remove the “close()” method from the Socket, rather than making it call back on the context to destroy itself? I’m happy either way, really.

1reaction
miniwaycommented, Feb 4, 2013

Sorry for the confusing. Actually you must not.

Undocumented usage is

If you create Socket from low level ZMQ.Context, you must close it by yourself. If you create Socket from high level ZContext, you could close it by destroySocket otherwise ZContext.destroy will close it for you.

You can’t mix it.

ZMQ.Context is low level libzmq ctz_t equivalent which is created from zmq_ctx_new. And ZContext is high level czmq zctx_t equivalent which is created from zctx_new.

ZMQ.Socket is mix of libzmq.SocketBase and czmq.zsocket_t. I think this is a design dept, but it was the legacy from initial design of jzmq. It should have created ZSocket, but it would not easy without breaking existing applications.

The zguide has an epic that describes 0MQ using low level API at chpater1 then introduces high-level czmq at chpater2. Java examples also follows the epic. So they use ZMQ.context at chapter1 examples and ZContext from chapter2.

I guess the new API is a re-designed high-level API. The examples in chapter1 might not be good candidates for a high level API. If they are written in a high level API and read within the zguide epic, stories would not fit well.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Chapter 2 - Sockets and Patterns - ZeroMQ Guide
Adding a pub-sub proxy solves the dynamic discovery problem in our example. We set the proxy in the “middle” of the network. The...
Read more >
ØMQ - The Guide
The Guide examples live in the Guide's git repository. The simplest way to get all the examples is to clone this repository:.
Read more >
Xmitter Login - Teletalk
Issue with ZContext in the mtrelay guide example · Issue #40 ... Feb 3, 2013 ... PAIR); xmitter.connect("inproc://step2"); System.out.println ("Step 1 .
Read more >
2. Sockets and Patterns
Handling Multiple Sockets # ... Create; // Connect to task ventilator receiver := Context.Socket( stPull ); receiver.RaiseEAgain := false; receiver.connect( 'tcp ...
Read more >
感谢朋友帮助你的文档- CSDN
If you are using older versions of ZeroMQ then some of the examples and ... package guide; // // Hello World server in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found