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 testClearCache() { HystrixConcurrencyStrategy strategy = HystrixConcurrencyStrategyDefault.getInstance(); HystrixRequestContext context = HystrixRequestContext.initializeContext(); try { HystrixRequestCache cache1 = HystrixRequestCache.getInstance(HystrixCommandKey.Factory.asKey("command1"), strategy); cache1.putIfAbsent("valueA", new TestObservable("a1")); assertEquals("a1", cache1.get("valueA").toBlocking().last()); cache1.clear("valueA"); assertNull(cache1.get("valueA")); } catch (Exception e) { fail("Exception: " + e.getMessage()); e.printStackTrace(); } finally { context.shutdown(); } }
@After public void tearDown() throws Exception { context.shutdown(); }
@Before public void setUp() throws Exception { context = HystrixRequestContext.initializeContext(); }