From ae4a159eaabef961c50b97d63c80a33baf676700 Mon Sep 17 00:00:00 2001 From: sancar Date: Thu, 2 Aug 2018 16:49:51 +0300 Subject: [PATCH] Throw ClientNotActiveException instead of generic HazelcastException alive check in ClientConnectionManager is moved to a more centralized method and type of the exception is fixed. --- .../nio/ClientConnectionManagerImpl.java | 10 ++++------ .../com/hazelcast/client/ClientReconnectTest.java | 14 +++++++++++++- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/hazelcast-client/src/main/java/com/hazelcast/client/connection/nio/ClientConnectionManagerImpl.java b/hazelcast-client/src/main/java/com/hazelcast/client/connection/nio/ClientConnectionManagerImpl.java index f14ba63eebfa..ec6d19a24557 100644 --- a/hazelcast-client/src/main/java/com/hazelcast/client/connection/nio/ClientConnectionManagerImpl.java +++ b/hazelcast-client/src/main/java/com/hazelcast/client/connection/nio/ClientConnectionManagerImpl.java @@ -18,6 +18,7 @@ import com.hazelcast.client.AuthenticationException; import com.hazelcast.client.ClientExtension; +import com.hazelcast.client.HazelcastClientNotActiveException; import com.hazelcast.client.HazelcastClientOfflineException; import com.hazelcast.client.config.ClientNetworkConfig; import com.hazelcast.client.connection.AddressProvider; @@ -322,6 +323,9 @@ private Connection getConnection(Address target, boolean asOwner, boolean acquir } private void checkAllowed(Address target, boolean asOwner, boolean acquiresResources) throws IOException { + if (!alive) { + throw new HazelcastClientNotActiveException("ConnectionManager is not active!"); + } if (asOwner) { connectionStrategy.beforeConnectToCluster(target); //opening an owner connection is always allowed @@ -376,9 +380,6 @@ private AuthenticationFuture triggerConnect(Address target, boolean asOwner) { if (!asOwner) { connectionStrategy.beforeOpenConnection(target); } - if (!alive) { - throw new HazelcastException("ConnectionManager is not active!"); - } AuthenticationFuture future = new AuthenticationFuture(); AuthenticationFuture oldFuture = connectionsInProgress.putIfAbsent(target, future); @@ -459,9 +460,6 @@ private void bindSocketToPort(Socket socket) throws IOException { } protected ClientConnection createSocketConnection(final Address remoteAddress) throws IOException { - if (!alive) { - throw new HazelcastException("ConnectionManager is not active!"); - } SocketChannel socketChannel = null; try { socketChannel = SocketChannel.open(); diff --git a/hazelcast-client/src/test/java/com/hazelcast/client/ClientReconnectTest.java b/hazelcast-client/src/test/java/com/hazelcast/client/ClientReconnectTest.java index b8e9ce8f5d53..65b3d55c5814 100644 --- a/hazelcast-client/src/test/java/com/hazelcast/client/ClientReconnectTest.java +++ b/hazelcast-client/src/test/java/com/hazelcast/client/ClientReconnectTest.java @@ -152,7 +152,7 @@ public void testRequestShouldFailOnShutdown() { } @Test(expected = HazelcastClientNotActiveException.class) - public void testExceptionAfterClientShutdown() throws Exception { + public void testExceptionAfterClientShutdown() { hazelcastFactory.newHazelcastInstance(); ClientConfig clientConfig = new ClientConfig(); HazelcastInstance client = hazelcastFactory.newHazelcastClient(clientConfig); @@ -165,6 +165,18 @@ public void testExceptionAfterClientShutdown() throws Exception { test.get("key"); } + @Test(expected = HazelcastClientNotActiveException.class) + public void testExceptionAfterClientShutdown_fromClientConnectionManager() { + hazelcastFactory.newHazelcastInstance(); + ClientConfig clientConfig = new ClientConfig(); + HazelcastInstance client = hazelcastFactory.newHazelcastClient(clientConfig); + + IMap test = client.getMap("test"); + test.put("key", "value"); + client.shutdown(); + test.size(); + } + @Test public void testShutdownClient_whenThereIsNoCluster() { ClientConfig clientConfig = new ClientConfig();