Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Netty阻止Tcp请求_Java_Multithreading_Tcp_Netty_Future - Fatal编程技术网

Java 使用Netty阻止Tcp请求

Java 使用Netty阻止Tcp请求,java,multithreading,tcp,netty,future,Java,Multithreading,Tcp,Netty,Future,我是新来的,我的英语不是母语。首先,对于这种情况,我会尽可能多地解释 我有一个连接到netty服务器并发送tcp请求的应用程序。例如,我们必须使用该服务器进行阻塞和非阻塞操作 1-一些请求将异步执行,我将向服务器发送请求,我不等待响应。在这种情况下没有问题 2-第二种情况是同步。当我向服务器发送带有事务id的请求时,我必须等待具有相同事务id的响应。我找不到关于阻止netty请求的明确示例。所有这些都是关于promise future的,它的实现方式如下,但它不是线程安全的。当我与~40tps一

我是新来的,我的英语不是母语。首先,对于这种情况,我会尽可能多地解释

我有一个连接到netty服务器并发送tcp请求的应用程序。例如,我们必须使用该服务器进行阻塞和非阻塞操作

1-一些请求将异步执行,我将向服务器发送请求,我不等待响应。在这种情况下没有问题

2-第二种情况是同步。当我向服务器发送带有事务id的请求时,我必须等待具有相同事务id的响应。我找不到关于阻止netty请求的明确示例。所有这些都是关于promise future的,它的实现方式如下,但它不是线程安全的。当我与~40tps一起使用时,一些请求将不会与响应消息一起收集,一些消息正在转义。例如,在1分钟内请求2400条消息,2000条消息正常,但或多或少有400条消息转义。我看到这个400消息从服务器返回,但future的get方法没有得到它,并且超时

网络客户端配置

Bootstrap clientBootstrap = new Bootstrap()
                    .group(group)
                    .channel(NioSocketChannel.class)
                    .option(ChannelOption.SO_KEEPALIVE,true)
                    .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 50)
                    .remoteAddress(new InetSocketAddress(host.getIp(), host.getPort()))
                    .handler(new ChannelInitializer<SocketChannel>() {
                        protected void initChannel(SocketChannel socketChannel) {
                            socketChannel.config().setRecvByteBufAllocator(new FixedRecvByteBufAllocator(2146));
                            ClientHandler clientHandler = new clientHandler(host);
                            socketChannel.pipeline().addLast(clientHandler);
                        }
                    });

            channelFuture = clientBootstrap.connect().sync(); 
            channelFuture.channel().closeFuture().sync();
最后,我得到了这样的未来承诺

@Override
public void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf in) {
        String input = in.toString(CharsetUtil.UTF_8);
        String transactionKey = input.substring(52, 66).trim();
        if(responseFuture.get(Long.valueOf(transactionKey))!=null)
            responseFuture.get(Long.valueOf(transactionKey)).set(input);
        else
            log.info("[{}] Tcp Response map is empty",transactionKey);
        }
Object obj = responseFuture.get(ddaTimeoutParam, TimeUnit.MILLISECONDS);
      if(obj!=null) {
          response = obj.toString();
          ddaDelta = System.currentTimeMillis()-ddaRequestStartTime;
      }
有时,当这一行
responseFuture.get(Long.valueOf(transactionKey)).set(input)
responseFuture.get
之前工作时,一些并发线程正在运行。如何解决此问题?当收到响应时,我可以使用netty的其他好方法发送事件吗

Object obj = responseFuture.get(ddaTimeoutParam, TimeUnit.MILLISECONDS);
      if(obj!=null) {
          response = obj.toString();
          ddaDelta = System.currentTimeMillis()-ddaRequestStartTime;
      }