JZMQ错误:没有线程可用

时间:2014-03-31 19:30:41

标签: java zeromq jzmq

我正在尝试编写一个可以在服务器上运行并执行一些简单操作的简单Java应用程序。但目前我的应用程序失败了一个奇怪的错误我似乎无法找到Exception in thread "main" org.zeromq.ZMQException: No thread available(0x9523dfe)上的任何信息

两个服务都在同一台服务器上运行,但只有我的应用程序失败。我发现只有C zmq框架的最小文档。没什么用于java。

发生错误的类:

public class TestService extends GenericService {

public TestService(String[] args) {
    init(args);
}

private void init(String[] args) {
    int i = 0;
    serviceId = args[i++];
    servicePort = Integer.valueOf(args[i++]);
}

private void retrieveConfig() {

}

public void run() {
    System.out.println("Running");
    initZmq(); // <---- Here
    retrieveConfig();
    for (int i=0; i<120; i++) {
        System.out.println("Counter = " + i);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        }
    }
    closeZmq();
    System.out.println("Done!");
}

@Override
protected void requestInit() {
    zmqClient.send(getInitMessage().getBytes(),0);
    byte[] reply = zmqClient.recv(0);
    System.out.println("Config - " + (new String(reply)));
}

}

抽象通用服务类。

public abstract class GenericService {

protected Context zmqContext;
protected Socket zmqClient;
protected int port;
protected int servicePort;
protected String serviceId;

protected static final String addressNoPort = "tcp://127.0.0.1:";

public GenericService() {}

protected void initZmq() {
    zmqContext = ZMQ.context(0);
    zmqClient = zmqContext.socket(ZMQ.REQ);
    zmqClient.connect(getAddress());
}

protected void closeZmq() {
    zmqClient.close();
    zmqContext.term();
}

protected abstract void requestInit();

public String getAddress() {
    return addressNoPort+servicePort;
}

public String getInitMessage() {
    return serviceId+":init";
}

}

我如何运行我的应用程序。

public class TestServiceRunner {

public static void main(String[] args) {
    for (String string : args) {
        System.out.println("Params = "+string);
    }

    System.out.println("Starting Test service ...");

    TestService testService = new TestService(args);
    testService.run();
}

}

我的堆栈跟踪

Exception in thread "main" org.zeromq.ZMQException: No thread available(0x9523dfe)
    at org.zeromq.ZMQ$Socket.connect(Native Method)
    at GenericService.initZmq(GenericService.java:22)
    at TestService.run(TestService.java:25)
    at TestServiceRunner.main(TestServiceRunner.java:13)

1 个答案:

答案 0 :(得分:2)

想出这个!

GenericService中的

zmqContext = ZMQ.context(0);行是问题......该行应该是zmqContext = ZMQ.context(1);