[grizzly~git:281e7a3c] [2.3.x] Fix for http://java.net/jira/browse/GRIZZLY-1420 (Need a way to c

  • From: rlubke@...
  • To: commits@...
  • Subject: [grizzly~git:281e7a3c] [2.3.x] Fix for http://java.net/jira/browse/GRIZZLY-1420 (Need a way to c
  • Date: Fri, 1 Feb 2013 20:28:42 +0000

Project:    grizzly
Repository: git
Revision:   281e7a3c3efdf680467f6902116a2b316c5f3e4e
Author:     rlubke
Date:       2013-02-01 20:28:30 UTC
Link:       

Log Message:
------------
[2.3.x] Fix for http://java.net/jira/browse/GRIZZLY-1420 ;(Need a way to 
configure the timeout for nonblocking IO).


Revisions:
----------
281e7a3c3efdf680467f6902116a2b316c5f3e4e


Modified Paths:
---------------
modules/config/src/main/java/org/glassfish/grizzly/config/GenericGrizzlyListener.java
modules/config/src/main/java/org/glassfish/grizzly/config/dom/Transport.java
modules/grizzly/src/main/java/org/glassfish/grizzly/AbstractTransport.java
modules/grizzly/src/main/java/org/glassfish/grizzly/Transport.java
modules/grizzly/src/main/java/org/glassfish/grizzly/nio/transport/TCPNIOTransport.java
modules/grizzly/src/main/java/org/glassfish/grizzly/nio/transport/UDPNIOTransport.java
modules/grizzly/src/test/java/org/glassfish/grizzly/TCPNIOTransportTest.java
modules/grizzly/src/test/java/org/glassfish/grizzly/UDPNIOTransportTest.java


Diffs:
------
diff --git 
a/modules/config/src/main/java/org/glassfish/grizzly/config/GenericGrizzlyListener.java
 
b/modules/config/src/main/java/org/glassfish/grizzly/config/GenericGrizzlyListener.java
index 9ae2c05..4b59738 100644
--- 
a/modules/config/src/main/java/org/glassfish/grizzly/config/GenericGrizzlyListener.java
+++ 
b/modules/config/src/main/java/org/glassfish/grizzly/config/GenericGrizzlyListener.java
@@ -274,6 +274,8 @@ public class GenericGrizzlyListener implements 
GrizzlyListener {
         final TCPNIOTransport tcpTransport = 
TCPNIOTransportBuilder.newInstance().build();
         
tcpTransport.setTcpNoDelay(Boolean.parseBoolean(transportConfig.getTcpNoDelay()));
         
tcpTransport.setLinger(Integer.parseInt(transportConfig.getLinger()));
+        
tcpTransport.setWriteTimeout(Long.parseLong(transportConfig.getWriteTimeoutMillis()),
 TimeUnit.MILLISECONDS);
+        
tcpTransport.setReadTimeout(Long.parseLong(transportConfig.getReadTimeoutMillis()),
 TimeUnit.MILLISECONDS);
         
tcpTransport.setServerConnectionBackLog(Integer.parseInt(transportConfig.getMaxConnectionsCount()));
         return tcpTransport;
     }
diff --git 
a/modules/config/src/main/java/org/glassfish/grizzly/config/dom/Transport.java
 
