@Override public K call() throws Exception { HystrixRequestContext existingState = HystrixRequestContext.getContextForCurrentThread(); try { // set the state of this thread to that of its parent HystrixRequestContext.setContextOnCurrentThread(parentThreadState); // execute actual Callable with the state of the parent return actual.call(); } finally { // restore this thread back to its original state HystrixRequestContext.setContextOnCurrentThread(existingState); } }
@Override public void run() { HystrixRequestContext existingState = HystrixRequestContext.getContextForCurrentThread(); try { // set the state of this thread to that of its parent HystrixRequestContext.setContextOnCurrentThread(parentThreadState); // execute actual Callable with the state of the parent try { actual.call(); } catch (Exception e) { throw new RuntimeException(e); } } finally { // restore this thread back to its original state HystrixRequestContext.setContextOnCurrentThread(existingState); } }
private static void setRequestContextIfNeeded(final HystrixRequestContext currentRequestContext) { if (!HystrixRequestContext.isCurrentThreadInitialized()) { // even if the user Observable doesn't have context we want it set for chained operators HystrixRequestContext.setContextOnCurrentThread(currentRequestContext); } }
@Override public Void call() throws Exception { HystrixRequestContext existingState = HystrixRequestContext.getContextForCurrentThread(); try { // set the state of this thread to that of its parent HystrixRequestContext.setContextOnCurrentThread(parentThreadState); // execute actual Action0 with the state of the parent actual.call(); return null; } finally { // restore this thread back to its original state HystrixRequestContext.setContextOnCurrentThread(existingState); } } });
public void run() { HystrixRequestContext.setContextOnCurrentThread(context); CommandUsingRequestCache command2a = new CommandUsingRequestCache(2); Future<Boolean> resultCommand2a = command2a.queue(); try { assertTrue(resultCommand2a.get()); System.out.println(Thread.currentThread() + " " + command2a.isResponseFromCache()); if (command2a.isResponseFromCache()) { numCacheResponses.getAndIncrement(); } } catch (Exception e) { fail("Exception: " + e.getMessage()); } } });
public void run() { HystrixRequestContext.setContextOnCurrentThread(context); CommandUsingRequestCache command2b = new CommandUsingRequestCache(2); Future<Boolean> resultCommand2b = command2b.queue(); try { assertTrue(resultCommand2b.get()); System.out.println(Thread.currentThread() + " " + command2b.isResponseFromCache()); if (command2b.isResponseFromCache()) { numCacheResponses.getAndIncrement(); } } catch (Exception e) { fail("Exception: " + e.getMessage()); } } });
@After public void reset() { HystrixRequestContext.setContextOnCurrentThread(null); HystrixPropertiesFactory.reset(); HystrixPlugins.reset(); }
/** * Test that we can do basic execution without a RequestVariable being initialized. */ @Test public void testBasicExecutionWorksWithoutRequestVariable() throws Exception { /* force the RequestVariable to not be initialized */ HystrixRequestContext.setContextOnCurrentThread(null); TestHystrixCommand<Boolean> command = new SuccessfulTestCommand(); assertEquals(true, command.execute()); TestHystrixCommand<Boolean> command2 = new SuccessfulTestCommand(); assertEquals(true, command2.queue().get()); }
/** * Test that if we try and execute a command with a cacheKey without initializing RequestVariable that it gives an error. */ @Test(expected = HystrixRuntimeException.class) public void testCacheKeyExecutionRequiresRequestVariable() throws Exception { /* force the RequestVariable to not be initialized */ HystrixRequestContext.setContextOnCurrentThread(null); TestCircuitBreaker circuitBreaker = new TestCircuitBreaker(); SuccessfulCacheableCommand command = new SuccessfulCacheableCommand<String>(circuitBreaker, true, "one"); assertEquals("one", command.execute()); SuccessfulCacheableCommand command2 = new SuccessfulCacheableCommand<String>(circuitBreaker, true, "two"); assertEquals("two", command2.queue().get()); }
/** * Test that if we try and execute a command with a cacheKey without initializing RequestVariable that it gives an error. */ @Test public void testCacheKeyExecutionRequiresRequestVariable() { try { /* force the RequestVariable to not be initialized */ HystrixRequestContext.setContextOnCurrentThread(null); TestCircuitBreaker circuitBreaker = new TestCircuitBreaker(); SuccessfulCacheableCommand<String> command = new SuccessfulCacheableCommand<String>(circuitBreaker, true, "one"); assertEquals("one", command.observe().toBlocking().single()); SuccessfulCacheableCommand<String> command2 = new SuccessfulCacheableCommand<String>(circuitBreaker, true, "two"); assertEquals("two", command2.observe().toBlocking().toFuture().get()); fail("We expect an exception because cacheKey requires RequestVariable."); // semaphore isolated assertFalse(command.isExecutedInThread()); assertNull(command.getExecutionException()); } catch (Exception e) { e.printStackTrace(); } }
/** * Test that we can do basic execution without a RequestVariable being initialized. */ @Test public void testBasicExecutionWorksWithoutRequestVariable() { try { /* force the RequestVariable to not be initialized */ HystrixRequestContext.setContextOnCurrentThread(null); TestHystrixObservableCommand<Boolean> command = new SuccessfulTestCommand(ExecutionIsolationStrategy.SEMAPHORE); assertEquals(true, command.observe().toBlocking().single()); TestHystrixObservableCommand<Boolean> command2 = new SuccessfulTestCommand(ExecutionIsolationStrategy.SEMAPHORE); assertEquals(true, command2.observe().toBlocking().toFuture().get()); // we should be able to execute without a RequestVariable if ... // 1) We don't have a cacheKey // 2) We don't ask for the RequestLog // 3) We don't do collapsing // semaphore isolated assertFalse(command.isExecutedInThread()); assertNull(command.getExecutionException()); } catch (Exception e) { e.printStackTrace(); fail("We received an exception => " + e.getMessage()); } assertNull(HystrixRequestLog.getCurrentRequest()); }
/** * HystrixConcurrencyStrategy ** useDefaultRequestContext : false * HystrixCommand ** useRequestCache : true ** useRequestLog : true * * OUTCOME: RequestLog not set up in command, not available statically */ @Test public void testCommandRequiresContextConcurrencyStrategyDoesNotProvideItContextLeftUninitialized() { HystrixConcurrencyStrategy strategy = new CustomConcurrencyStrategy(false); HystrixPlugins.getInstance().registerConcurrencyStrategy(strategy); //context is not set up HystrixRequestContext.setContextOnCurrentThread(null); HystrixCommand<Boolean> cmd = new TestCommand(true, true); assertTrue(cmd.execute()); //command execution not affected by missing context printRequestLog(); assertNull(HystrixRequestLog.getCurrentRequest()); assertNull(HystrixRequestLog.getCurrentRequest(strategy)); assertNull(cmd.currentRequestLog); }
/** * HystrixConcurrencyStrategy ** useDefaultRequestContext : true * HystrixCommand ** useRequestCache : true ** useRequestLog : true * * OUTCOME: RequestLog not set up properly in command, static access is null */ @Test public void testCommandRequiresContextConcurrencyStrategyProvidesItContextLeftUninitialized() { HystrixConcurrencyStrategy strategy = new CustomConcurrencyStrategy(true); HystrixPlugins.getInstance().registerConcurrencyStrategy(strategy); //context is not set up HystrixRequestContext.setContextOnCurrentThread(null); HystrixCommand<Boolean> cmd = new TestCommand(true, true); assertTrue(cmd.execute()); //command execution not affected by missing context printRequestLog(); assertNull(HystrixRequestLog.getCurrentRequest()); assertNull(HystrixRequestLog.getCurrentRequest(strategy)); assertNull(cmd.currentRequestLog); }
/** * HystrixConcurrencyStrategy ** useDefaultRequestContext : true * HystrixCommand ** useRequestCache : false ** useRequestLog : false * * OUTCOME: RequestLog not set up in command, static access is null */ @Test public void testCommandDoesNotRequireContextConcurrencyStrategyProvidesItContextLeftUninitialized() { HystrixConcurrencyStrategy strategy = new CustomConcurrencyStrategy(true); HystrixPlugins.getInstance().registerConcurrencyStrategy(strategy); //context is not set up HystrixRequestContext.setContextOnCurrentThread(null); HystrixCommand<Boolean> cmd = new TestCommand(false, false); assertTrue(cmd.execute()); //command execution not affected by missing context printRequestLog(); assertNull(HystrixRequestLog.getCurrentRequest()); assertNull(HystrixRequestLog.getCurrentRequest(strategy)); assertNull(cmd.currentRequestLog); }
/** * HystrixConcurrencyStrategy ** useDefaultRequestContext : false * HystrixCommand ** useRequestCache : false ** useRequestLog : false * * OUTCOME: RequestLog not set up in command, not available statically */ @Test public void testCommandDoesNotRequireContextConcurrencyStrategyDoesNotProvideItContextLeftUninitialized() { HystrixConcurrencyStrategy strategy = new CustomConcurrencyStrategy(false); HystrixPlugins.getInstance().registerConcurrencyStrategy(strategy); //context is not set up HystrixRequestContext.setContextOnCurrentThread(null); HystrixCommand<Boolean> cmd = new TestCommand(true, true); assertTrue(cmd.execute()); //command execution unaffected by missing context printRequestLog(); assertNull(HystrixRequestLog.getCurrentRequest()); assertNull(HystrixRequestLog.getCurrentRequest(strategy)); assertNull(cmd.currentRequestLog); }
@Override public K call() throws Exception { HystrixRequestContext existingState = HystrixRequestContext.getContextForCurrentThread(); try { // set the state of this thread to that of its parent HystrixRequestContext.setContextOnCurrentThread(parentThreadState); // execute actual Callable with the state of the parent return actual.call(); } finally { // restore this thread back to its original state HystrixRequestContext.setContextOnCurrentThread(existingState); } }
@Override public void run() { HystrixRequestContext existingState = HystrixRequestContext.getContextForCurrentThread(); try { // set the state of this thread to that of its parent HystrixRequestContext.setContextOnCurrentThread(parentThreadState); // execute actual Callable with the state of the parent try { actual.call(); } catch (Exception e) { throw new RuntimeException(e); } } finally { // restore this thread back to its original state HystrixRequestContext.setContextOnCurrentThread(existingState); } }
private static void setRequestContextIfNeeded(final HystrixRequestContext currentRequestContext) { if (!HystrixRequestContext.isCurrentThreadInitialized()) { // even if the user Observable doesn't have context we want it set for chained operators HystrixRequestContext.setContextOnCurrentThread(currentRequestContext); } }
@Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { HystrixRequestContext.setContextOnCurrentThread(globalSharedContext); return super.preHandle(request, response, handler); } }
@Override public Void call() throws Exception { HystrixRequestContext existingState = HystrixRequestContext.getContextForCurrentThread(); try { // set the state of this thread to that of its parent HystrixRequestContext.setContextOnCurrentThread(parentThreadState); // execute actual Action0 with the state of the parent actual.call(); return null; } finally { // restore this thread back to its original state HystrixRequestContext.setContextOnCurrentThread(existingState); } } });