From 9368489182915e0440e34580e676864326792d16 Mon Sep 17 00:00:00 2001 From: Ryan Baxter Date: Wed, 7 Feb 2018 14:34:58 -0500 Subject: [PATCH 1/9] Removing close call from exception --- .../ClientHttpResponseStatusCodeException.java | 12 +++++------- .../loadbalancer/RetryLoadBalancerInterceptor.java | 5 ++++- .../ClientHttpResponseStatusCodeExceptionTest.java | 4 ++-- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/ClientHttpResponseStatusCodeException.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/ClientHttpResponseStatusCodeException.java index 0d7dd299..eaf74882 100644 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/ClientHttpResponseStatusCodeException.java +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/ClientHttpResponseStatusCodeException.java @@ -21,7 +21,6 @@ import java.io.InputStream; import org.springframework.http.HttpHeaders; import org.springframework.http.client.AbstractClientHttpResponse; import org.springframework.http.client.ClientHttpResponse; -import org.springframework.util.StreamUtils; /** * {@link RetryableStatusCodeException} that captures a {@link ClientHttpResponse} @@ -35,12 +34,11 @@ public class ClientHttpResponseStatusCodeException extends RetryableStatusCodeEx * Constructor * @param serviceId The service id * @param response The response object - * @throws IOException Thrown if the {@link ClientHttpResponse} body cannot be retrieved + * @throws IOException Thrown if the {@link ClientHttpResponse} response code cant be retrieved */ - public ClientHttpResponseStatusCodeException(String serviceId, ClientHttpResponse response) throws IOException { + public ClientHttpResponseStatusCodeException(String serviceId, ClientHttpResponse response, byte[] body) throws IOException { super(serviceId, response.getRawStatusCode(), response, null); - this.response = new ClientHttpResponseWrapper(response); - response.close(); + this.response = new ClientHttpResponseWrapper(response, body); } @Override @@ -53,9 +51,9 @@ public class ClientHttpResponseStatusCodeException extends RetryableStatusCodeEx private ClientHttpResponse response; private byte[] body; - public ClientHttpResponseWrapper(ClientHttpResponse response) throws IOException { + public ClientHttpResponseWrapper(ClientHttpResponse response, byte[] body) { this.response = response; - this.body = StreamUtils.copyToByteArray(response.getBody()); + this.body = body; } @Override diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/RetryLoadBalancerInterceptor.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/RetryLoadBalancerInterceptor.java index c2616653..409e94a6 100644 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/RetryLoadBalancerInterceptor.java +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/RetryLoadBalancerInterceptor.java @@ -32,6 +32,7 @@ import org.springframework.retry.backoff.NoBackOffPolicy; import org.springframework.retry.policy.NeverRetryPolicy; import org.springframework.retry.support.RetryTemplate; import org.springframework.util.Assert; +import org.springframework.util.StreamUtils; /** * @author Ryan Baxter @@ -131,7 +132,9 @@ public class RetryLoadBalancerInterceptor implements ClientHttpRequestIntercepto requestFactory.createRequest(request, body, execution)); int statusCode = response.getRawStatusCode(); if (retryPolicy != null && retryPolicy.retryableStatusCode(statusCode)) { - throw new ClientHttpResponseStatusCodeException(serviceName, response); + byte[] body = StreamUtils.copyToByteArray(response.getBody()); + response.close(); + throw new ClientHttpResponseStatusCodeException(serviceName, response, body); } return response; } diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/loadbalancer/ClientHttpResponseStatusCodeExceptionTest.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/loadbalancer/ClientHttpResponseStatusCodeExceptionTest.java index 9358e7ea..61155168 100644 --- a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/loadbalancer/ClientHttpResponseStatusCodeExceptionTest.java +++ b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/loadbalancer/ClientHttpResponseStatusCodeExceptionTest.java @@ -25,8 +25,8 @@ public class ClientHttpResponseStatusCodeExceptionTest { public void testCreation() throws Exception { MyClientHttpResponse response = new MyClientHttpResponse(); assertFalse(response.isClosed()); - ClientHttpResponseStatusCodeException exp = new ClientHttpResponseStatusCodeException("service", response); - assertTrue(response.isClosed()); + ClientHttpResponseStatusCodeException exp = new ClientHttpResponseStatusCodeException("service", + response, response.getStatusText().getBytes()); ClientHttpResponse expResponse = exp.getResponse(); assertEquals(response.getRawStatusCode(), expResponse.getRawStatusCode()); assertEquals(response.getStatusText(), expResponse.getStatusText()); From 009e81ba9fc142617df5300b64f67b36272b6d44 Mon Sep 17 00:00:00 2001 From: Ryan Baxter Date: Wed, 7 Feb 2018 16:02:33 -0500 Subject: [PATCH 2/9] bumping spring cloud build version --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 5a6c36b7..e3bd1f38 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ org.springframework.cloud spring-cloud-build - 1.3.6.RELEASE + 1.3.8.RELEASE From 7db219830d1b8c4142cc9c1a20903cd7573042e6 Mon Sep 17 00:00:00 2001 From: Ryan Baxter Date: Wed, 7 Feb 2018 16:04:45 -0500 Subject: [PATCH 3/9] bumping spring-cloud-dependencies-parent --- spring-cloud-commons-dependencies/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-cloud-commons-dependencies/pom.xml b/spring-cloud-commons-dependencies/pom.xml index 0c4a765d..cd11c350 100644 --- a/spring-cloud-commons-dependencies/pom.xml +++ b/spring-cloud-commons-dependencies/pom.xml @@ -5,7 +5,7 @@ spring-cloud-dependencies-parent org.springframework.cloud - 1.3.6.RELEASE + 1.3.8.RELEASE spring-cloud-commons-dependencies From 5d6db3e4388974649b3d5c3c38bab335695f6514 Mon Sep 17 00:00:00 2001 From: Ryan Baxter Date: Wed, 7 Feb 2018 16:41:39 -0500 Subject: [PATCH 4/9] using snapshot --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index e3bd1f38..9a0c5fba 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ org.springframework.cloud spring-cloud-build - 1.3.8.RELEASE + 1.3.8.BUILD-SNAPSHOT From 9f9f2857c062eec14eb8fdeed184c576c2ba2335 Mon Sep 17 00:00:00 2001 From: Ryan Baxter Date: Wed, 7 Feb 2018 16:42:25 -0500 Subject: [PATCH 5/9] using snapshot --- spring-cloud-commons-dependencies/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-cloud-commons-dependencies/pom.xml b/spring-cloud-commons-dependencies/pom.xml index cd11c350..56c3ab20 100644 --- a/spring-cloud-commons-dependencies/pom.xml +++ b/spring-cloud-commons-dependencies/pom.xml @@ -5,7 +5,7 @@ spring-cloud-dependencies-parent org.springframework.cloud - 1.3.8.RELEASE + 1.3.8.BUILD-SNAPSHOT spring-cloud-commons-dependencies From cefd46c33d9e54c70219653fa168c815de4f7dc9 Mon Sep 17 00:00:00 2001 From: buildmaster Date: Thu, 8 Feb 2018 01:21:37 +0000 Subject: [PATCH 6/9] Update SNAPSHOT to 1.3.2.RELEASE --- docs/pom.xml | 2 +- pom.xml | 4 ++-- spring-cloud-commons-dependencies/pom.xml | 4 ++-- spring-cloud-commons/pom.xml | 2 +- spring-cloud-context/pom.xml | 2 +- spring-cloud-starter/pom.xml | 2 +- 6 files changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/pom.xml b/docs/pom.xml index c47cb511..cf641419 100644 --- a/docs/pom.xml +++ b/docs/pom.xml @@ -6,7 +6,7 @@ org.springframework.cloud spring-cloud-commons-parent - 1.3.2.BUILD-SNAPSHOT + 1.3.2.RELEASE pom Spring Cloud Commons Docs diff --git a/pom.xml b/pom.xml index 9a0c5fba..f3841fb5 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 org.springframework.cloud spring-cloud-commons-parent - 1.3.2.BUILD-SNAPSHOT + 1.3.2.RELEASE pom Spring Cloud Commons Parent Spring Cloud Commons Parent @@ -11,7 +11,7 @@ org.springframework.cloud spring-cloud-build - 1.3.8.BUILD-SNAPSHOT + 1.3.8.RELEASE diff --git a/spring-cloud-commons-dependencies/pom.xml b/spring-cloud-commons-dependencies/pom.xml index 56c3ab20..f6ae957f 100644 --- a/spring-cloud-commons-dependencies/pom.xml +++ b/spring-cloud-commons-dependencies/pom.xml @@ -5,11 +5,11 @@ spring-cloud-dependencies-parent org.springframework.cloud - 1.3.8.BUILD-SNAPSHOT + 1.3.8.RELEASE spring-cloud-commons-dependencies - 1.3.2.BUILD-SNAPSHOT + 1.3.2.RELEASE pom spring-cloud-commons-dependencies Spring Cloud Commons Dependencies diff --git a/spring-cloud-commons/pom.xml b/spring-cloud-commons/pom.xml index e4c87ae5..7fca6fd6 100644 --- a/spring-cloud-commons/pom.xml +++ b/spring-cloud-commons/pom.xml @@ -6,7 +6,7 @@ org.springframework.cloud spring-cloud-commons-parent - 1.3.2.BUILD-SNAPSHOT + 1.3.2.RELEASE .. spring-cloud-commons diff --git a/spring-cloud-context/pom.xml b/spring-cloud-context/pom.xml index fa74e07c..af9e7149 100644 --- a/spring-cloud-context/pom.xml +++ b/spring-cloud-context/pom.xml @@ -6,7 +6,7 @@ org.springframework.cloud spring-cloud-commons-parent - 1.3.2.BUILD-SNAPSHOT + 1.3.2.RELEASE .. spring-cloud-context diff --git a/spring-cloud-starter/pom.xml b/spring-cloud-starter/pom.xml index 49f3801a..31e47e9f 100644 --- a/spring-cloud-starter/pom.xml +++ b/spring-cloud-starter/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-commons-parent - 1.3.2.BUILD-SNAPSHOT + 1.3.2.RELEASE spring-cloud-starter spring-cloud-starter From 8055d1a6bb3e42172bcde914909f0087ab3a2f9f Mon Sep 17 00:00:00 2001 From: buildmaster Date: Thu, 8 Feb 2018 01:22:38 +0000 Subject: [PATCH 7/9] Going back to snapshots --- docs/pom.xml | 2 +- pom.xml | 4 ++-- spring-cloud-commons-dependencies/pom.xml | 4 ++-- spring-cloud-commons/pom.xml | 2 +- spring-cloud-context/pom.xml | 2 +- spring-cloud-starter/pom.xml | 2 +- 6 files changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/pom.xml b/docs/pom.xml index cf641419..c47cb511 100644 --- a/docs/pom.xml +++ b/docs/pom.xml @@ -6,7 +6,7 @@ org.springframework.cloud spring-cloud-commons-parent - 1.3.2.RELEASE + 1.3.2.BUILD-SNAPSHOT pom Spring Cloud Commons Docs diff --git a/pom.xml b/pom.xml index f3841fb5..9a0c5fba 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 org.springframework.cloud spring-cloud-commons-parent - 1.3.2.RELEASE + 1.3.2.BUILD-SNAPSHOT pom Spring Cloud Commons Parent Spring Cloud Commons Parent @@ -11,7 +11,7 @@ org.springframework.cloud spring-cloud-build - 1.3.8.RELEASE + 1.3.8.BUILD-SNAPSHOT diff --git a/spring-cloud-commons-dependencies/pom.xml b/spring-cloud-commons-dependencies/pom.xml index f6ae957f..56c3ab20 100644 --- a/spring-cloud-commons-dependencies/pom.xml +++ b/spring-cloud-commons-dependencies/pom.xml @@ -5,11 +5,11 @@ spring-cloud-dependencies-parent org.springframework.cloud - 1.3.8.RELEASE + 1.3.8.BUILD-SNAPSHOT spring-cloud-commons-dependencies - 1.3.2.RELEASE + 1.3.2.BUILD-SNAPSHOT pom spring-cloud-commons-dependencies Spring Cloud Commons Dependencies diff --git a/spring-cloud-commons/pom.xml b/spring-cloud-commons/pom.xml index 7fca6fd6..e4c87ae5 100644 --- a/spring-cloud-commons/pom.xml +++ b/spring-cloud-commons/pom.xml @@ -6,7 +6,7 @@ org.springframework.cloud spring-cloud-commons-parent - 1.3.2.RELEASE + 1.3.2.BUILD-SNAPSHOT .. spring-cloud-commons diff --git a/spring-cloud-context/pom.xml b/spring-cloud-context/pom.xml index af9e7149..fa74e07c 100644 --- a/spring-cloud-context/pom.xml +++ b/spring-cloud-context/pom.xml @@ -6,7 +6,7 @@ org.springframework.cloud spring-cloud-commons-parent - 1.3.2.RELEASE + 1.3.2.BUILD-SNAPSHOT .. spring-cloud-context diff --git a/spring-cloud-starter/pom.xml b/spring-cloud-starter/pom.xml index 31e47e9f..49f3801a 100644 --- a/spring-cloud-starter/pom.xml +++ b/spring-cloud-starter/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-commons-parent - 1.3.2.RELEASE + 1.3.2.BUILD-SNAPSHOT spring-cloud-starter spring-cloud-starter From ce7d9e30923e208d2a20404631ca7babbf013839 Mon Sep 17 00:00:00 2001 From: buildmaster Date: Thu, 8 Feb 2018 01:22:39 +0000 Subject: [PATCH 8/9] Bumping versions to 1.3.3.BUILD-SNAPSHOT after release --- docs/pom.xml | 2 +- pom.xml | 4 ++-- spring-cloud-commons-dependencies/pom.xml | 2 +- spring-cloud-commons/pom.xml | 2 +- spring-cloud-context/pom.xml | 2 +- spring-cloud-starter/pom.xml | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/pom.xml b/docs/pom.xml index c47cb511..66f5d56a 100644 --- a/docs/pom.xml +++ b/docs/pom.xml @@ -6,7 +6,7 @@ org.springframework.cloud spring-cloud-commons-parent - 1.3.2.BUILD-SNAPSHOT + 1.3.3.BUILD-SNAPSHOT pom Spring Cloud Commons Docs diff --git a/pom.xml b/pom.xml index 9a0c5fba..14c8c492 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 org.springframework.cloud spring-cloud-commons-parent - 1.3.2.BUILD-SNAPSHOT + 1.3.3.BUILD-SNAPSHOT pom Spring Cloud Commons Parent Spring Cloud Commons Parent @@ -11,7 +11,7 @@ org.springframework.cloud spring-cloud-build - 1.3.8.BUILD-SNAPSHOT + 1.3.8.RELEASE diff --git a/spring-cloud-commons-dependencies/pom.xml b/spring-cloud-commons-dependencies/pom.xml index 56c3ab20..053df5e4 100644 --- a/spring-cloud-commons-dependencies/pom.xml +++ b/spring-cloud-commons-dependencies/pom.xml @@ -9,7 +9,7 @@ spring-cloud-commons-dependencies - 1.3.2.BUILD-SNAPSHOT + 1.3.3.BUILD-SNAPSHOT pom spring-cloud-commons-dependencies Spring Cloud Commons Dependencies diff --git a/spring-cloud-commons/pom.xml b/spring-cloud-commons/pom.xml index e4c87ae5..ba11f0d7 100644 --- a/spring-cloud-commons/pom.xml +++ b/spring-cloud-commons/pom.xml @@ -6,7 +6,7 @@ org.springframework.cloud spring-cloud-commons-parent - 1.3.2.BUILD-SNAPSHOT + 1.3.3.BUILD-SNAPSHOT .. spring-cloud-commons diff --git a/spring-cloud-context/pom.xml b/spring-cloud-context/pom.xml index fa74e07c..1cc99e58 100644 --- a/spring-cloud-context/pom.xml +++ b/spring-cloud-context/pom.xml @@ -6,7 +6,7 @@ org.springframework.cloud spring-cloud-commons-parent - 1.3.2.BUILD-SNAPSHOT + 1.3.3.BUILD-SNAPSHOT .. spring-cloud-context diff --git a/spring-cloud-starter/pom.xml b/spring-cloud-starter/pom.xml index 49f3801a..4da17ff3 100644 --- a/spring-cloud-starter/pom.xml +++ b/spring-cloud-starter/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-commons-parent - 1.3.2.BUILD-SNAPSHOT + 1.3.3.BUILD-SNAPSHOT spring-cloud-starter spring-cloud-starter From 5802c0e2be792738d86509e3d2c93dcd8b997d4d Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Thu, 8 Mar 2018 11:55:49 +0000 Subject: [PATCH 9/9] Actually close parent context in RestartEndpoint There could be consequences for apps that have more complex context hierarchies, but RestartEndpoint doesn't work well at all for those apps anyway, so we are probably safe to just make changes in the endpoint itself. Fixes gh-333 --- .gitignore | 3 ++- .../context/restart/RestartEndpoint.java | 20 ++++++++++++++++++- .../restart/RestartIntegrationTests.java | 20 ++++++++++++++----- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 86f0d9af..6ee2be21 100644 --- a/.gitignore +++ b/.gitignore @@ -11,10 +11,11 @@ _site/ .project .settings .springBeans +.sts4-cache/ .DS_Store *.sw* *.iml .idea .factorypath /spring-cloud-release-tools*.jar -antrun \ No newline at end of file +antrun diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/context/restart/RestartEndpoint.java b/spring-cloud-context/src/main/java/org/springframework/cloud/context/restart/RestartEndpoint.java index 4454c788..7ac31c36 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/context/restart/RestartEndpoint.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/context/restart/RestartEndpoint.java @@ -16,12 +16,17 @@ package org.springframework.cloud.context.restart; +import java.io.Closeable; +import java.io.IOException; + import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; + import org.springframework.boot.SpringApplication; import org.springframework.boot.actuate.endpoint.AbstractEndpoint; import org.springframework.boot.context.event.ApplicationPreparedEvent; import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationListener; import org.springframework.context.ConfigurableApplicationContext; @@ -155,7 +160,7 @@ public class RestartEndpoint extends AbstractEndpoint this.integrationShutdown.stop(this.timeout); } this.application.setEnvironment(this.context.getEnvironment()); - this.context.close(); + close(); // If running in a webapp then the context classloader is probably going to // die so we need to revert to a safe place before starting again overrideClassLoaderForRestart(); @@ -164,6 +169,19 @@ public class RestartEndpoint extends AbstractEndpoint return this.context; } + private void close() { + ApplicationContext context = this.context; + while (context instanceof Closeable) { + try { + ((Closeable) context).close(); + } + catch (IOException e) { + logger.error("Cannot close context: " + context.getId(), e); + } + context = context.getParent(); + } + } + @ManagedAttribute public boolean isRunning() { if (this.context != null) { diff --git a/spring-cloud-context/src/test/java/org/springframework/cloud/context/restart/RestartIntegrationTests.java b/spring-cloud-context/src/test/java/org/springframework/cloud/context/restart/RestartIntegrationTests.java index 139d584b..b069c8fe 100644 --- a/spring-cloud-context/src/test/java/org/springframework/cloud/context/restart/RestartIntegrationTests.java +++ b/spring-cloud-context/src/test/java/org/springframework/cloud/context/restart/RestartIntegrationTests.java @@ -16,16 +16,19 @@ package org.springframework.cloud.context.restart; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNotSame; -import static org.junit.Assert.assertNull; - import org.junit.After; import org.junit.Test; + import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Configuration; +import org.springframework.context.support.LiveBeansView; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertNull; public class RestartIntegrationTests { @@ -41,7 +44,10 @@ public class RestartIntegrationTests { @Test public void testRestartTwice() throws Exception { - context = SpringApplication.run(TestConfiguration.class, "--endpoints.restart.enabled=true", "--server.port=0"); + context = SpringApplication.run(TestConfiguration.class, + "--endpoints.restart.enabled=true", "--server.port=0", + "--spring.liveBeansView.mbeanDomain=livebeans"); + RestartEndpoint endpoint = context.getBean(RestartEndpoint.class); assertNotNull(context.getParent()); assertNull(context.getParent().getParent()); @@ -59,6 +65,10 @@ public class RestartIntegrationTests { assertNotNull(context.getParent()); assertNull(context.getParent().getParent()); + LiveBeansView beans = new LiveBeansView(); + String json = beans.getSnapshotAsJson(); + assertThat(json).containsOnlyOnce("parent\": \"bootstrap"); + assertThat(json).containsOnlyOnce("parent\": null"); } public static void main(String[] args) {