我正在尝试编写一个简单的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)
想通了这个!
泛型服务中的zmqContext = ZMQ.context(0);
行是问题所在...这条线应该zmqContext = ZMQ.context(1);