This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch cc
in repository https://gitbox.apache.org/repos/asf/camel.git

commit f44c6c029fdf72e46f1dbfd0a2900bd067d4efb9
Author: Claus Ibsen <claus.ib...@gmail.com>
AuthorDate: Wed Nov 29 12:01:44 2023 +0100

    CAMEL-20164: camel-timer: Expose more stats of its running state.
---
 .../camel/component/timer/TimerConsumer.java       |  15 ++-
 .../camel/impl/console/ConsumerDevConsole.java     | 125 ++++++++++++++++++++-
 2 files changed, 137 insertions(+), 3 deletions(-)

diff --git 
a/components/camel-timer/src/main/java/org/apache/camel/component/timer/TimerConsumer.java
 
b/components/camel-timer/src/main/java/org/apache/camel/component/timer/TimerConsumer.java
index 355af0be804..396909eb1ad 100644
--- 
a/components/camel-timer/src/main/java/org/apache/camel/component/timer/TimerConsumer.java
+++ 
b/components/camel-timer/src/main/java/org/apache/camel/component/timer/TimerConsumer.java
@@ -29,6 +29,8 @@ import org.apache.camel.LoggingLevel;
 import org.apache.camel.Processor;
 import org.apache.camel.StartupListener;
 import org.apache.camel.Suspendable;
+import org.apache.camel.api.management.ManagedAttribute;
+import org.apache.camel.api.management.ManagedResource;
 import org.apache.camel.support.DefaultConsumer;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -36,6 +38,7 @@ import org.slf4j.LoggerFactory;
 /**
  * The timer consumer.
  */
+@ManagedResource(description = "Managed TimerConsumer")
 public class TimerConsumer extends DefaultConsumer implements StartupListener, 