b/modules/config/src/main/java/org/glassfish/grizzly/config/dom/Transport.java
index 1e1f6b2..0ce9e9d 100644
--- 
a/modules/config/src/main/java/org/glassfish/grizzly/config/dom/Transport.java
+++ 
b/modules/config/src/main/java/org/glassfish/grizzly/config/dom/Transport.java
@@ -64,6 +64,7 @@ public interface Transport extends ConfigBeanProxy, 
PropertyBag {
     int LINGER = -1;
     int MAX_CONNECTIONS_COUNT = 4096;
     int READ_TIMEOUT = 30000;
+    int WRITE_TIMEOUT = 30000;
     int SELECTOR_POLL_TIMEOUT = 1000;
     String BYTE_BUFFER_TYPE = "HEAP";
     String CLASSNAME = "org.glassfish.grizzly.nio.transport.TCPNIOTransport";
@@ -175,7 +176,7 @@ public interface Transport extends ConfigBeanProxy, 
PropertyBag {
     /**
      * Write operation timeout in ms
      */
-    @Attribute(defaultValue = "" + READ_TIMEOUT, dataType = Integer.class)
+    @Attribute(defaultValue = "" + WRITE_TIMEOUT, dataType = Integer.class)
     String getWriteTimeoutMillis();
 
     void setWriteTimeoutMillis(String value);
diff --git 
a/modules/grizzly/src/main/java/org/glassfish/grizzly/AbstractTransport.java 
b/modules/grizzly/src/main/java/org/glassfish/grizzly/AbstractTransport.java
index 8f7d395..6ee3f6a 100644
--- 
a/modules/grizzly/src/main/java/org/glassfish/grizzly/AbstractTransport.java
+++ 
b/modules/grizzly/src/main/java/org/glassfish/grizzly/AbstractTransport.java
@@ -42,6 +42,8 @@ package org.glassfish.grizzly;
 
 import java.io.IOException;
 import java.util.concurrent.ExecutorService;
+import java.util.concurrent.TimeUnit;
+
 import org.glassfish.grizzly.attributes.AttributeBuilder;
 import org.glassfish.grizzly.memory.MemoryManager;
 import org.glassfish.grizzly.monitoring.jmx.JmxMonitoringConfig;
@@ -129,6 +131,10 @@ public abstract class AbstractTransport implements 
Transport {
 
     protected boolean managedWorkerPool = true;
 
+    protected long writeTimeout = 
TimeUnit.MILLISECONDS.convert(DEFAULT_WRITE_TIMEOUT, TimeUnit.SECONDS);
+
+    protected long readTimeout = 
TimeUnit.MILLISECONDS.convert(DEFAULT_READ_TIMEOUT, TimeUnit.SECONDS);
+
     /**
      * Transport probes
      */
@@ -483,6 +489,68 @@ public abstract class AbstractTransport implements 
Transport {
     }
 
     /**
+     * {@inheritDoc}
+     */
+    @Override
+    public long getReadTimeout(TimeUnit timeUnit) {
+        if (readTimeout <= 0) {
+            return -1;
+        } else {
+            if (timeUnit == TimeUnit.MILLISECONDS) {
+                return readTimeout;
+            }
+            return timeUnit.convert(readTimeout, TimeUnit.MILLISECONDS);
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void setReadTimeout(long timeout, TimeUnit timeUnit) {
+        if (timeout <= 0) {
+            readTimeout = -1;
+        } else {
+            if (timeUnit == TimeUnit.MILLISECONDS) {
+                readTimeout = timeout;
+            } else {
+                readTimeout = TimeUnit.MILLISECONDS.convert(timeout, 
timeUnit);
+            }
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public long getWriteTimeout(TimeUnit timeUnit) {
+        if (writeTimeout <= 0) {
+            return -1;
+        } else {
+            if (timeUnit == TimeUnit.MILLISECONDS) {
+                return readTimeout;
+            }
+            return timeUnit.convert(writeTimeout, TimeUnit.MILLISECONDS);
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void setWriteTimeout(long timeout, TimeUnit timeUnit) {
+        if (timeout <= 0) {
+            writeTimeout = -1;
+        } else {
+            if (timeUnit == TimeUnit.MILLISECONDS) {
+                writeTimeout = timeout;
+            } else {
+                writeTimeout = TimeUnit.MILLISECONDS.convert(timeout, 
timeUnit);
+            }
+        }
+    }
+
+    /**
      * Notify registered {@link TransportProbe}s about the config changed 
event.
      *
      * @param transport the <tt>Transport</tt> event occurred on.
diff --git 
a/modules/grizzly/src/main/java/org/glassfish/grizzly/Transport.java 
b/modules/grizzly/src/main/java/org/glassfish/grizzly/Transport.java
index 0017f49..3036bdc 100644
--- a/modules/grizzly/src/main/java/org/glassfish/grizzly/Transport.java
+++ b/modules/grizzly/src/main/java/org/glassfish/grizzly/Transport.java
@@ -42,6 +42,8 @@ package org.glassfish.grizzly;
 
 import java.io.IOException;
 import java.util.concurrent.ExecutorService;
+import java.util.concurrent.TimeUnit;
+
 import org.glassfish.grizzly.attributes.Attribute;
 import org.glassfish.grizzly.attributes.AttributeBuilder;
 import org.glassfish.grizzly.memory.MemoryManager;
@@ -70,6 +72,17 @@ public interface Transport extends 
JmxMonitoringAware<TransportProbe> {
      * @since 2.2.8
      */
     static final int DEFAULT_READ_BUFFER_SIZE = 1024 * 64;
+
+    /**
+     * Default read timeout in seconds.
+     */
+    public static final int DEFAULT_READ_TIMEOUT = 30;
+
+    /**
+     * Default write timeout in seconds.
+     */
+    public static final int DEFAULT_WRITE_TIMEOUT = 30;
+
     
     enum State {STARTING, START, PAUSE, STOPPING, STOP}
 
@@ -500,4 +513,53 @@ public interface Transport extends 
JmxMonitoringAware<TransportProbe> {
      * @param error {@link Throwable}.
      */
     void notifyTransportError(Throwable error);
+
+    /**
+     * Returns the current value for the read timeout converted to the 
provided
+     * {@link TimeUnit} specification.  If this value hasn't been explicitly
+     * set, it will default to {@value #DEFAULT_READ_TIMEOUT} seconds.
+     *
+     * @param timeUnit the {@link TimeUnit} to convert the returned result 
to.
+     *
+     * @since 2.3
+     */
+    long getReadTimeout(TimeUnit timeUnit);
+
+    /**
+     * Specifies the timeout for reads.  This may be overridden on a 
per-connection basis.
+     * A value of zero or less effectively disables the timeout.
+     *
+     * @param timeout the new timeout value
+     * @param timeUnit the {@TimeUnit} specification of the provided value.
+     *
+     * @see Connection#setReadTimeout(long, java.util.concurrent.TimeUnit)
+     *
+     * @since 2.3
+     */
+    void setReadTimeout(long timeout, TimeUnit timeUnit);
+
+    /**
+     * Returns the current value for the write timeout converted to the 
provided
+     * {@link TimeUnit} specification.  If this value hasn't been explicitly
+     * set, it will default to {@value #DEFAULT_WRITE_TIMEOUT} seconds.
+     *
+     * @param timeUnit the {@link TimeUnit} to convert the returned result 
to.
+     *
+     * @since 2.3
+     */
+    long getWriteTimeout(TimeUnit timeUnit);
+
+    /**
+     * Specifies the timeout for writes.  This may be overridden on a 
per-connection basis.
+     * A value of zero or less effectively disables the timeout.
+     *
+     * @param timeout  the new timeout value
+     * @param timeUnit the {@TimeUnit} specification of the provided value.
+     *
+     * @see Connection#setWriteTimeout(long, java.util.concurrent.TimeUnit)
+     *
+     * @since 2.3
+     */
+    void setWriteTimeout(long timeout, TimeUnit timeUnit);
+
 }
diff --git 
a/modules/grizzly/src/main/java/org/glassfish/grizzly/nio/transport/TCPNIOTransport.java
 
b/modules/grizzly/src/main/java/org/glassfish/grizzly/nio/transport/TCPNIOTransport.java
index 8c268c4..4ebe32e 100644
--- 
a/modules/grizzly/src/main/java/org/glassfish/grizzly/nio/transport/TCPNIOTransport.java
+++ 
b/modules/grizzly/src/main/java/org/glassfish/grizzly/nio/transport/TCPNIOTransport.java
@@ -615,6 +615,8 @@ public final class TCPNIOTransport extends NIOTransport 
implements
         connection.configureStandalone(isStandalone);
         connection.setProcessor(processor);
         connection.setProcessorSelector(processorSelector);
+        connection.setReadTimeout(readTimeout, TimeUnit.MILLISECONDS);
+        connection.setWriteTimeout(writeTimeout, TimeUnit.MILLISECONDS);
         if (connectionMonitoringConfig.hasProbes()) {
             
connection.setMonitoringProbes(connectionMonitoringConfig.getProbes());
         }
diff --git 
a/modules/grizzly/src/main/java/org/glassfish/grizzly/nio/transport/UDPNIOTransport.java
 
b/modules/grizzly/src/main/java/org/glassfish/grizzly/nio/transport/UDPNIOTransport.java
index 3130028..104ec5d 100644
--- 
a/modules/grizzly/src/main/java/org/glassfish/grizzly/nio/transport/UDPNIOTransport.java
+++ 
b/modules/grizzly/src/main/java/org/glassfish/grizzly/nio/transport/UDPNIOTransport.java
@@ -845,6 +845,8 @@ public final class UDPNIOTransport extends NIOTransport 
implements
         connection.configureStandalone(isStandalone);
         connection.setProcessor(processor);
         connection.setProcessorSelector(processorSelector);
+        connection.setReadTimeout(readTimeout, TimeUnit.MILLISECONDS);
+        connection.setWriteTimeout(writeTimeout, TimeUnit.MILLISECONDS);
         if (connectionMonitoringConfig.hasProbes()) {
             
connection.setMonitoringProbes(connectionMonitoringConfig.getProbes());
         }
diff --git 
a/modules/grizzly/src/test/java/org/glassfish/grizzly/TCPNIOTransportTest.java
 
b/modules/grizzly/src/test/java/org/glassfish/grizzly/TCPNIOTransportTest.java
index 45f9538..90fbd5d 100644
--- 
a/modules/grizzly/src/test/java/org/glassfish/grizzly/TCPNIOTransportTest.java
+++ 
b/modules/grizzly/src/test/java/org/glassfish/grizzly/TCPNIOTransportTest.java
@@ -115,6 +115,19 @@ public class TCPNIOTransportTest extends GrizzlyTestCase 
{
         }
     }
 
+    public void testReadWriteTimeout() throws Exception {
+        TCPNIOTransport transport = 
TCPNIOTransportBuilder.newInstance().build();
+        assertEquals(30, transport.getWriteTimeout(TimeUnit.SECONDS));
+        assertEquals(30, transport.getReadTimeout(TimeUnit.SECONDS));
+        transport.setReadTimeout(45, TimeUnit.MINUTES);
+        assertEquals(TimeUnit.MILLISECONDS.convert(45, TimeUnit.MINUTES), 
transport.getReadTimeout(TimeUnit.MILLISECONDS));
+        assertEquals(30, transport.getWriteTimeout(TimeUnit.SECONDS));
+        transport.setReadTimeout(-5, TimeUnit.SECONDS);
+        assertEquals(-1, transport.getReadTimeout(TimeUnit.MILLISECONDS));
+        transport.setReadTimeout(0, TimeUnit.SECONDS);
+        assertEquals(-1, transport.getReadTimeout(TimeUnit.MILLISECONDS));
+    }
+
     public void testConnectorHandlerConnect() throws Exception {
         Connection connection = null;
         TCPNIOTransport transport = 
TCPNIOTransportBuilder.newInstance().build();
diff --git 
a/modules/grizzly/src/test/java/org/glassfish/grizzly/UDPNIOTransportTest.java
 
b/modules/grizzly/src/test/java/org/glassfish/grizzly/UDPNIOTransportTest.java
index 183550d..a964b4b 100644
--- 
a/modules/grizzly/src/test/java/org/glassfish/grizzly/UDPNIOTransportTest.java
+++ 
b/modules/grizzly/src/test/java/org/glassfish/grizzly/UDPNIOTransportTest.java
@@ -90,6 +90,19 @@ public class UDPNIOTransportTest extends GrizzlyTestCase {
         }
     }
 
+    public void testReadWriteTimeout() throws Exception {
+        UDPNIOTransport transport = 
UDPNIOTransportBuilder.newInstance().build();
+        assertEquals(30, transport.getWriteTimeout(TimeUnit.SECONDS));
+        assertEquals(30, transport.getReadTimeout(TimeUnit.SECONDS));
+        transport.setReadTimeout(45, TimeUnit.MINUTES);
+        assertEquals(TimeUnit.MILLISECONDS.convert(45, TimeUnit.MINUTES), 
transport.getReadTimeout(TimeUnit.MILLISECONDS));
+        assertEquals(30, transport.getWriteTimeout(TimeUnit.SECONDS));
+        transport.setReadTimeout(-5, TimeUnit.SECONDS);
+        assertEquals(-1, transport.getReadTimeout(TimeUnit.MILLISECONDS));
+        transport.setReadTimeout(0, TimeUnit.SECONDS);
+        assertEquals(-1, transport.getReadTimeout(TimeUnit.MILLISECONDS));
+    }
+
     public void testPortRangeBind() throws Exception {
         final int portsTest = 10;
         final PortRange portRange = new PortRange(PORT, PORT + portsTest - 
1);




[grizzly~git:281e7a3c] [2.3.x] Fix for http://java.net/jira/browse/GRIZZLY-1420 (Need a way to c

rlubke 02/01/2013
Terms of Use; Privacy Policy; Copyright ©2013-2014 (revision 20140418.2d69abc)
 
 
Close
loading
Please Confirm
Close