问题
i have this senario : client that try to send to server , but the server is offline . the socket.send(request, 0); in inside java Timer thread :
this is the thread run() the ZMQ.Socket m_socket is initiates outside the thread
public void run() {
m_socket.send(request, 0);
byte[] reply = m_socket.recv(0);
//get seperator
byte[] empty1 = m_socket.recv(0);
//get secound frame
byte[] byteFileStruct = m_socket.recv(0);
if(null!=reply) // this is indication that the server is offline , is there better way to check ?
{
......
}
}
now the Timer triggers the run() methods again and when it execute the m_socket.send(request, 0); in the second round im getting :
Exception in thread "Timer-0" org.zeromq.ZMQException: Operation cannot be accomplished in current state(0x9523dfb)
at org.zeromq.ZMQ$Socket.send(Native Method)
at com.agent.core.Poller$PollarWorker.run(Poller.java:155)
at java.util.TimerThread.mainLoop(Unknown Source)
at java.util.TimerThread.run(Unknown Source)
do i need to create new socket each round ? that is this :
m_context = ZMQ.context(1);
m_socket = m_context.socket(ZMQ.REQ);
m_socket.setReceiveTimeOut(2000);
m_socket.setSendTimeOut(2000);
m_socket.connect (sControllerDomain);
回答1:
It's not clear from the code, but it looks like you're creating the socket on one thread, and using it on another one.
ZeroMQ socket must be used on the thread where it has been created. On the other hand, one ZeroMQ context is usually enough for an application.
Working with ZeroMQ sockets is much more convenient in message loops: socket is created before loop and destroyed right after it. Really, there's no need for timers/monitors/etc., ZeroMQ itself provides synchronisation primitives.
I'd recommend to review these chapters in ZeroMQ guide:
Multithreading with ØMQ
Chapter 3 - Advanced Request-Reply Patterns
来源:https://stackoverflow.com/questions/14649666/zeromq-getting-exception-in-thread-timer-0-org-zeromq-zmqexception-operatio