Skip to content

Commit

Permalink
Use java.time.Instant precision in
Browse files Browse the repository at this point in the history
org.apache.commons.pool2.impl.ThrowableCallStack.Snapshot throwable
message
  • Loading branch information
garydgregory committed Nov 8, 2024
1 parent ef93c90 commit fbce876
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ The <action> type attribute can be add,update,fix,remove.
<body>
<release version="2.12.1" date="YYYY-MM-DD" description="This is a feature and maintenance release (Java 8 or above).">
<!-- FIX -->
<action type="fix" dev="ggregory" due-to="Gary Gregory">Use java.time.Instant precision in org.apache.commons.pool2.impl.ThrowableCallStack.Snapshot throwable message.</action>
<!-- ADD -->
<!-- UPDATE -->
<action type="update" dev="ggregory">Bump org.apache.commons:commons-parent from 62 to 73.</action>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.Instant;

/**
* CallStack strategy that uses the stack trace from a {@link Throwable}. This strategy, while slower than the
Expand All @@ -34,12 +35,31 @@ public class ThrowableCallStack implements CallStack {
* A snapshot of a throwable.
*/
private static final class Snapshot extends Throwable {

private static final long serialVersionUID = 1L;
private final long timestampMillis = System.currentTimeMillis();
private final Instant timestamp;

/**
* Constructs a new instance with its message set to the now instant.
*/
public Snapshot() {
this(Instant.now());
}

/**
* Constructs a new instance and use the timestamp as the message with using {@link DateTimeFormatter#ISO_INSTANT} for more precision.
*
* @param timestamp normally the now instant.
*/
private Snapshot(final Instant timestamp) {
super(timestamp.toString());
this.timestamp = timestamp;
}
}

private final String messageFormat;

// We keep the SimpleDateFormat for backward compatibility instead of a DateTimeFormatter.
//@GuardedBy("dateFormat")
private final DateFormat dateFormat;

Expand Down Expand Up @@ -77,7 +97,8 @@ public synchronized boolean printStackTrace(final PrintWriter writer) {
message = messageFormat;
} else {
synchronized (dateFormat) {
message = dateFormat.format(Long.valueOf(snapshotRef.timestampMillis));
// The throwable message is in {@link DateTimeFormatter#ISO_INSTANT} format for more precision.
message = dateFormat.format(Long.valueOf(snapshotRef.timestamp.toEpochMilli()));
}
}
writer.println(message);
Expand Down

0 comments on commit fbce876

Please sign in to comment.