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 2010/06/08 15:16:06 UTC

svn commit: r952644 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/ main/java/org/apache/camel/impl/ main/java/org/apache/camel/management/mbean/ main/java/org/apache/camel/util/ test/java/org/apache/camel/management/

Author: davsclaus
Date: Tue Jun  8 13:16:05 2010
New Revision: 952644

URL: http://svn.apache.org/viewvc?rev=952644&view=rev
Log:
CAMEL-2628: Uptime is now logged when stopping Camel and avail in JMX as well.

Added:
    camel/trunk/camel-core/src/main/java/org/apache/camel/util/TimeUtils.java   (with props)
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedCamelContext.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedCamelContextTest.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java?rev=952644&r1=952643&r2=952644&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java Tue Jun  8 13:16:05 2010
@@ -74,6 +74,13 @@ public interface CamelContext extends Se
      */
     ServiceStatus getStatus();
 
+    /**
+     * Gets the uptime in a human readable format
+     *
+     * @return the uptime in days/hours/minutes
+     */
+    String getUptime();
+
     // Service Methods
     //-----------------------------------------------------------------------
 

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java?rev=952644&r1=952643&r2=952644&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java Tue Jun  8 13:16:05 2010
@@ -19,13 +19,13 @@ package org.apache.camel.impl;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.Date;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.TreeMap;
-import java.util.concurrent.Callable;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicInteger;
@@ -97,6 +97,7 @@ import org.apache.camel.util.ObjectHelpe
 import org.apache.camel.util.ReflectionInjector;
 import org.apache.camel.util.ServiceHelper;
 import org.apache.camel.util.StopWatch;
+import org.apache.camel.util.TimeUtils;
 import org.apache.camel.util.URISupport;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -163,6 +164,7 @@ public class DefaultCamelContext extends
     private ShutdownRunningTask shutdownRunningTask = ShutdownRunningTask.CompleteCurrentTaskOnly;
     private ExecutorServiceStrategy executorServiceStrategy = new DefaultExecutorServiceStrategy(this);
     private final StopWatch stopWatch = new StopWatch(false);
+    private Date startDate;
 
     public DefaultCamelContext() {
         super();
@@ -899,6 +901,15 @@ public class DefaultCamelContext extends
         return producerServicePool;
     }
 
+    public String getUptime() {
+        // compute and log uptime
+        if (startDate == null) {
+            return "not started";
+        }
+        long delta = new Date().getTime() - startDate.getTime();
+        return TimeUtils.printDuration(delta);
+    }
+
     public void start() throws Exception {
         boolean doNotStart = !firstStartDone && !isAutoStartup();
         firstStartDone = true;
@@ -1030,6 +1041,7 @@ public class DefaultCamelContext extends
     // -----------------------------------------------------------------------
 
     protected synchronized void doStart() throws Exception {
+        startDate = new Date();
         stopWatch.restart();
         LOG.info("Apache Camel " + getVersion() + " (CamelContext: " + getName() + ") is starting");
 
@@ -1153,6 +1165,10 @@ public class DefaultCamelContext extends
         // shutdown management as the last one
         shutdownServices(managementStrategy);
 
+        LOG.info("Uptime: " + getUptime());
+        // and clear start date
+        startDate = null;
+
         LOG.info("Apache Camel " + getVersion() + " (CamelContext: " + getName() + ") is shutdown in " + stopWatch.stop() + " millis");
     }
 

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedCamelContext.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedCamelContext.java?rev=952644&r1=952643&r2=952644&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedCamelContext.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedCamelContext.java Tue Jun  8 13:16:05 2010
@@ -69,6 +69,11 @@ public class ManagedCamelContext {
         return status.name();
     }
 
+    @ManagedAttribute(description = "Uptime")
+    public String getUptime() {
+        return context.getUptime();
+    }
+
     @ManagedAttribute(description = "Camel Properties")
     public Map<String, String> getProperties() {
         if (context.getProperties().isEmpty()) {

Added: camel/trunk/camel-core/src/main/java/org/apache/camel/util/TimeUtils.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/TimeUtils.java?rev=952644&view=auto
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/TimeUtils.java (added)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/TimeUtils.java Tue Jun  8 13:16:05 2010
@@ -0,0 +1,77 @@
+/**
+ * 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.util;
+
+import java.text.DecimalFormat;
+import java.text.DecimalFormatSymbols;
+import java.text.NumberFormat;
+import java.util.Locale;
+
+/**
+ * Time utils.
+ *
+ * @version $Revision$
+ */
+public final class TimeUtils {
+
+    private TimeUtils() {
+    }
+
+    /**
+     * Prints the duration in a human readable format as X days Y hours Z minutes etc.
+     *
+     * @param uptime the uptime in millis
+     * @return the time used for displaying on screen or in logs
+     */
+    public static String printDuration(double uptime) {
+        // Code taken from Karaf
+        // https://svn.apache.org/repos/asf/felix/trunk/karaf/shell/commands/src/main/java/org/apache/felix/karaf/shell/commands/InfoAction.java
+
+        NumberFormat fmtI = new DecimalFormat("###,###", new DecimalFormatSymbols(Locale.ENGLISH));
+        NumberFormat fmtD = new DecimalFormat("###,##0.000", new DecimalFormatSymbols(Locale.ENGLISH));
+
+        uptime /= 1000;
+        if (uptime < 60) {
+            return fmtD.format(uptime) + " seconds";
+        }
+        uptime /= 60;
+        if (uptime < 60) {
+            long minutes = (long) uptime;
+            String s = fmtI.format(minutes) + (minutes > 1 ? " minutes" : " minute");
+            return s;
+        }
+        uptime /= 60;
+        if (uptime < 24) {
+            long hours = (long) uptime;
+            long minutes = (long) ((uptime - hours) * 60);
+            String s = fmtI.format(hours) + (hours > 1 ? " hours" : " hour");
+            if (minutes != 0) {
+                s += " " + fmtI.format(minutes) + (minutes > 1 ? " minutes" : "minute");
+            }
+            return s;
+        }
+        uptime /= 24;
+        long days = (long) uptime;
+        long hours = (long) ((uptime - days) * 60);
+        String s = fmtI.format(days) + (days > 1 ? " days" : " day");
+        if (hours != 0) {
+            s += " " + fmtI.format(hours) + (hours > 1 ? " hours" : "hour");
+        }
+        return s;
+    }
+
+}

Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/util/TimeUtils.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/util/TimeUtils.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedCamelContextTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedCamelContextTest.java?rev=952644&r1=952643&r2=952644&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedCamelContextTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedCamelContextTest.java Tue Jun  8 13:16:05 2010
@@ -52,6 +52,9 @@ public class ManagedCamelContextTest ext
         String name = (String) mbeanServer.getAttribute(on, "CamelId");
         assertEquals("camel-1", name);
 
+        String uptime = (String) mbeanServer.getAttribute(on, "Uptime");
+        assertNotNull(uptime);
+
         // invoke operations
         MockEndpoint mock = getMockEndpoint("mock:result");
         mock.expectedBodiesReceived("Hello World");