Suspendable {
 
     private static final Logger LOG = 
LoggerFactory.getLogger(TimerConsumer.class);
@@ -59,6 +62,7 @@ public class TimerConsumer extends DefaultConsumer implements 
StartupListener, S
     /**
      * Total number of polls run
      */
+    @ManagedAttribute(description = "Total number of polls run")
     public long getCounter() {
         return counter.get();
     }
@@ -66,32 +70,39 @@ public class TimerConsumer extends DefaultConsumer 
implements StartupListener, S
     /**
      * Whether polling is currently in progress
      */
+    @ManagedAttribute(description = "Whether polling is currently in progress")
     public boolean isPolling() {
         return polling;
     }
 
+    @ManagedAttribute(description = "Timer Name")
     public String getTimerName() {
         return getEndpoint().getTimerName();
     }
 
+    @ManagedAttribute(description = "Timer FixedRate")
     public boolean isFixedRate() {
         return getEndpoint().isFixedRate();
     }
 
+    @ManagedAttribute(description = "Timer Delay")
     public long getDelay() {
         return getEndpoint().getDelay();
     }
 
+    @ManagedAttribute(description = "Timer Period")
     public long getPeriod() {
         return getEndpoint().getPeriod();
     }
 
+    @ManagedAttribute(description = "Repeat Count")
     public long getRepeatCount() {
         return getEndpoint().getRepeatCount();
     }
 
-    public LoggingLevel getRunLoggingLevel() {
-        return getEndpoint().getRunLoggingLevel();
+    @ManagedAttribute(description = "The consumer logs a start/complete log 
line when it polls. This option allows you to configure the logging level for 
that.")
+    public String getRunLoggingLevel() {
+        return getEndpoint().getRunLoggingLevel().name();
     }
 
     @Override
diff --git 
a/core/camel-console/src/main/java/org/apache/camel/impl/console/ConsumerDevConsole.java
 
b/core/camel-console/src/main/java/org/apache/camel/impl/console/ConsumerDevConsole.java
index 401e608f63b..8d94953b527 100644
--- 
a/core/camel-console/src/main/java/org/apache/camel/impl/console/ConsumerDevConsole.java
+++ 
b/core/camel-console/src/main/java/org/apache/camel/impl/console/ConsumerDevConsole.java
@@ -16,8 +16,14 @@
  */
 package org.apache.camel.impl.console;
 
+import java.lang.management.ManagementFactory;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Map;
 
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+
 import org.apache.camel.Route;
 import org.apache.camel.api.management.ManagedCamelContext;
 import org.apache.camel.api.management.mbean.ManagedConsumerMBean;
@@ -75,6 +81,43 @@ public class ConsumerDevConsole extends AbstractDevConsole {
                                 mpc.getBackoffCounter(), 
mpc.getBackoffMultiplier(), mpc.getBackoffErrorThreshold(),
                                 mpc.getBackoffIdleThreshold()));
                     }
+                    if ("TimerConsumer".equals(mc.getServiceType())) {
+                        // need to use JMX to gather details for camel-timer 
consumer
+                        try {
+                            MBeanServer ms = 
ManagementFactory.getPlatformMBeanServer();
+                            ObjectName on = 
getCamelContext().getManagementStrategy().getManagementObjectNameStrategy()
+                                    
.getObjectNameForConsumer(getCamelContext(),
+                                            route.getConsumer());
+                            if (ms.isRegistered(on)) {
+                                String timerName = (String) 
ms.getAttribute(on, "TimerName");
+                                Long counter = (Long) ms.getAttribute(on, 
"Counter");
+                                Boolean polling = (Boolean) 
ms.getAttribute(on, "Polling");
+                                Boolean fixedRate = (Boolean) 
ms.getAttribute(on, "FixedRate");
+                                Long delay = (Long) ms.getAttribute(on, 
"Delay");
+                                Long period = (Long) ms.getAttribute(on, 
"Period");
+                                Long repeatCount = (Long) ms.getAttribute(on, 
"RepeatCount");
+                                String runLoggingLevel = (String) 
ms.getAttribute(on, "RunLoggingLevel");
+
+                                sb.append(String.format("\n    Timer Name: 
%s", timerName));
+                                sb.append(String.format("\n    Polling: %s", 
polling));
+                                sb.append(String.format("\n    Fixed Rate: 
%s", fixedRate));
+                                if (delay != null) {
+                                    sb.append(String.format("\n    Delay: %s", 
delay));
+                                }
+                                if (period != null) {
+                                    sb.append(String.format("\n    Period: 
%s", period));
+                                }
+                                if (repeatCount != null) {
+                                    sb.append(String.format("\n    Repeat 
Count: %s", repeatCount));
+                                }
+                                sb.append(String.format("\n    Running Logging 
Level: %s", runLoggingLevel));
+                                sb.append(String.format("\n    Counter(total: 
%s)", counter));
+
+                            }
+                        } catch (Exception e) {
+                            // ignore
+                        }
+                    }
                 }
             }
         }
@@ -84,7 +127,87 @@ public class ConsumerDevConsole extends AbstractDevConsole {
 
     @Override
     protected JsonObject doCallJson(Map<String, Object> options) {
-        JsonObject root = new JsonObject();
+        final JsonObject root = new JsonObject();
+        final List<JsonObject> list = new ArrayList<>();
+        root.put("consumers", list);
+
+        ManagedCamelContext mcc = 
getCamelContext().getCamelContextExtension().getContextPlugin(ManagedCamelContext.class);
+        if (mcc != null) {
+            for (Route route : getCamelContext().getRoutes()) {
+                String id = route.getId();
+                ManagedConsumerMBean mc = mcc.getManagedConsumer(id);
+                if (mc != null) {
+                    JsonObject jo = new JsonObject();
+                    Integer inflight = mc.getInflightExchanges();
+                    if (inflight == null) {
+                        inflight = 0;
+                    }
+
+                    jo.put("id", id);
+                    jo.put("from", mc.getEndpointUri());
+                    jo.put("state", mc.getState());
+                    jo.put("class", mc.getServiceType());
+                    jo.put("inflight", inflight);
+                    if (mcc instanceof ManagedSchedulePollConsumerMBean mpc) {
+                        jo.put("polling", mpc.isPolling());
+                        jo.put("firstPollDone", mpc.isFirstPollDone());
+                        jo.put("schedulerStarted", mpc.isSchedulerStarted());
+                        jo.put("schedulerClass", mpc.getSchedulerClassName());
+                        jo.put("repeatCount", mpc.getRepeatCount());
+                        jo.put("fixedDelay", mpc.isUseFixedDelay());
+                        jo.put("initialDelay", mpc.getInitialDelay());
+                        jo.put("delay", mpc.getDelay());
+                        jo.put("timeUnit", mpc.getTimeUnit());
+                        jo.put("greedy", mpc.isGreedy());
+                        jo.put("runningLoggingLevel", 
mpc.getRunningLoggingLevel());
+                        jo.put("totalCounter", mpc.getCounter());
+                        jo.put("errorCounter", mpc.getErrorCounter());
+                        jo.put("successCounter", mpc.getSuccessCounter());
+                        jo.put("backoffCounter", mpc.getBackoffCounter());
+                        jo.put("backoffMultiplier", 
mpc.getBackoffMultiplier());
+                        jo.put("backoffErrorThreshold", 
mpc.getBackoffErrorThreshold());
+                        jo.put("backoffIdleThreshold", 
mpc.getBackoffIdleThreshold());
+                    }
+                    if ("TimerConsumer".equals(mc.getServiceType())) {
+                        // need to use JMX to gather details for camel-timer 
consumer
+                        try {
+                            MBeanServer ms = 
ManagementFactory.getPlatformMBeanServer();
+                            ObjectName on = 
getCamelContext().getManagementStrategy().getManagementObjectNameStrategy()
+                                    
.getObjectNameForConsumer(getCamelContext(),
+                                            route.getConsumer());
+                            if (ms.isRegistered(on)) {
+                                String timerName = (String) 
ms.getAttribute(on, "TimerName");
+                                Long counter = (Long) ms.getAttribute(on, 
"Counter");
+                                Boolean polling = (Boolean) 
ms.getAttribute(on, "Polling");
+                                Boolean fixedRate = (Boolean) 
ms.getAttribute(on, "FixedRate");
+                                Long delay = (Long) ms.getAttribute(on, 
"Delay");
+                                Long period = (Long) ms.getAttribute(on, 
"Period");
+                                Long repeatCount = (Long) ms.getAttribute(on, 
"RepeatCount");
+                                String runLoggingLevel = (String) 
ms.getAttribute(on, "RunLoggingLevel");
+
+                                jo.put("timerName", timerName);
+                                jo.put("polling", polling);
+                                jo.put("fixedRate", fixedRate);
+                                if (delay != null) {
+                                    jo.put("delay", delay);
+                                }
+                                if (period != null) {
+                                    jo.put("period", period);
+                                }
+                                if (repeatCount != null) {
+                                    jo.put("repeatCount", repeatCount);
+                                }
+                                jo.put("runningLoggingLevel", runLoggingLevel);
+                                jo.put("totalCounter", counter);
+                            }
+                        } catch (Exception e) {
+                            // ignore
+                        }
+                    }
+                    list.add(jo);
+                }
+            }
+        }
 
         return root;
     }

Reply via email to