You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by tr...@apache.org on 2006/11/28 03:50:24 UTC

svn commit: r479859 - in /mina/trunk/core/src/main/java/org/apache/mina/transport/socket/nio/support: DatagramAcceptorDelegate.java DatagramConnectorDelegate.java

Author: trustin
Date: Mon Nov 27 18:50:24 2006
New Revision: 479859

URL: http://svn.apache.org/viewvc?view=rev&rev=479859
Log:
Related issue: DIRMINA-309 (Decreasing performance in case of fewer number of connections)
* Changed selector fields to final and make it opened when the service is constructed and closed when finialized by VM


Modified:
    mina/trunk/core/src/main/java/org/apache/mina/transport/socket/nio/support/DatagramAcceptorDelegate.java
    mina/trunk/core/src/main/java/org/apache/mina/transport/socket/nio/support/DatagramConnectorDelegate.java

Modified: mina/trunk/core/src/main/java/org/apache/mina/transport/socket/nio/support/DatagramAcceptorDelegate.java
URL: http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/transport/socket/nio/support/DatagramAcceptorDelegate.java?view=diff&rev=479859&r1=479858&r2=479859
==============================================================================
--- mina/trunk/core/src/main/java/org/apache/mina/transport/socket/nio/support/DatagramAcceptorDelegate.java (original)
+++ mina/trunk/core/src/main/java/org/apache/mina/transport/socket/nio/support/DatagramAcceptorDelegate.java Mon Nov 27 18:50:24 2006
@@ -38,6 +38,7 @@
 import org.apache.mina.common.IoSession;
 import org.apache.mina.common.IoSessionConfig;
 import org.apache.mina.common.IoSessionRecycler;
+import org.apache.mina.common.RuntimeIOException;
 import org.apache.mina.common.IoFilter.WriteRequest;
 import org.apache.mina.common.support.BaseIoAcceptor;
 import org.apache.mina.common.support.IoServiceListenerSupport;
@@ -62,7 +63,7 @@
     private final IoAcceptor wrapper;
     private final Executor executor;
     private final int id = nextId ++ ;
-    private Selector selector;
+    private final Selector selector;
     private DatagramChannel channel;
     private final Queue<RegistrationRequest> registerQueue = new ConcurrentLinkedQueue<RegistrationRequest>();
     private final Queue<CancellationRequest> cancelQueue = new ConcurrentLinkedQueue<CancellationRequest>();
@@ -75,10 +76,33 @@
     public DatagramAcceptorDelegate( IoAcceptor wrapper, Executor executor )
     {
         super( new DefaultDatagramSessionConfig() );
+
+        try
+        {
+            this.selector = Selector.open();
+        }
+        catch( IOException e )
+        {
+            throw new RuntimeIOException( "Failed to open a selector.", e );
+        }
+        
         this.wrapper = wrapper;
         this.executor = executor;
     }
 
