You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2024/09/07 07:24:28 UTC
(camel) branch main updated: CAMEL-21185: camel-management - ShutdownStrategy should be a MBean so we can better define its JMX API. Also fix so all its attributes was exposed so you can configure timeout again. (#15471)
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new cf70d57b333 CAMEL-21185: camel-management - ShutdownStrategy should be a MBean so we can better define its JMX API. Also fix so all its attributes was exposed so you can configure timeout again. (#15471)
cf70d57b333 is described below
commit cf70d57b33345a0b82eb1d44f1c0c110874ec65c
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sat Sep 7 09:24:22 2024 +0200
CAMEL-21185: camel-management - ShutdownStrategy should be a MBean so we can better define its JMX API. Also fix so all its attributes was exposed so you can configure timeout again. (#15471)
---
.../org/apache/camel/spi/ShutdownStrategy.java | 10 ++
.../camel/impl/engine/DefaultShutdownStrategy.java | 5 +
.../mbean/ManagedShutdownStrategyMBean.java | 73 +++++++++++++
.../management/JmxManagementLifecycleStrategy.java | 6 +-
.../management/mbean/ManagedShutdownStrategy.java | 120 +++++++++++++++++++++
.../management/ManagedShutdownStrategyTest.java | 27 ++++-
.../camel/support/LoggingExceptionHandler.java | 2 +-
.../modules/ROOT/pages/graceful-shutdown.adoc | 4 +-
8 files changed, 241 insertions(+), 6 deletions(-)
diff --git a/core/camel-api/src/main/java/org/apache/camel/spi/ShutdownStrategy.java b/core/camel-api/src/main/java/org/apache/camel/spi/ShutdownStrategy.java
index 4cf0650a156..50dbbf3d024 100644
--- a/core/camel-api/src/main/java/org/apache/camel/spi/ShutdownStrategy.java
+++ b/core/camel-api/src/main/java/org/apache/camel/spi/ShutdownStrategy.java
@@ -229,9 +229,19 @@ public interface ShutdownStrategy extends StaticService {
/**
* Whether a timeout has occurred during a shutdown.
+ *
+ * @deprecated use {@link #isTimeoutOccurred()}
*/
+ @Deprecated(since = "4.8.0")
boolean hasTimeoutOccurred();
+ /**
+ * Whether a timeout has occurred during a shutdown.
+ */
+ default boolean isTimeoutOccurred() {
+ return hasTimeoutOccurred();
+ }
+
/**
* Gets the logging level used for logging shutdown activity (such as starting and stopping routes). The default
* logging level is DEBUG.
diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultShutdownStrategy.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultShutdownStrategy.java
index d2b253f1769..9b9dfef4e83 100644
--- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultShutdownStrategy.java
+++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultShutdownStrategy.java
@@ -284,6 +284,11 @@ public class DefaultShutdownStrategy extends ServiceSupport implements ShutdownS
@Override
public boolean hasTimeoutOccurred() {
+ return isTimeoutOccurred();
+ }
+
+ @Override
+ public boolean isTimeoutOccurred() {
return timeoutOccurred.get();
}
diff --git a/core/camel-management-api/src/main/java/org/apache/camel/api/management/mbean/ManagedShutdownStrategyMBean.java b/core/camel-management-api/src/main/java/org/apache/camel/api/management/mbean/ManagedShutdownStrategyMBean.java
new file mode 100644
index 00000000000..0eb51b1d22a
--- /dev/null
+++ b/core/camel-management-api/src/main/java/org/apache/camel/api/management/mbean/ManagedShutdownStrategyMBean.java
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ * http://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.apache.camel.api.management.mbean;
+
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.api.management.ManagedAttribute;
+
+public interface ManagedShutdownStrategyMBean extends ManagedServiceMBean {
+
+ @ManagedAttribute(description = "Shutdown timeout")
+ void setTimeout(long timeout);
+
+ @ManagedAttribute(description = "Shutdown timeout")
+ long getTimeout();
+
+ @ManagedAttribute(description = "Shutdown timeout time unit")
+ void setTimeUnit(TimeUnit timeUnit);
+
+ @ManagedAttribute(description = "Shutdown timeout time unit")
+ TimeUnit getTimeUnit();
+
+ @ManagedAttribute(description = "Whether Camel should try to suppress logging during shutdown and timeout was triggered, meaning forced shutdown is happening.")
+ void setSuppressLoggingOnTimeout(boolean suppressLoggingOnTimeout);
+
+ @ManagedAttribute(description = "Whether Camel should try to suppress logging during shutdown and timeout was triggered, meaning forced shutdown is happening.")
+ boolean isSuppressLoggingOnTimeout();
+
+ @ManagedAttribute(description = "Whether to force shutdown of all consumers when a timeout occurred.")
+ void setShutdownNowOnTimeout(boolean shutdownNowOnTimeout);
+
+ @ManagedAttribute(description = "Whether to force shutdown of all consumers when a timeout occurred.")
+ boolean isShutdownNowOnTimeout();
+
+ @ManagedAttribute(description = "Sets whether routes should be shutdown in reverse or the same order as they were started")
+ void setShutdownRoutesInReverseOrder(boolean shutdownRoutesInReverseOrder);
+
+ @ManagedAttribute(description = "Sets whether routes should be shutdown in reverse or the same order as they were started")
+ boolean isShutdownRoutesInReverseOrder();
+
+ @ManagedAttribute(description = "Whether to log information about the inflight Exchanges which are still running during a shutdown which didn't complete without the given timeout.")
+ void setLogInflightExchangesOnTimeout(boolean logInflightExchangesOnTimeout);
+
+ @ManagedAttribute(description = "Whether to log information about the inflight Exchanges which are still running during a shutdown which didn't complete without the given timeout.")
+ boolean isLogInflightExchangesOnTimeout();
+
+ @ManagedAttribute(description = "Whether the shutdown strategy is forcing to shutdown")
+ boolean isForceShutdown();
+
+ @ManagedAttribute(description = "Whether a timeout has occurred during a shutdown.")
+ boolean isTimeoutOccurred();
+
+ @ManagedAttribute(description = "logging level used for logging shutdown activity (such as starting and stopping routes). The default logging level is DEBUG.")
+ String getLoggingLevel();
+
+ @ManagedAttribute(description = "logging level used for logging shutdown activity (such as starting and stopping routes). The default logging level is DEBUG.")
+ void setLoggingLevel(String loggingLevel);
+
+}
diff --git a/core/camel-management/src/main/java/org/apache/camel/management/JmxManagementLifecycleStrategy.java b/core/camel-management/src/main/java/org/apache/camel/management/JmxManagementLifecycleStrategy.java
index b7f80401711..0f058a2e009 100644
--- a/core/camel-management/src/main/java/org/apache/camel/management/JmxManagementLifecycleStrategy.java
+++ b/core/camel-management/src/main/java/org/apache/camel/management/JmxManagementLifecycleStrategy.java
@@ -68,6 +68,7 @@ import org.apache.camel.management.mbean.ManagedRestRegistry;
import org.apache.camel.management.mbean.ManagedRoute;
import org.apache.camel.management.mbean.ManagedRuntimeEndpointRegistry;
import org.apache.camel.management.mbean.ManagedService;
+import org.apache.camel.management.mbean.ManagedShutdownStrategy;
import org.apache.camel.management.mbean.ManagedStreamCachingStrategy;
import org.apache.camel.management.mbean.ManagedThrottlingExceptionRoutePolicy;
import org.apache.camel.management.mbean.ManagedThrottlingInflightRoutePolicy;
@@ -104,6 +105,7 @@ import org.apache.camel.spi.ManagementStrategy;
import org.apache.camel.spi.ProducerCache;
import org.apache.camel.spi.RestRegistry;
import org.apache.camel.spi.RuntimeEndpointRegistry;
+import org.apache.camel.spi.ShutdownStrategy;
import org.apache.camel.spi.StreamCachingStrategy;
import org.apache.camel.spi.Tracer;
import org.apache.camel.spi.TransformerRegistry;
@@ -565,8 +567,10 @@ public class JmxManagementLifecycleStrategy extends ServiceSupport implements Li
answer = new ManagedRuntimeEndpointRegistry(context, runtimeEndpointRegistry);
} else if (service instanceof StreamCachingStrategy streamCachingStrategy) {
answer = new ManagedStreamCachingStrategy(context, streamCachingStrategy);
+ } else if (service instanceof ShutdownStrategy shutdownStrategy) {
+ answer = new ManagedShutdownStrategy(context, shutdownStrategy);
} else if (service instanceof EventNotifier eventNotifier)
- answer = getManagementObjectStrategy().getManagedObjectForEventNotifier(context, (EventNotifier) service);
+ answer = getManagementObjectStrategy().getManagedObjectForEventNotifier(context, eventNotifier);
else if (service instanceof TransformerRegistry transformerRegistry) {
answer = new ManagedTransformerRegistry(context, transformerRegistry);
} else if (service instanceof ValidatorRegistry validatorRegistry) {
diff --git a/core/camel-management/src/main/java/org/apache/camel/management/mbean/ManagedShutdownStrategy.java b/core/camel-management/src/main/java/org/apache/camel/management/mbean/ManagedShutdownStrategy.java
new file mode 100644
index 00000000000..3fb9b22bd10
--- /dev/null
+++ b/core/camel-management/src/main/java/org/apache/camel/management/mbean/ManagedShutdownStrategy.java
@@ -0,0 +1,120 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ * http://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.apache.camel.management.mbean;
+
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.LoggingLevel;
+import org.apache.camel.api.management.ManagedResource;
+import org.apache.camel.api.management.mbean.ManagedShutdownStrategyMBean;
+import org.apache.camel.spi.ShutdownStrategy;
+
+@ManagedResource(description = "Managed ShutdownStrategy")
+public class ManagedShutdownStrategy extends ManagedService implements ManagedShutdownStrategyMBean {
+
+ private final ShutdownStrategy strategy;
+
+ public ManagedShutdownStrategy(CamelContext context, ShutdownStrategy controller) {
+ super(context, controller);
+ this.strategy = controller;
+ }
+
+ public ShutdownStrategy getShutdownStrategy() {
+ return strategy;
+ }
+
+ @Override
+ public void setTimeout(long timeout) {
+ strategy.setTimeout(timeout);
+ }
+
+ @Override
+ public long getTimeout() {
+ return strategy.getTimeout();
+ }
+
+ @Override
+ public void setTimeUnit(TimeUnit timeUnit) {
+ strategy.setTimeUnit(timeUnit);
+ }
+
+ @Override
+ public TimeUnit getTimeUnit() {
+ return strategy.getTimeUnit();
+ }
+
+ @Override
+ public void setSuppressLoggingOnTimeout(boolean suppressLoggingOnTimeout) {
+ strategy.setSuppressLoggingOnTimeout(suppressLoggingOnTimeout);
+ }
+
+ @Override
+ public boolean isSuppressLoggingOnTimeout() {
+ return strategy.isSuppressLoggingOnTimeout();
+ }
+
+ @Override
+ public void setShutdownNowOnTimeout(boolean shutdownNowOnTimeout) {
+ strategy.setShutdownNowOnTimeout(shutdownNowOnTimeout);
+ }
+
+ @Override
+ public boolean isShutdownNowOnTimeout() {
+ return strategy.isShutdownNowOnTimeout();
+ }
+
+ @Override
+ public void setShutdownRoutesInReverseOrder(boolean shutdownRoutesInReverseOrder) {
+ strategy.setShutdownRoutesInReverseOrder(shutdownRoutesInReverseOrder);
+ }
+
+ @Override
+ public boolean isShutdownRoutesInReverseOrder() {
+ return strategy.isShutdownRoutesInReverseOrder();
+ }
+
+ @Override
+ public void setLogInflightExchangesOnTimeout(boolean logInflightExchangesOnTimeout) {
+ strategy.setLogInflightExchangesOnTimeout(logInflightExchangesOnTimeout);
+ }
+
+ @Override
+ public boolean isLogInflightExchangesOnTimeout() {
+ return strategy.isLogInflightExchangesOnTimeout();
+ }
+
+ @Override
+ public boolean isForceShutdown() {
+ return strategy.isForceShutdown();
+ }
+
+ @Override
+ public boolean isTimeoutOccurred() {
+ return strategy.isTimeoutOccurred();
+ }
+
+ @Override
+ public String getLoggingLevel() {
+ return strategy.getLoggingLevel().toString();
+ }
+
+ @Override
+ public void setLoggingLevel(String loggingLevel) {
+ strategy.setLoggingLevel(LoggingLevel.valueOf(loggingLevel));
+ }
+}
diff --git a/core/camel-management/src/test/java/org/apache/camel/management/ManagedShutdownStrategyTest.java b/core/camel-management/src/test/java/org/apache/camel/management/ManagedShutdownStrategyTest.java
index 0e265dd7d98..478df605ceb 100644
--- a/core/camel-management/src/test/java/org/apache/camel/management/ManagedShutdownStrategyTest.java
+++ b/core/camel-management/src/test/java/org/apache/camel/management/ManagedShutdownStrategyTest.java
@@ -17,6 +17,7 @@
package org.apache.camel.management;
import java.util.Locale;
+import java.util.Set;
import java.util.concurrent.TimeUnit;
import javax.management.MBeanServer;
@@ -27,7 +28,10 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.OS;
+import static org.apache.camel.management.DefaultManagementObjectNameStrategy.TYPE_SERVICE;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
@DisabledOnOs(OS.AIX)
public class ManagedShutdownStrategyTest extends ManagementTestSupport {
@@ -39,13 +43,30 @@ public class ManagedShutdownStrategyTest extends ManagementTestSupport {
MBeanServer mbeanServer = getMBeanServer();
- ObjectName on = getContextObjectName();
+ ObjectName on = getCamelObjectName(TYPE_SERVICE, "*");
- Long timeout = (Long) mbeanServer.getAttribute(on, "Timeout");
+ // number of services
+ Set<ObjectName> names = mbeanServer.queryNames(on, null);
+ ObjectName name = null;
+ for (ObjectName service : names) {
+ if (service.toString().contains("DefaultShutdownStrategy")) {
+ name = service;
+ break;
+ }
+ }
+ assertNotNull(name, "Cannot find DefaultShutdownStrategy");
+
+ Long timeout = (Long) mbeanServer.getAttribute(name, "Timeout");
assertEquals(300, timeout.longValue());
- TimeUnit unit = (TimeUnit) mbeanServer.getAttribute(on, "TimeUnit");
+ TimeUnit unit = (TimeUnit) mbeanServer.getAttribute(name, "TimeUnit");
assertEquals("seconds", unit.toString().toLowerCase(Locale.ENGLISH));
+
+ String level = (String) mbeanServer.getAttribute(name, "LoggingLevel");
+ assertEquals("DEBUG", level);
+
+ Boolean order = (Boolean) mbeanServer.getAttribute(name, "ShutdownRoutesInReverseOrder");
+ assertTrue(order);
}
@Override
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/LoggingExceptionHandler.java b/core/camel-support/src/main/java/org/apache/camel/support/LoggingExceptionHandler.java
index dd60c9e7ead..d6c5ef0ee30 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/LoggingExceptionHandler.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/LoggingExceptionHandler.java
@@ -98,7 +98,7 @@ public class LoggingExceptionHandler implements ExceptionHandler {
protected boolean isSuppressLogging() {
if (camelContext != null) {
return (camelContext.getStatus().isStopping() || camelContext.getStatus().isStopped())
- && camelContext.getShutdownStrategy().hasTimeoutOccurred()
+ && camelContext.getShutdownStrategy().isTimeoutOccurred()
&& camelContext.getShutdownStrategy().isSuppressLoggingOnTimeout();
} else {
return false;
diff --git a/docs/user-manual/modules/ROOT/pages/graceful-shutdown.adoc b/docs/user-manual/modules/ROOT/pages/graceful-shutdown.adoc
index cafa4c76302..fa1e13d247d 100644
--- a/docs/user-manual/modules/ROOT/pages/graceful-shutdown.adoc
+++ b/docs/user-manual/modules/ROOT/pages/graceful-shutdown.adoc
@@ -99,7 +99,9 @@ context.getShutdownStrategy().setLogInflightExchangesOnTimeout(false);
=== JMX managed
The `ShutdownStrategy` is JMX aware, so you can manage it from a
-JMX console. This allows to adjust the strategy at runtime, such as changing the timeout setting.
+JMX console. This allows to adjust the strategy at runtime.
+
+TIP: The timeout settings can also be changed on the `CamelContextMBean`.
== Controlling ordering of routes