1

我正在尝试编写一个简单的 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)
4

1 回答 1

3

想出了这个!

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

于 2014-03-31T19:54:41.290 回答