diff options
author | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-01-17 18:18:46 +0100 |
---|---|---|
committer | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-01-17 18:18:46 +0100 |
commit | ee986f93216e63460a6aaba1cb7d2f451401a23a (patch) | |
tree | 075f06440ad466ce1c26a549d3e0e27edc3661f7 /same/src | |
parent | a2f0a58484bdfc22a8af7b16df051c1e611fc83d (diff) |
Add timeout to network operations.
Diffstat (limited to 'same/src')
-rw-r--r-- | same/src/main/java/com/orbekk/same/ClientApp.java | 4 | ||||
-rw-r--r-- | same/src/main/java/com/orbekk/same/ConnectionManagerImpl.java | 14 | ||||
-rw-r--r-- | same/src/main/java/com/orbekk/same/MasterApp.java | 4 |
3 files changed, 18 insertions, 4 deletions
diff --git a/same/src/main/java/com/orbekk/same/ClientApp.java b/same/src/main/java/com/orbekk/same/ClientApp.java index bf391dc..0effab4 100644 --- a/same/src/main/java/com/orbekk/same/ClientApp.java +++ b/same/src/main/java/com/orbekk/same/ClientApp.java @@ -10,11 +10,13 @@ import com.orbekk.net.HttpUtil; public class ClientApp { private Logger logger = LoggerFactory.getLogger(getClass()); private Server server; + private static final int timeout = 1000; public void run(int port, String networkName, String masterUrl) { logger.info("Starting client with port:{}, networkName:{}, masterUrl:{}", new Object[]{port, networkName, masterUrl}); - ConnectionManagerImpl connections = new ConnectionManagerImpl(); + ConnectionManagerImpl connections = new ConnectionManagerImpl(timeout, + timeout); State state = new State(networkName); Broadcaster broadcaster = BroadcasterImpl.getDefaultBroadcastRunner(connections); diff --git a/same/src/main/java/com/orbekk/same/ConnectionManagerImpl.java b/same/src/main/java/com/orbekk/same/ConnectionManagerImpl.java index 0a1e54f..f74bca7 100644 --- a/same/src/main/java/com/orbekk/same/ConnectionManagerImpl.java +++ b/same/src/main/java/com/orbekk/same/ConnectionManagerImpl.java @@ -8,16 +8,26 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class ConnectionManagerImpl implements ConnectionManager { - + private int connectionTimeout; + private int readTimeout; + private Logger logger = LoggerFactory.getLogger(getClass()); - public ConnectionManagerImpl() { + /** + * @param connectionTimout Timeout for establishing connection in milliseconds. + * @param readTimeout Timeout for waiting for answer in milliseconds. + */ + public ConnectionManagerImpl(int connectionTimout, int readTimeout) { + this.connectionTimeout = connectionTimout; + this.readTimeout = readTimeout; } private <T>T getClassProxy(String url, Class<T> clazz) { T service = null; try { JsonRpcHttpClient client = new JsonRpcHttpClient(new URL(url)); + client.setConnectionTimeoutMillis(connectionTimeout); + client.setReadTimeoutMillis(readTimeout); service = ProxyUtil.createProxy( this.getClass().getClassLoader(), clazz, diff --git a/same/src/main/java/com/orbekk/same/MasterApp.java b/same/src/main/java/com/orbekk/same/MasterApp.java index 3e22563..1aff6fc 100644 --- a/same/src/main/java/com/orbekk/same/MasterApp.java +++ b/same/src/main/java/com/orbekk/same/MasterApp.java @@ -9,9 +9,11 @@ import com.googlecode.jsonrpc4j.JsonRpcServer; public class MasterApp { private Logger logger = LoggerFactory.getLogger(getClass()); private Server server; + private static final int timeout = 1000; public void run(int port) { - ConnectionManagerImpl connections = new ConnectionManagerImpl(); + ConnectionManagerImpl connections = new ConnectionManagerImpl(timeout, + timeout); State state = new State("MasterNetwork"); Broadcaster broadcaster = BroadcasterImpl.getDefaultBroadcastRunner(connections); |