added ActiveMQ style uptime at shutdown

Project: http://git-wip-us.apache.org/repos/asf/activemq-artemis/repo
Commit: http://git-wip-us.apache.org/repos/asf/activemq-artemis/commit/3ef184ab
Tree: http://git-wip-us.apache.org/repos/asf/activemq-artemis/tree/3ef184ab
Diff: http://git-wip-us.apache.org/repos/asf/activemq-artemis/diff/3ef184ab

Branch: refs/heads/master
Commit: 3ef184aba5dea337cf03017eca9e7cbbd3dba013
Parents: 1d91680
Author: Andy Taylor <[email protected]>
Authored: Tue Dec 22 11:38:01 2015 +0000
Committer: Clebert Suconic <[email protected]>
Committed: Wed Dec 23 10:58:44 2015 -0500

----------------------------------------------------------------------
 .../core/server/ActiveMQServerLogger.java       |  4 +-
 .../core/server/impl/ActiveMQServerImpl.java    | 27 ++++++-
 .../activemq/artemis/utils/TimeUtils.java       | 77 ++++++++++++++++++++
 3 files changed, 104 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/3ef184ab/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/ActiveMQServerLogger.java
----------------------------------------------------------------------
diff --git 
a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/ActiveMQServerLogger.java
 
b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/ActiveMQServerLogger.java
index 526abca..8b31875 100644
--- 
a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/ActiveMQServerLogger.java
+++ 
b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/ActiveMQServerLogger.java
@@ -86,8 +86,8 @@ public interface ActiveMQServerLogger extends BasicLogger {
    void serverStarted(String fullVersion, SimpleString nodeId, String 
identity);
 
    @LogMessage(level = Logger.Level.INFO)
-   @Message(id = 221002, value = "Apache ActiveMQ Artemis Message Broker 
version {0} [{1}] stopped", format = Message.Format.MESSAGE_FORMAT)
-   void serverStopped(String version, SimpleString nodeId);
+   @Message(id = 221002, value = "Apache ActiveMQ Artemis Message Broker 
version {0} [{1}] stopped, uptime {2}", format = Message.Format.MESSAGE_FORMAT)
+   void serverStopped(String version, SimpleString nodeId, String uptime);
 
    @LogMessage(level = Logger.Level.INFO)
    @Message(id = 221003, value = "Trying to deploy queue {0}", format = 
Message.Format.MESSAGE_FORMAT)

http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/3ef184ab/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/ActiveMQServerImpl.java
----------------------------------------------------------------------
diff --git 
a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/ActiveMQServerImpl.java
 
b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/ActiveMQServerImpl.java
index 8b3dae0..cc2c73d 100644
--- 
a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/ActiveMQServerImpl.java
+++ 
b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/ActiveMQServerImpl.java
@@ -26,6 +26,7 @@ import java.lang.management.ManagementFactory;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
 import java.util.ArrayList;
+import java.util.Date;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.LinkedList;
@@ -133,6 +134,7 @@ import org.apache.activemq.artemis.utils.ExecutorFactory;
 import org.apache.activemq.artemis.utils.OrderedExecutorFactory;
 import org.apache.activemq.artemis.utils.ReusableLatch;
 import org.apache.activemq.artemis.utils.SecurityFormatter;
+import org.apache.activemq.artemis.utils.TimeUtils;
 import org.apache.activemq.artemis.utils.VersionLoader;
 
 /**
@@ -266,6 +268,7 @@ public class ActiveMQServerImpl implements ActiveMQServer {
 
    private ServiceRegistry serviceRegistry;
 
+   private Date startDate;
    // Constructors
    // 
---------------------------------------------------------------------------------
 
@@ -369,6 +372,8 @@ public class ActiveMQServerImpl implements ActiveMQServer {
          return;
       }
 
+      startDate = new Date();
+
       state = SERVER_STATE.STARTING;
 
       if (haPolicy == null) {
@@ -771,10 +776,10 @@ public class ActiveMQServerImpl implements ActiveMQServer 
{
       scaledDownNodeIDs.clear();
 
       if (identity != null) {
-         ActiveMQServerLogger.LOGGER.serverStopped("identity=" + identity + 
",version=" + getVersion().getFullVersion(), tempNodeID);
+         ActiveMQServerLogger.LOGGER.serverStopped("identity=" + identity + 
",version=" + getVersion().getFullVersion(), tempNodeID, getUptime());
       }
       else {
-         
ActiveMQServerLogger.LOGGER.serverStopped(getVersion().getFullVersion(), 
tempNodeID);
+         
ActiveMQServerLogger.LOGGER.serverStopped(getVersion().getFullVersion(), 
tempNodeID, getUptime());
       }
    }
 
@@ -2075,4 +2080,22 @@ public class ActiveMQServerImpl implements 
ActiveMQServer {
          }
       }
    }
+
+   public String getUptime() {
+      long delta = getUptimeMillis();
+
+      if (delta == 0) {
+         return "not started";
+      }
+
+      return TimeUtils.printDuration(delta);
+   }
+
+   public long getUptimeMillis() {
+      if (startDate == null) {
+         return 0;
+      }
+
+      return new Date().getTime() - startDate.getTime();
+   }
 }

http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/3ef184ab/artemis-server/src/main/java/org/apache/activemq/artemis/utils/TimeUtils.java
----------------------------------------------------------------------
diff --git 
a/artemis-server/src/main/java/org/apache/activemq/artemis/utils/TimeUtils.java 
b/artemis-server/src/main/java/org/apache/activemq/artemis/utils/TimeUtils.java
new file mode 100644
index 0000000..faf109d
--- /dev/null
+++ 
b/artemis-server/src/main/java/org/apache/activemq/artemis/utils/TimeUtils.java
@@ -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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.activemq.artemis.utils;
+
+import java.text.DecimalFormat;
+import java.text.DecimalFormatSymbols;
+import java.text.NumberFormat;
+import java.util.Locale;
+
+/**
+ * Time utils.
+ *
+ * @version
+ */
+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/karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/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) * 24);
+      String s = fmtI.format(days) + (days > 1 ? " days" : " day");
+      if (hours != 0) {
+         s += " " + fmtI.format(hours) + (hours > 1 ? " hours" : " hour");
+      }
+      return s;
+   }
+
+}

Reply via email to