You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by cu...@apache.org on 2010/07/20 22:55:26 UTC

svn commit: r966007 - in /avro/trunk: CHANGES.txt lang/java/src/java/org/apache/avro/ipc/NettyServer.java lang/java/src/test/java/org/apache/avro/ipc/TestNettyServer.java

Author: cutting
Date: Tue Jul 20 20:55:26 2010
New Revision: 966007

URL: http://svn.apache.org/viewvc?rev=966007&view=rev
Log:
AVRO-596.  Java: Start Netty server eagerly in constructor.  Contributed by Patrick Linehan.

Modified:
    avro/trunk/CHANGES.txt
    avro/trunk/lang/java/src/java/org/apache/avro/ipc/NettyServer.java
    avro/trunk/lang/java/src/test/java/org/apache/avro/ipc/TestNettyServer.java

Modified: avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=966007&r1=966006&r2=966007&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Tue Jul 20 20:55:26 2010
@@ -66,6 +66,9 @@ Avro 1.4.0 (unreleased)
 
     AVRO-494. Add support for default values to IDL.  (cutting)
 
+    AVRO-596. Start Netty server eagerly in constructor.
+    (Patrick Linehan via cutting)
+
   BUG FIXES
 
     AVRO-502. Memory leak from parsing JSON schema.

Modified: avro/trunk/lang/java/src/java/org/apache/avro/ipc/NettyServer.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/src/java/org/apache/avro/ipc/NettyServer.java?rev=966007&r1=966006&r2=966007&view=diff
==============================================================================
--- avro/trunk/lang/java/src/java/org/apache/avro/ipc/NettyServer.java (original)
+++ avro/trunk/lang/java/src/java/org/apache/avro/ipc/NettyServer.java Tue Jul 20 20:55:26 2010
@@ -22,6 +22,7 @@ import java.io.IOException;
 import java.net.InetSocketAddress;
 import java.nio.ByteBuffer;
 import java.util.List;
+import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.Executors;
 
 import org.apache.avro.ipc.NettyTransportCodec.NettyDataPack;
@@ -49,40 +50,20 @@ import org.slf4j.LoggerFactory;
 /**
  * A Netty-based RPC {@link Server} implementation.
  */
-public class NettyServer extends Thread implements Server {
+public class NettyServer implements Server {
   private static final Logger LOG = LoggerFactory.getLogger(NettyServer.class
       .getName());
 
   private Responder responder;
-  private InetSocketAddress addr;
 
   private Channel serverChannel;
   private ChannelGroup allChannels = new DefaultChannelGroup(
       "avro-netty-server");
   private ChannelFactory channelFactory;
-
+  private CountDownLatch closed = new CountDownLatch(1);
+  
   public NettyServer(Responder responder, InetSocketAddress addr) {
     this.responder = responder;
-    this.addr = addr;
-
-    setName("AvroNettyServer on " + addr);
-    setDaemon(true);
-  }
-
-  @Override
-  public void close() {
-    ChannelGroupFuture future = allChannels.close();
-    future.awaitUninterruptibly();
-    channelFactory.releaseExternalResources();
-  }
-
-  @Override
-  public int getPort() {
-    return ((InetSocketAddress) serverChannel.getLocalAddress()).getPort();
-  }
-
-  @Override
-  public void run() {
     channelFactory = new NioServerSocketChannelFactory(Executors
         .newCachedThreadPool(), Executors.newCachedThreadPool());
     ServerBootstrap bootstrap = new ServerBootstrap(channelFactory);
@@ -100,6 +81,29 @@ public class NettyServer extends Thread 
     allChannels.add(serverChannel);
   }
 
+  @Override
+  public void start() {
+    // No-op.
+  }
+  
+  @Override
+  public void close() {
+    ChannelGroupFuture future = allChannels.close();
+    future.awaitUninterruptibly();
+    channelFactory.releaseExternalResources();
+    closed.countDown();
+  }
+  
+  @Override
+  public int getPort() {
+    return ((InetSocketAddress) serverChannel.getLocalAddress()).getPort();
+  }
+
+  @Override
+  public void join() throws InterruptedException {
+    closed.await();
+  }
+
   /**
    * Avro server handler for the Netty transport 
    */

Modified: avro/trunk/lang/java/src/test/java/org/apache/avro/ipc/TestNettyServer.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/src/test/java/org/apache/avro/ipc/TestNettyServer.java?rev=966007&r1=966006&r2=966007&view=diff
==============================================================================
--- avro/trunk/lang/java/src/test/java/org/apache/avro/ipc/TestNettyServer.java (original)
+++ avro/trunk/lang/java/src/test/java/org/apache/avro/ipc/TestNettyServer.java Tue Jul 20 20:55:26 2010
@@ -29,7 +29,6 @@ public class TestNettyServer {
     Responder responder = new SpecificResponder(Mail.class, new MailImpl());
     Server server = new NettyServer(responder, new InetSocketAddress(0));
     server.start();
-    Thread.sleep(1000); // waiting for server startup
 
     int serverPort = server.getPort();
     System.out.println("server port : " + serverPort);