-
Notifications
You must be signed in to change notification settings - Fork 242
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a shutdown event, endpoint, and listener
- Loading branch information
1 parent
78ae184
commit a8fb367
Showing
17 changed files
with
485 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
...s-tests/src/test/java/org/springframework/cloud/bus/ShutdownListenerIntegrationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Copyright 2012-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.cloud.bus; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
|
||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.testcontainers.containers.RabbitMQContainer; | ||
import org.testcontainers.junit.jupiter.Container; | ||
import org.testcontainers.junit.jupiter.Testcontainers; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.SpringBootConfiguration; | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.boot.builder.SpringApplicationBuilder; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.testcontainers.service.connection.ServiceConnection; | ||
import org.springframework.cloud.bus.event.EnvironmentChangeRemoteApplicationEvent; | ||
import org.springframework.context.ApplicationListener; | ||
import org.springframework.context.ConfigurableApplicationContext; | ||
import org.springframework.test.web.reactive.server.WebTestClient; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; | ||
|
||
/** | ||
* @author Ryan Baxter | ||
*/ | ||
@SpringBootTest(webEnvironment = RANDOM_PORT, | ||
properties = { "management.endpoints.web.exposure.include=*", | ||
"spring.cloud.stream.bindings.springCloudBusOutput.producer.errorChannelEnabled=true", | ||
"logging.level.org.springframework.cloud.bus=TRACE", "spring.cloud.bus.id=app:1" }) | ||
@Testcontainers | ||
public class ShutdownListenerIntegrationTests { | ||
|
||
private static ConfigurableApplicationContext context; | ||
|
||
@Container | ||
@ServiceConnection | ||
private static final RabbitMQContainer rabbitMQContainer = new RabbitMQContainer("rabbitmq:4.0-management"); | ||
|
||
@BeforeAll | ||
static void before() { | ||
context = new SpringApplicationBuilder(TestConfig.class) | ||
.properties("server.port=0", "spring.rabbitmq.host=" + rabbitMQContainer.getHost(), | ||
"spring.rabbitmq.port=" + rabbitMQContainer.getAmqpPort(), | ||
"management.endpoints.web.exposure.include=*", "spring.cloud.bus.id=app:2", "debug=true") | ||
.run(); | ||
} | ||
|
||
@AfterAll | ||
static void after() { | ||
if (context != null) { | ||
context.close(); | ||
} | ||
} | ||
|
||
@Test | ||
void testShutdown(@Autowired WebTestClient client) { | ||
assertThat(rabbitMQContainer.isRunning()); | ||
client.post().uri("/actuator/busshutdown/app:2").exchange().expectStatus().is2xxSuccessful(); | ||
assertThat(context.isClosed()); | ||
} | ||
|
||
@SpringBootConfiguration | ||
@EnableAutoConfiguration | ||
static class TestConfig implements ApplicationListener<EnvironmentChangeRemoteApplicationEvent> { | ||
|
||
CountDownLatch latch = new CountDownLatch(1); | ||
|
||
@Override | ||
public void onApplicationEvent(EnvironmentChangeRemoteApplicationEvent event) { | ||
latch.countDown(); | ||
} | ||
|
||
} | ||
|
||
} |
55 changes: 55 additions & 0 deletions
55
...g-cloud-bus/src/main/java/org/springframework/cloud/bus/BusShutdownAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright 2012-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.cloud.bus; | ||
|
||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.cloud.bus.endpoint.ShutdownBusEndpoint; | ||
import org.springframework.cloud.bus.event.Destination; | ||
import org.springframework.cloud.bus.event.ShutdownListener; | ||
import org.springframework.context.ApplicationEventPublisher; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** | ||
* @author Ryan Baxter | ||
*/ | ||
@Configuration(proxyBeanMethods = false) | ||
@ConditionalOnBusEnabled | ||
public class BusShutdownAutoConfiguration { | ||
|
||
@Bean | ||
@ConditionalOnProperty(value = "spring.cloud.bus.shutdown.enabled", matchIfMissing = true) | ||
@ConditionalOnMissingBean | ||
public ShutdownListener shutdownListener(ServiceMatcher serviceMatcher) { | ||
return new ShutdownListener(serviceMatcher); | ||
} | ||
|
||
@Configuration(proxyBeanMethods = false) | ||
@ConditionalOnClass(name = { "org.springframework.boot.actuate.endpoint.annotation.Endpoint" }) | ||
protected static class BusShutdownEndpointConfiguration { | ||
|
||
@Bean | ||
public ShutdownBusEndpoint shutdownBusEndpoint(ApplicationEventPublisher publisher, BusProperties bus, | ||
Destination.Factory destinationFactory) { | ||
return new ShutdownBusEndpoint(publisher, bus.getId(), destinationFactory); | ||
} | ||
|
||
} | ||
|
||
} |
48 changes: 48 additions & 0 deletions
48
...g-cloud-bus/src/main/java/org/springframework/cloud/bus/endpoint/ShutdownBusEndpoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright 2012-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.cloud.bus.endpoint; | ||
|
||
import org.springframework.boot.actuate.endpoint.annotation.Endpoint; | ||
import org.springframework.boot.actuate.endpoint.annotation.Selector; | ||
import org.springframework.boot.actuate.endpoint.annotation.WriteOperation; | ||
import org.springframework.cloud.bus.event.Destination; | ||
import org.springframework.cloud.bus.event.ShutdownRemoteApplicationEvent; | ||
import org.springframework.context.ApplicationEventPublisher; | ||
import org.springframework.util.StringUtils; | ||
|
||
/** | ||
* @author Ryan Baxter | ||
*/ | ||
@Endpoint(id = "busshutdown") | ||
public class ShutdownBusEndpoint extends AbstractBusEndpoint { | ||
|
||
public ShutdownBusEndpoint(ApplicationEventPublisher publisher, String id, Destination.Factory destinationFactory) { | ||
super(publisher, id, destinationFactory); | ||
} | ||
|
||
@WriteOperation | ||
public void busShutdownWithDestination(@Selector(match = Selector.Match.ALL_REMAINING) String[] destinations) { | ||
String destination = StringUtils.arrayToDelimitedString(destinations, ":"); | ||
publish(new ShutdownRemoteApplicationEvent(this, getInstanceId(), getDestination(destination))); | ||
} | ||
|
||
@WriteOperation | ||
public void busShutdown() { | ||
publish(new ShutdownRemoteApplicationEvent(this, getInstanceId(), getDestination(null))); | ||
} | ||
|
||
} |
65 changes: 65 additions & 0 deletions
65
spring-cloud-bus/src/main/java/org/springframework/cloud/bus/event/ShutdownListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright 2012-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.cloud.bus.event; | ||
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
|
||
import org.springframework.beans.BeansException; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.cloud.bus.ServiceMatcher; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.ApplicationContextAware; | ||
import org.springframework.context.ApplicationListener; | ||
|
||
/** | ||
* @author Ryan Baxter | ||
*/ | ||
public class ShutdownListener implements ApplicationListener<ShutdownRemoteApplicationEvent>, ApplicationContextAware { | ||
|
||
private static final Log LOG = LogFactory.getLog(ShutdownListener.class); | ||
|
||
private ApplicationContext context; | ||
|
||
private ServiceMatcher serviceMatcher; | ||
|
||
public ShutdownListener(ServiceMatcher serviceMatcher) { | ||
this.serviceMatcher = serviceMatcher; | ||
} | ||
|
||
@Override | ||
public void onApplicationEvent(ShutdownRemoteApplicationEvent event) { | ||
if (serviceMatcher.isForSelf(event)) { | ||
LOG.warn("Received remote shutdown request from " + event.getOriginService() + ". Shutting down."); | ||
shutdown(); | ||
} | ||
else { | ||
LOG.info("Shutdown not performed, the event was targeting " + event.getDestinationService()); | ||
} | ||
|
||
} | ||
|
||
protected int shutdown() { | ||
return SpringApplication.exit(context, () -> 0); | ||
} | ||
|
||
@Override | ||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { | ||
this.context = applicationContext; | ||
} | ||
|
||
} |
Oops, something went wrong.