【问题标题】:jboss netty upload file handler using ChunkedWriteHandlerjboss netty 使用 ChunkedWriteHandler 上传文件处理程序
【发布时间】:2011-10-28 02:35:17
【问题描述】:

我正在使用 ChunkedWriteHandler 来处理 ChunkedFile 并遇到以下情况 如果我将 ChunkedFile 写入 channelFuture 侦听器内的通道,

我得到了 java.nio.channels.ClosedChannelException & java.lang.NullPointerException

当我做 clientChannel.write()

 InetSocketAddress inetSocketAddress = new InetSocketAddress("localhost", testPort);
 final ChannelFuture channelFuture =  clientBootstrap.connect(inetSocketAddress);

 channelFuture.addListener(
            new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    System.out.println("connected!");

                    Channel clientChannel = channelFuture.getChannel();
                    ChannelFuture channelFuture2 = clientChannel.write( new ChunkedFile( new File("c:\\openjdk-7-fcs-src-b147-27_jun_2011.zip"))  );

                    channelFuture2.addListener(
                            new ChannelFutureListener() {
                                @Override
                                public void operationComplete(ChannelFuture future) throws Exception {
                                    System.out.println("finished uploading!");
                                }
                            }
                    );

                }
        }
    );

我知道完成这项工作的唯一方法是: - 不使用监听器 - 并添加 Thread.sleep(1000)

    InetSocketAddress inetSocketAddress = new InetSocketAddress("localhost", testPort);
    final ChannelFuture channelFuture =  clientBootstrap.connect(inetSocketAddress);

    Thread.sleep(1000);

    Channel clientChannel = channelFuture.getChannel();
    ChannelFuture channelFuture2 = clientChannel.write( new ChunkedFile( new File("k:\\soft\\openjdk-7-fcs-src-b147-27_jun_2011.zip"))  );

    channelFuture2.addListener(
            new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    System.out.println("finished uploading!");
                }
            }
    );

我做错了什么?

完整代码(也在http://github.com/lydonchandra/netty/blob/master/src/test/java/org/jboss/netty/handler/stream/ChunkedWriteHandlerTest.java

            NioClientSocketChannelFactory channelFactory = new NioClientSocketChannelFactory(clientExecutor, Executors.newCachedThreadPool());
    ClientBootstrap clientBootstrap = new ClientBootstrap(channelFactory);
    clientBootstrap.setPipelineFactory(new ChannelPipelineFactory() {

        @Override
        public ChannelPipeline getPipeline() throws Exception {
            return Channels.pipeline(
                    new SimpleChannelUpstreamHandler() {

                        @Override
                          public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) {
                                // Send the first message.  Server will not send anything here
                                // because the firstMessage's capacity is 0.
                                System.out.println("client - channel connected");
                          }

                        @Override
                        public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
                            System.out.println("message received");
                        }

                        @Override
                              public void exceptionCaught(
                                      ChannelHandlerContext ctx, ExceptionEvent e) {
                                    // Close the connection when an exception is raised.
                                    System.out.println(e.getCause());
                                    e.getChannel().close();
                              }
                    },

                    new ChunkedWriteHandler()
            );
        }

    });

    InetSocketAddress inetSocketAddress = new InetSocketAddress("localhost", testPort);
    final ChannelFuture channelFuture =  clientBootstrap.connect(inetSocketAddress);

    //      channelFuture.addListener(
    //              new ChannelFutureListener() {
    //                  @Override
    //                  public void operationComplete(ChannelFuture future) throws Exception {
    //                      System.out.println("connected!");
    //                      
    //                      Channel clientChannel = channelFuture.getChannel();
    //                      ChannelFuture channelFuture2 = clientChannel.write( new ChunkedFile( new File("k:\\soft\\openjdk-7-fcs-src-b147-27_jun_2011.zip"))  );
    //                      
    //                      channelFuture2.addListener(
    //                              new ChannelFutureListener() {
    //                                  @Override
    //                                  public void operationComplete(ChannelFuture future) throws Exception {
    //                                      System.out.println("finished uploading!");
    //                                  }
    //                              }
    //                      );
    ////                        Thread.sleep(20000);
    ////                        clientChannel.getCloseFuture().await(100000);
    //                      
    //                  }
    //          }
    //      );

    // why do I need this sleep ??? for some reason, I can't just use channelFutureListener.operationComplete.
    // something else is killing the channel if i wrap things up inside channelFutureListener.operationComplete
    Thread.sleep(1000);

    Channel clientChannel = channelFuture.getChannel();
    ChannelFuture channelFuture2 = clientChannel.write( new ChunkedFile( new File("k:\\soft\\openjdk-7-fcs-src-b147-27_jun_2011.zip"))  );

    channelFuture2.addListener(
            new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    System.out.println("finished uploading!");
                }
            }
    );

【问题讨论】:

    标签: netty


    【解决方案1】:

    升级到 netty 3.2.7.Final。之前的版本(我认为是 3.2.5.Final)中有一个错误,它抛出了 NPE。

    【讨论】:

      猜你喜欢
      • 2023-01-20
      • 2013-09-29
      • 2012-01-18
      • 2019-09-16
      • 2013-06-11
      • 2013-03-06
      • 1970-01-01
      • 2010-11-17
      • 1970-01-01
      相关资源
      最近更新 更多