重试时无法在当前状态

重试时无法在当前状态

本文介绍了ZeroMQ:在线程“Timer-0"中获取异常org.zeromq.ZMQException:重试时无法在当前状态(0x9523dfb)下完成操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个场景:尝试发送到服务器的客户端,但服务器处于脱机状态.socket.send(request, 0);在 Java Timer 线程中:

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 :

这是线程 run()ZMQ.Socket m_socket 是在线程外发起的

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 ?
  {
    ......
  }

}

现在 Timer 再次触发 run() 方法,并在它执行 m_socket.send(request, 0) 时;在第二轮我得到:

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);

推荐答案

从代码中看不清楚,但看起来您是在一个线程上创建套接字,然后在另一个线程上使用它.

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 套接字必须在创建它的线程上使用.另一方面,一个 ZeroMQ 上下文通常对于应用程序就足够了.

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.

在消息循环中使用 ZeroMQ 套接字要方便得多:套接字在循环之前创建并在循环之后立即销毁.真的,不需要定时器/监视器等,ZeroMQ 本身提供同步原语.

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.

我建议您阅读 ZeroMQ 指南中的这些章节:

I'd recommend to review these chapters in ZeroMQ guide:

使用ØMQ的多线程

第 3 章 - 高级请求-回复模式

这篇关于ZeroMQ:在线程“Timer-0"中获取异常org.zeromq.ZMQException:重试时无法在当前状态(0x9523dfb)下完成操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 08:53