Skip to content

Commit

Permalink
Another approach
Browse files Browse the repository at this point in the history
  • Loading branch information
henryju committed Nov 10, 2023
1 parent ade16f7 commit 6cd0673
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,12 @@ public static class Builder<T> {
protected boolean validateMessages;
protected Consumer<GsonBuilder> configureGson;
protected ClassLoader classLoader;
/**
* @deprecated use {@link #tracer} instead
*/
@Deprecated
protected MessageTracer messageTracer;
protected PrintWriter tracer;

public Builder<T> setLocalService(Object localService) {
this.localServices = Collections.singletonList(localService);
Expand Down Expand Up @@ -294,7 +299,7 @@ public Builder<T> validateMessages(boolean validate) {

public Builder<T> traceMessages(PrintWriter tracer) {
if (tracer != null) {
this.messageTracer = new MessageTracer(tracer);
this.tracer = tracer;
}
return this;
}
Expand All @@ -318,8 +323,8 @@ public Launcher<T> create() {
// Create the JSON handler, remote endpoint and remote proxy
MessageJsonHandler jsonHandler = createJsonHandler();

if (messageTracer != null) {
messageTracer.setJsonHandler(jsonHandler);
if (tracer != null) {
messageTracer = new MessageTracer(tracer, jsonHandler);
}

RemoteEndpoint remoteEndpoint = createRemoteEndpoint(jsonHandler);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,13 @@
*/
public class MessageTracer implements Function<MessageConsumer, MessageConsumer> {
private final PrintWriter printWriter;
private MessageJsonHandler jsonHandler;
private final MessageJsonHandler jsonHandler;
private final Map<String, RequestMetadata> sentRequests = new HashMap<>();
private final Map<String, RequestMetadata> receivedRequests = new HashMap<>();

MessageTracer(PrintWriter printWriter) {
MessageTracer(PrintWriter printWriter, MessageJsonHandler jsonHandler) {
this.printWriter = Objects.requireNonNull(printWriter);
}

public void setJsonHandler(MessageJsonHandler jsonHandler) {
this.jsonHandler = jsonHandler;
this.jsonHandler = Objects.requireNonNull(jsonHandler);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.time.Clock;
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -49,6 +50,7 @@ public class TracingMessageConsumer implements MessageConsumer {
* @param sentRequests A map that keeps track of pending sent request data.
* @param receivedRequests A map that keeps track of pending received request data.
* @param printWriter Where to write the log to.
* @param jsonHandler The handler to be used to serialize messages.
* @param clock The clock that is used to calculate timestamps and durations.
*/
public TracingMessageConsumer(
Expand All @@ -67,6 +69,54 @@ public TracingMessageConsumer(
* @param receivedRequests A map that keeps track of pending received request data.
* @param printWriter Where to write the log to.
* @param clock The clock that is used to calculate timestamps and durations.
* @deprecated use {@link TracingMessageConsumer#TracingMessageConsumer(MessageConsumer, Map, Map, PrintWriter, MessageJsonHandler, Clock)}
*/
@Deprecated
public TracingMessageConsumer(
MessageConsumer messageConsumer,
Map<String, RequestMetadata> sentRequests,
Map<String, RequestMetadata> receivedRequests,
PrintWriter printWriter,
Clock clock) {
this(messageConsumer, sentRequests, receivedRequests, printWriter, defaultHandlerWithoutUserAdapters(), clock, null);
}

/**
* For backward compatibility, we are using a default handler that does not contain any user adapters.
*/
private static MessageJsonHandler defaultHandlerWithoutUserAdapters() {
return new MessageJsonHandler(Collections.emptyMap(), gsonBuilder -> {
gsonBuilder.setPrettyPrinting();
});
}

/**
* @param messageConsumer The {@link MessageConsumer} to wrap.
* @param sentRequests A map that keeps track of pending sent request data.
* @param receivedRequests A map that keeps track of pending received request data.
* @param printWriter Where to write the log to.
* @param clock The clock that is used to calculate timestamps and durations.
* @param locale THe Locale to format the timestamps and durations, or <code>null</code> to use default locale.
* @deprecated use {@link TracingMessageConsumer#TracingMessageConsumer(MessageConsumer, Map, Map, PrintWriter, MessageJsonHandler, Clock, Locale)}
*/
@Deprecated
public TracingMessageConsumer(
MessageConsumer messageConsumer,
Map<String, RequestMetadata> sentRequests,
Map<String, RequestMetadata> receivedRequests,
PrintWriter printWriter,
Clock clock,
Locale locale) {
this(messageConsumer, sentRequests, receivedRequests, printWriter, defaultHandlerWithoutUserAdapters(), clock, locale);
}

/**
* @param messageConsumer The {@link MessageConsumer} to wrap.
* @param sentRequests A map that keeps track of pending sent request data.
* @param receivedRequests A map that keeps track of pending received request data.
* @param printWriter Where to write the log to.
* @param jsonHandler The handler to be used to serialize messages.
* @param clock The clock that is used to calculate timestamps and durations.
* @param locale THe Locale to format the timestamps and durations, or <code>null</code> to use default locale.
*/
public TracingMessageConsumer(
Expand Down

0 comments on commit 6cd0673

Please sign in to comment.