Skip to content

Commit

Permalink
Refactor SimpleDateFormat to the new DateTimeFormatter of JDK 8 which…
Browse files Browse the repository at this point in the history
… is thread safe.
  • Loading branch information
Guido Medina authored and ktoso committed Apr 24, 2017
1 parent a2a6981 commit 64a3a9c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 25 deletions.
20 changes: 9 additions & 11 deletions akka-actor/src/main/scala/akka/event/Logging.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
*/
package akka.event

import java.time.Instant
import java.time.format.DateTimeFormatter
import java.util.concurrent.TimeoutException
import java.util.concurrent.atomic.AtomicInteger

Expand All @@ -12,13 +14,13 @@ import akka.dispatch.RequiresMessageQueue
import akka.event.Logging._
import akka.util.ReentrantGuard
import akka.util.Helpers.toRootLowerCase
import akka.{ AkkaException, ConfigurationException }
import akka.{AkkaException, ConfigurationException}

import scala.annotation.implicitNotFound
import scala.collection.immutable
import scala.concurrent.Await
import scala.language.existentials
import scala.util.control.{ NoStackTrace, NonFatal }
import scala.util.control.{NoStackTrace, NonFatal}

/**
* This trait brings log level handling to the EventStream: it reads the log
Expand Down Expand Up @@ -861,14 +863,11 @@ object Logging {
*/
class LoggerInitializationException(msg: String) extends AkkaException(msg)

private val formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss.SSS")

trait StdOutLogger {

import StdOutLogger._
import java.text.SimpleDateFormat
import java.util.Date

private val date = new Date()
private val dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss.SSS")

// format: OFF
// FIXME: remove those when we have the chance to break binary compatibility
Expand All @@ -879,10 +878,9 @@ object Logging {
private val debugFormat = DebugFormat
// format: ON

def timestamp(event: LogEvent): String = synchronized {
date.setTime(event.timestamp)
dateFormat.format(date)
} // SDF isn't threadsafe
def timestamp(event: LogEvent): String = {
formatter.format(Instant.ofEpochMilli(event.timestamp))
}

def print(event: Any): Unit = event match {
case e: Error error(e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ package akka.remote.artery

import java.io.File
import java.nio.file.Files
import java.text.SimpleDateFormat
import java.util.Date
import java.time.Instant
import java.time.format.DateTimeFormatter

import akka.actor.ActorSystem

Expand All @@ -27,15 +27,16 @@ object BenchmarkFileReporter {
target
}

val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss")

def apply(testName: String, system: ActorSystem): BenchmarkFileReporter =
new BenchmarkFileReporter {
val gitCommit = {
import sys.process._
Try("git describe".!!.trim).getOrElse("[unknown]")
}
val testResultFile: File = {
val format = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss")
val fileName = s"${format.format(new Date())}-Artery-$testName-$gitCommit-results.txt"
val fileName = s"${formatter.format(Instant.now())}-Artery-$testName-$gitCommit-results.txt"
new File(targetDirectory, fileName)
}
val config = system.settings.config
Expand Down
15 changes: 7 additions & 8 deletions akka-remote/src/main/java/akka/remote/artery/AeronErrorLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@

import java.io.File;
import java.nio.MappedByteBuffer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.atomic.AtomicLong;

/**
Expand All @@ -41,7 +41,7 @@ public class AeronErrorLog
final DirectBuffer cncMetaDataBuffer;
final int cncVersion;
final AtomicBuffer buffer;
final SimpleDateFormat dateFormat;
final DateTimeFormatter formatter;

public AeronErrorLog(File cncFile)
{
Expand All @@ -50,8 +50,7 @@ public AeronErrorLog(File cncFile)
cncMetaDataBuffer = CncFileDescriptor.createMetaDataBuffer(cncByteBuffer);
cncVersion = cncMetaDataBuffer.getInt(CncFileDescriptor.cncVersionOffset(0));
buffer = CncFileDescriptor.createErrorLogBuffer(cncByteBuffer, cncMetaDataBuffer);
dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSZ");

formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSZ");

if (CncFileDescriptor.CNC_VERSION != cncVersion)
{
Expand All @@ -62,7 +61,7 @@ public AeronErrorLog(File cncFile)

public long logErrors(LoggingAdapter log, long sinceTimestamp)
{
// using AtomicLong because access from lambda, not because of currency
// using AtomicLong because access from lambda, not because of concurrency
final AtomicLong lastTimestamp = new AtomicLong(sinceTimestamp);

ErrorLogReader.read(
Expand All @@ -71,8 +70,8 @@ public long logErrors(LoggingAdapter log, long sinceTimestamp)
log.error(String.format(
"Aeron error: %d observations from %s to %s for:%n %s",
observationCount,
dateFormat.format(new Date(firstObservationTimestamp)),
dateFormat.format(new Date(lastObservationTimestamp)),
formatter.format(Instant.ofEpochMilli(firstObservationTimestamp)),
formatter.format(Instant.ofEpochMilli(lastObservationTimestamp)),
encodedException));
lastTimestamp.set(Math.max(lastTimestamp.get(), lastObservationTimestamp));
}, sinceTimestamp);
Expand Down
8 changes: 6 additions & 2 deletions project/TimeStampede.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
*/
package akka

import java.time.Instant
import java.time.format.DateTimeFormatter

import sbt._
import sbt.Keys._

Expand All @@ -26,8 +29,9 @@ object TimeStampede extends AutoPlugin {
else version
}

val formatter = DateTimeFormatter.ofPattern("yyyyMMdd-HHmmss")

def timestamp(time: Long): String = {
val format = new java.text.SimpleDateFormat("yyyyMMdd-HHmmss")
format.format(new java.util.Date(time))
formatter.format(Instant.ofEpochMilli(time))
}
}

0 comments on commit 64a3a9c

Please sign in to comment.