public static void main(String... args) {
    // initialize
    DiscoveryAndLoadBalancer.getFactory();
    // hystrix stream => http://localhost:9999
    startHystrixMetricsStream();

    System.out.println("Server => Starting at http://localhost:8080/");
    System.out.println("   Sample URLs: ");
    System.out.println("      - http://localhost:8080/device/home?userId=123");
    System.out.println("----------------------------------------------------------------");

    // start web services => http://localhost:8080
    RxNetty.createHttpServer(
            8080,
            (request, response) -> {
              if (request.getPath().contains("favicon.ico")) {
                return Observable.empty();
              }
              // System.out.println("Server => Request: " + request.getPath());
              return Observable.defer(
                      () -> {
                        HystrixRequestContext.initializeContext();
                        try {
                          return handleRoutes(request, response);
                        } catch (Throwable e) {
                          System.err.println("Server => Error [" + request.getPath() + "] => " + e);
                          response.setStatus(HttpResponseStatus.BAD_REQUEST);
                          return response.writeStringAndFlush(
                              "Error 500: Bad Request\n" + e.getMessage() + "\n");
                        }
                      })
                  .onErrorResumeNext(
                      error -> {
                        System.err.println("Server => Error: " + error.getMessage());
                        error.printStackTrace();
                        return writeError(request, response, "Failed: " + error.getMessage());
                      })
                  .doOnTerminate(
                      () -> {
                        if (HystrixRequestContext.isCurrentThreadInitialized()) {
                          System.out.println(
                              "Server => Request ["
                                  + request.getPath()
                                  + "] => "
                                  + HystrixRequestLog.getCurrentRequest()
                                      .getExecutedCommandsAsString());
                          HystrixRequestContext.getContextForCurrentThread().shutdown();
                        } else {
                          System.err.println(
                              "HystrixRequestContext not initialized for thread: "
                                  + Thread.currentThread());
                        }
                        response.close();
                      });
            })
        .startAndWait();
  }
  @Test
  public void testCache() {
    HystrixConcurrencyStrategy strategy = HystrixConcurrencyStrategyDefault.getInstance();
    HystrixRequestContext context = HystrixRequestContext.initializeContext();
    try {
      HystrixRequestCache cache1 =
          HystrixRequestCache.getInstance(HystrixCommandKey.Factory.asKey("command1"), strategy);
      cache1.putIfAbsent("valueA", new TestObservable("a1"));
      cache1.putIfAbsent("valueA", new TestObservable("a2"));
      cache1.putIfAbsent("valueB", new TestObservable("b1"));

      HystrixRequestCache cache2 =
          HystrixRequestCache.getInstance(HystrixCommandKey.Factory.asKey("command2"), strategy);
      cache2.putIfAbsent("valueA", new TestObservable("a3"));

      assertEquals("a1", cache1.get("valueA").toBlocking().last());
      assertEquals("b1", cache1.get("valueB").toBlocking().last());

      assertEquals("a3", cache2.get("valueA").toBlocking().last());
      assertNull(cache2.get("valueB"));
    } catch (Exception e) {
      fail("Exception: " + e.getMessage());
      e.printStackTrace();
    } finally {
      context.shutdown();
    }

    context = HystrixRequestContext.initializeContext();
    try {
      // with a new context  the instance should have nothing in it
      HystrixRequestCache cache =
          HystrixRequestCache.getInstance(HystrixCommandKey.Factory.asKey("command1"), strategy);
      assertNull(cache.get("valueA"));
      assertNull(cache.get("valueB"));
    } finally {
      context.shutdown();
    }
  }
 @Before
 public void setUp() throws Exception {
   context = HystrixRequestContext.initializeContext();
 }