From bf924aa8d7bf2038c0e8f685f2dd80e9112feaa8 Mon Sep 17 00:00:00 2001 From: Matt Jacobs Date: Wed, 22 Jun 2016 06:58:28 -0700 Subject: [PATCH] Deprecated HystrixConfigurationJsonStream in favor of compositional serialization --- .../HystrixRequestEventsSseServlet.java | 1 + .../stream/HystrixConfigSseServlet.java | 12 ++----- .../HystrixConfigurationJsonStream.java | 3 ++ .../serial/SerialHystrixConfiguration.java | 33 ++++++++++++++++--- 4 files changed, 34 insertions(+), 15 deletions(-) diff --git a/hystrix-contrib/hystrix-metrics-event-stream/src/main/java/com/netflix/hystrix/contrib/requests/stream/HystrixRequestEventsSseServlet.java b/hystrix-contrib/hystrix-metrics-event-stream/src/main/java/com/netflix/hystrix/contrib/requests/stream/HystrixRequestEventsSseServlet.java index 87e9d5ecb..d5405348f 100644 --- a/hystrix-contrib/hystrix-metrics-event-stream/src/main/java/com/netflix/hystrix/contrib/requests/stream/HystrixRequestEventsSseServlet.java +++ b/hystrix-contrib/hystrix-metrics-event-stream/src/main/java/com/netflix/hystrix/contrib/requests/stream/HystrixRequestEventsSseServlet.java @@ -26,6 +26,7 @@ import java.util.concurrent.atomic.AtomicInteger; /** + * Servlet that writes SSE JSON every time a request is made */ public class HystrixRequestEventsSseServlet extends HystrixSampleSseServlet { diff --git a/hystrix-contrib/hystrix-metrics-event-stream/src/main/java/com/netflix/hystrix/contrib/sample/stream/HystrixConfigSseServlet.java b/hystrix-contrib/hystrix-metrics-event-stream/src/main/java/com/netflix/hystrix/contrib/sample/stream/HystrixConfigSseServlet.java index f2530fdda..5f5bb04de 100644 --- a/hystrix-contrib/hystrix-metrics-event-stream/src/main/java/com/netflix/hystrix/contrib/sample/stream/HystrixConfigSseServlet.java +++ b/hystrix-contrib/hystrix-metrics-event-stream/src/main/java/com/netflix/hystrix/contrib/sample/stream/HystrixConfigSseServlet.java @@ -19,12 +19,10 @@ import com.netflix.config.DynamicPropertyFactory; import com.netflix.hystrix.config.HystrixConfiguration; import com.netflix.hystrix.config.HystrixConfigurationStream; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import com.netflix.hystrix.metric.serial.SerialHystrixConfiguration; import rx.Observable; import rx.functions.Func1; -import java.io.IOException; import java.util.concurrent.atomic.AtomicInteger; /** @@ -51,7 +49,6 @@ public class HystrixConfigSseServlet extends HystrixSampleSseServlet { private static final long serialVersionUID = -3599771169762858235L; - private static final Logger logger = LoggerFactory.getLogger(HystrixConfigSseServlet.class); /* used to track number of connections and throttle */ private static AtomicInteger concurrentConnections = new AtomicInteger(0); @@ -65,12 +62,7 @@ public HystrixConfigSseServlet() { super(sampleStream.map(new Func1() { @Override public String call(HystrixConfiguration hystrixConfiguration) { - try { - return HystrixConfigurationJsonStream.convertToString(hystrixConfiguration); - } catch (IOException ioe) { - logger.error("IOException creating JSON from HystrixUtilization", ioe); - return " : " + ioe.getMessage(); - } + return SerialHystrixConfiguration.toJsonString(hystrixConfiguration); } }), pausePollerThreadDelayInMs); } diff --git a/hystrix-contrib/hystrix-metrics-event-stream/src/main/java/com/netflix/hystrix/contrib/sample/stream/HystrixConfigurationJsonStream.java b/hystrix-contrib/hystrix-metrics-event-stream/src/main/java/com/netflix/hystrix/contrib/sample/stream/HystrixConfigurationJsonStream.java index 12f5f3d42..1008fb970 100644 --- a/hystrix-contrib/hystrix-metrics-event-stream/src/main/java/com/netflix/hystrix/contrib/sample/stream/HystrixConfigurationJsonStream.java +++ b/hystrix-contrib/hystrix-metrics-event-stream/src/main/java/com/netflix/hystrix/contrib/sample/stream/HystrixConfigurationJsonStream.java @@ -25,6 +25,7 @@ import com.netflix.hystrix.config.HystrixConfiguration; import com.netflix.hystrix.config.HystrixConfigurationStream; import com.netflix.hystrix.config.HystrixThreadPoolConfiguration; +import com.netflix.hystrix.metric.HystrixRequestEventsStream; import rx.Observable; import rx.functions.Func1; @@ -40,7 +41,9 @@ *
  • Consumer of your choice that wants control over where to embed this stream * * + * @deprecated Instead, prefer mapping your preferred serialization on top of {@link HystrixConfigurationStream#observe()}. */ +@Deprecated //since 1.5.4 public class HystrixConfigurationJsonStream { private static final JsonFactory jsonFactory = new JsonFactory(); diff --git a/hystrix-data-stream/src/main/java/com/netflix/hystrix/metric/serial/SerialHystrixConfiguration.java b/hystrix-data-stream/src/main/java/com/netflix/hystrix/metric/serial/SerialHystrixConfiguration.java index 960c8f08d..dd118abc9 100644 --- a/hystrix-data-stream/src/main/java/com/netflix/hystrix/metric/serial/SerialHystrixConfiguration.java +++ b/hystrix-data-stream/src/main/java/com/netflix/hystrix/metric/serial/SerialHystrixConfiguration.java @@ -32,6 +32,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.io.StringWriter; import java.nio.ByteBuffer; import java.util.HashMap; import java.util.Iterator; @@ -46,8 +47,34 @@ public static byte[] toBytes(HystrixConfiguration config) { try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); - JsonGenerator json = cborFactory.createGenerator(bos); + JsonGenerator cbor = cborFactory.createGenerator(bos); + serializeConfiguration(config, cbor); + + retVal = bos.toByteArray(); + } catch (Exception e) { + throw new RuntimeException(e); + } + + return retVal; + } + + public static String toJsonString(HystrixConfiguration config) { + StringWriter jsonString = new StringWriter(); + + try { + JsonGenerator json = jsonFactory.createGenerator(jsonString); + + serializeConfiguration(config, json); + } catch (Exception e) { + throw new RuntimeException(e); + } + + return jsonString.getBuffer().toString(); + } + + private static void serializeConfiguration(HystrixConfiguration config, JsonGenerator json) { + try { json.writeStartObject(); json.writeStringField("type", "HystrixConfig"); json.writeObjectFieldStart("commands"); @@ -76,14 +103,10 @@ public static byte[] toBytes(HystrixConfiguration config) { json.writeEndObject(); json.writeEndObject(); json.close(); - - - retVal = bos.toByteArray(); } catch (Exception e) { throw new RuntimeException(e); } - return retVal; } public static HystrixConfiguration fromByteBuffer(ByteBuffer bb) {