From: markt Date: Sat, 15 Aug 2009 17:40:40 +0000 (+0000) Subject: Replace syncs with threadlocals. X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=264879cd58869cb10af0831eb253da9c98d14fe1;p=tomcat7.0 Replace syncs with threadlocals. Improve the logic that determines when to generate a new format string git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@804495 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/java/org/apache/catalina/valves/ExtendedAccessLogValve.java b/java/org/apache/catalina/valves/ExtendedAccessLogValve.java index af052bb23..14c6f819c 100644 --- a/java/org/apache/catalina/valves/ExtendedAccessLogValve.java +++ b/java/org/apache/catalina/valves/ExtendedAccessLogValve.java @@ -222,61 +222,55 @@ public class ExtendedAccessLogValve // ------------------------------------------------------ Lifecycle Methods - protected class DateElement implements AccessLogElement { - private Date currentDate = new Date(0); - - private String currentDateString = null; - - /** - * A date formatter to format a Date into a date in the format - * "yyyy-MM-dd". - */ - private SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd"); - - public DateElement() { - dateFormatter.setTimeZone(TimeZone.getTimeZone("GMT")); - } + protected static class DateElement implements AccessLogElement { + // Milli-seconds in 24 hours + private static final long INTERVAL = (1000 * 60 * 60 * 24); + private static final ThreadLocal currentDate = + new ThreadLocal() { + protected ElementTimestampStruct initialValue() { + return new ElementTimestampStruct("yyyy-MM-dd"); + } + }; + public void addElement(StringBuffer buf, Date date, Request request, Response response, long time) { - if (currentDate != date) { - synchronized (this) { - if (currentDate != date) { - currentDateString = dateFormatter.format(date); - currentDate = date; - } - } + ElementTimestampStruct eds = currentDate.get(); + long millis = eds.currentTimestamp.getTime(); + if (date.getTime() > (millis + INTERVAL -1) || + date.getTime() < millis) { + eds.currentTimestamp.setTime( + date.getTime() - (date.getTime() % INTERVAL)); + eds.currentTimestampString = + eds.currentTimestampFormat.format(eds.currentTimestamp); } - buf.append(currentDateString); + buf.append(eds.currentTimestampString); } } - protected class TimeElement implements AccessLogElement { - private Date currentDate = new Date(0); - - private String currentTimeString = null; - - /** - * A date formatter to format a Date into a time in the format - * "kk:mm:ss" (kk is a 24-hour representation of the hour). - */ - private SimpleDateFormat timeFormatter = new SimpleDateFormat("HH:mm:ss"); - - public TimeElement() { - timeFormatter.setTimeZone(TimeZone.getTimeZone("GMT")); - } + protected static class TimeElement implements AccessLogElement { + // Milli-seconds in a second + private static final long INTERVAL = 1000; + private static final ThreadLocal currentTime = + new ThreadLocal() { + protected ElementTimestampStruct initialValue() { + return new ElementTimestampStruct("HH:mm:ss"); + } + }; + public void addElement(StringBuffer buf, Date date, Request request, Response response, long time) { - if (currentDate != date) { - synchronized (this) { - if (currentDate != date) { - currentTimeString = timeFormatter.format(date); - currentDate = date; - } - } + ElementTimestampStruct eds = currentTime.get(); + long millis = eds.currentTimestamp.getTime(); + if (date.getTime() > (millis + INTERVAL -1) || + date.getTime() < millis) { + eds.currentTimestamp.setTime( + date.getTime() - (date.getTime() % INTERVAL)); + eds.currentTimestampString = + eds.currentTimestampFormat.format(eds.currentTimestamp); } - buf.append(currentTimeString); + buf.append(eds.currentTimestampString); } } @@ -845,5 +839,15 @@ public class ExtendedAccessLogValve + parameter); return null; } + + private static class ElementTimestampStruct { + private Date currentTimestamp = new Date(0); + private SimpleDateFormat currentTimestampFormat; + private String currentTimestampString; + ElementTimestampStruct(String format) { + currentTimestampFormat = new SimpleDateFormat(format); + currentTimestampFormat.setTimeZone(TimeZone.getTimeZone("GMT")); + } + } }