Author: markt Date: Sat Aug 15 17:40:40 2009 New Revision: 804495 URL: http://svn.apache.org/viewvc?rev=804495&view=rev Log: Replace syncs with threadlocals. Improve the logic that determines when to generate a new format string
Modified: tomcat/trunk/java/org/apache/catalina/valves/ExtendedAccessLogValve.java Modified: tomcat/trunk/java/org/apache/catalina/valves/ExtendedAccessLogValve.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/valves/ExtendedAccessLogValve.java?rev=804495&r1=804494&r2=804495&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/valves/ExtendedAccessLogValve.java (original) +++ tomcat/trunk/java/org/apache/catalina/valves/ExtendedAccessLogValve.java Sat Aug 15 17:40:40 2009 @@ -222,61 +222,55 @@ // ------------------------------------------------------ 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<ElementTimestampStruct> currentDate = + new ThreadLocal<ElementTimestampStruct>() { + 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<ElementTimestampStruct> currentTime = + new ThreadLocal<ElementTimestampStruct>() { + 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 @@ + 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")); + } + } } --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org