+    protected void finalize() throws Throwable
+    {
+        super.finalize();
+        try
+        {
+            selector.close();
+        }
+        catch( IOException e )
+        {
+            ExceptionMonitor.getInstance().exceptionCaught( e );
+        }
+    }
+
     protected Class<? extends SocketAddress> getAddressType()
     {
         return InetSocketAddress.class;
@@ -93,11 +117,9 @@
     protected void doBind() throws IOException
     {
         RegistrationRequest request = new RegistrationRequest();
-        synchronized( this )
-        {
-            registerQueue.offer( request );
-            startupWorker();
-        }
+
+        registerQueue.offer( request );
+        startupWorker();
         selector.wakeup();
         
         synchronized( request )
@@ -127,23 +149,9 @@
     protected void doUnbind()
     {
         CancellationRequest request = new CancellationRequest();
-        synchronized( this )
-        {
-            try
-            {
-                startupWorker();
-            }
-            catch( IOException e )
-            {
-                // IOException is thrown only when Worker thread is not
-                // running and failed to open a selector.  We simply throw
-                // IllegalArgumentException here because we can simply
-                // conclude that nothing is bound to the selector.
-                throw new IllegalArgumentException( "Address not bound: " + getLocalAddress() );
-            }
 
-            cancelQueue.offer( request );
-        }
+        cancelQueue.offer( request );
+        startupWorker();
         selector.wakeup();
         
         synchronized( request )
@@ -256,11 +264,10 @@
         this.getThreadModel().buildFilterChain( session.getFilterChain() );
     }
     
-    private synchronized void startupWorker() throws IOException
+    private synchronized void startupWorker()
     {
         if( worker == null )
         {
-            selector = Selector.open();
             worker = new Worker();
             executor.execute( new NamePreservingRunnable( worker ) );
         }
@@ -316,18 +323,6 @@
                                 cancelQueue.isEmpty() )
                             {
                                 worker = null;
-                                try
-                                {
-                                    selector.close();
-                                }
-                                catch( IOException e )
-                                {
-                                    ExceptionMonitor.getInstance().exceptionCaught( e );
-                                }
-                                finally
-                                {
-                                    selector = null;
-                                }
                                 break;
                             }
                         }

Modified: mina/trunk/core/src/main/java/org/apache/mina/transport/socket/nio/support/DatagramConnectorDelegate.java
URL: http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/transport/socket/nio/support/DatagramConnectorDelegate.java?view=diff&rev=479859&r1=479858&r2=479859
==============================================================================
--- mina/trunk/core/src/main/java/org/apache/mina/transport/socket/nio/support/DatagramConnectorDelegate.java (original)
+++ mina/trunk/core/src/main/java/org/apache/mina/transport/socket/nio/support/DatagramConnectorDelegate.java Mon Nov 27 18:50:24 2006
@@ -39,6 +39,7 @@
 import org.apache.mina.common.IoSession;
 import org.apache.mina.common.IoSessionConfig;
 import org.apache.mina.common.IoSessionRecycler;
+import org.apache.mina.common.RuntimeIOException;
 import org.apache.mina.common.IoFilter.WriteRequest;
 import org.apache.mina.common.support.AbstractIoFilterChain;
 import org.apache.mina.common.support.BaseIoConnector;
@@ -64,7 +65,7 @@
     private final IoConnector wrapper;
     private final Executor executor;
     private final int id = nextId ++ ;
-    private Selector selector;
+    private final Selector selector;
     private final Queue<RegistrationRequest> registerQueue = new ConcurrentLinkedQueue<RegistrationRequest>();
     private final Queue<DatagramSessionImpl> cancelQueue = new ConcurrentLinkedQueue<DatagramSessionImpl>();
     private final Queue<DatagramSessionImpl> flushingSessions = new ConcurrentLinkedQueue<DatagramSessionImpl>();
@@ -77,10 +78,33 @@
     public DatagramConnectorDelegate( IoConnector wrapper, Executor executor )
     {
         super( new DefaultDatagramSessionConfig() );
+
+        try
+        {
+            this.selector = Selector.open();
+        }
+        catch( IOException e )
+        {
+            throw new RuntimeIOException( "Failed to open a selector.", e );
+        }
+
         this.wrapper = wrapper;
         this.executor = executor;
     }
 
+    protected void finalize() throws Throwable
+    {
+        super.finalize();
+        try
+        {
+            selector.close();
+        }
+        catch( IOException e )
+        {
+            ExceptionMonitor.getInstance().exceptionCaught( e );
+        }
+    }
+
     protected Class<? extends SocketAddress> getAddressType()
     {
         return InetSocketAddress.class;
@@ -140,31 +164,11 @@
         }
 
         RegistrationRequest request = new RegistrationRequest( ch );
-        synchronized( this )
-        {
-            try
-            {
-                startupWorker();
-            }
-            catch( IOException e )
-            {
-                try
-                {
-                    ch.disconnect();
-                    ch.close();
-                }
-                catch( IOException e2 )
-                {
-                    ExceptionMonitor.getInstance().exceptionCaught( e2 );
-                }
-
-                return DefaultConnectFuture.newFailedFuture( e );
-            }
-            
-            registerQueue.offer( request );
-        }
 
+        registerQueue.offer( request );
+        startupWorker();
         selector.wakeup();
+
         return request;
     }
     
@@ -183,11 +187,10 @@
         this.sessionRecycler = sessionRecycler;
     }
 
-    private synchronized void startupWorker() throws IOException
+    private synchronized void startupWorker() 
     {
         if( worker == null )
         {
-            selector = Selector.open();
             worker = new Worker();
             executor.execute( new NamePreservingRunnable( worker ) );
         }
@@ -195,25 +198,8 @@
 
     public void closeSession( DatagramSessionImpl session )
     {
-        synchronized( this )
-        {
-            try
-            {
-                startupWorker();
-            }
-            catch( IOException e )
-            {
-                // IOException is thrown only when Worker thread is not
-                // running and failed to open a selector.  We simply return
-                // silently here because it we can simply conclude that
-                // this session is not managed by this connector or
-                // already closed.
-                return;
-            }
-
-            cancelQueue.offer( session );
-        }
-
+        cancelQueue.offer( session );
+        startupWorker();
         selector.wakeup();
     }
 
@@ -321,18 +307,6 @@
                                 cancelQueue.isEmpty() )
                             {
                                 worker = null;
-                                try
-                                {
-                                    selector.close();
-                                }
-                                catch( IOException e )
-                                {
-                                    ExceptionMonitor.getInstance().exceptionCaught( e );
-                                }
-                                finally
-                                {
-                                    selector = null;
-                                }
                                 break;
                             }
                         }