Return-Path: X-Original-To: apmail-cxf-commits-archive@www.apache.org Delivered-To: apmail-cxf-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 166FC17C19 for ; Mon, 3 Nov 2014 14:03:43 +0000 (UTC) Received: (qmail 47108 invoked by uid 500); 3 Nov 2014 14:03:43 -0000 Delivered-To: apmail-cxf-commits-archive@cxf.apache.org Received: (qmail 47046 invoked by uid 500); 3 Nov 2014 14:03:42 -0000 Mailing-List: contact commits-help@cxf.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cxf.apache.org Delivered-To: mailing list commits@cxf.apache.org Received: (qmail 47036 invoked by uid 99); 3 Nov 2014 14:03:42 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 03 Nov 2014 14:03:42 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id A9A2699B52A; Mon, 3 Nov 2014 14:03:42 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: sergeyb@apache.org To: commits@cxf.apache.org Message-Id: X-Mailer: ASF-Git Admin Mailer Subject: git commit: [CXFR-6066] Closing WebClient on finalize too Date: Mon, 3 Nov 2014 14:03:42 +0000 (UTC) Repository: cxf Updated Branches: refs/heads/master b7e74f08a -> 61c7b99b6 [CXFR-6066] Closing WebClient on finalize too Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/61c7b99b Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/61c7b99b Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/61c7b99b Branch: refs/heads/master Commit: 61c7b99b67fe4a29e61af4a079b75d5154e585f9 Parents: b7e74f0 Author: Sergey Beryozkin Authored: Mon Nov 3 14:03:26 2014 +0000 Committer: Sergey Beryozkin Committed: Mon Nov 3 14:03:26 2014 +0000 ---------------------------------------------------------------------- .../apache/cxf/jaxrs/client/AbstractClient.java | 65 ++++++++++++-------- 1 file changed, 39 insertions(+), 26 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/61c7b99b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java ---------------------------------------------------------------------- diff --git a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java index 46acbfb..a745d77 100644 --- a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java +++ b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java @@ -36,6 +36,7 @@ import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.logging.Logger; import javax.ws.rs.ProcessingException; @@ -107,7 +108,7 @@ public abstract class AbstractClient implements Client { protected ClientConfiguration cfg = new ClientConfiguration(); private ClientState state; - + private AtomicBoolean closed = new AtomicBoolean(); protected AbstractClient(ClientState initialState) { this.state = initialState; } @@ -286,33 +287,36 @@ public abstract class AbstractClient implements Client { } public void close() { - if (cfg.getBus() == null) { - return; - } - for (Closeable c : cfg.getEndpoint().getCleanupHooks()) { - try { - c.close(); - } catch (IOException e) { - //ignore + if (closed.compareAndSet(false, true)) { + if (cfg.getBus() == null) { + return; } - } - ClientLifeCycleManager mgr = cfg.getBus().getExtension(ClientLifeCycleManager.class); - if (null != mgr) { - mgr.clientDestroyed(new FrontendClientAdapter(getConfiguration())); - } - - if (cfg.getConduitSelector() instanceof Closeable) { - try { - ((Closeable)cfg.getConduitSelector()).close(); - } catch (IOException e) { - //ignore, we're destroying anyway + for (Closeable c : cfg.getEndpoint().getCleanupHooks()) { + try { + c.close(); + } catch (IOException e) { + //ignore + } } - } else { - cfg.getConduit().close(); + ClientLifeCycleManager mgr = cfg.getBus().getExtension(ClientLifeCycleManager.class); + if (null != mgr) { + mgr.clientDestroyed(new FrontendClientAdapter(getConfiguration())); + } + + if (cfg.getConduitSelector() instanceof Closeable) { + try { + ((Closeable)cfg.getConduitSelector()).close(); + } catch (IOException e) { + //ignore, we're destroying anyway + } + } else { + cfg.getConduit().close(); + } + state.reset(); + state = null; + cfg = null; } - state.reset(); - state = null; - cfg = null; + closed = null; } private void possiblyAddHeader(String name, String value) { @@ -872,7 +876,15 @@ public abstract class AbstractClient implements Client { outMessage.put(Message.PROCESS_ONEWAY_RESPONSE, true); } } - + private void checkClosed() { + if (closed.get()) { + throw new IllegalStateException("Client is closed"); + } + } + protected void finalize() throws Throwable { + super.finalize(); + close(); + } protected Message createMessage(Object body, String httpMethod, MultivaluedMap headers, @@ -880,6 +892,7 @@ public abstract class AbstractClient implements Client { Exchange exchange, Map invocationContext, boolean proxy) { + checkClosed(); Message m = cfg.getConduitSelector().getEndpoint().getBinding().createMessage(); m.put(Message.REQUESTOR_ROLE, Boolean.TRUE); m.put(Message.INBOUND_MESSAGE, Boolean.FALSE);