/** * Used for synchronous execution of command. * * @return R * Result of {@link #run()} execution or a fallback from {@link #getFallback()} if the command fails for any reason. * @throws HystrixRuntimeException * if a failure occurs and a fallback cannot be retrieved * @throws HystrixBadRequestException * if invalid arguments or state were used representing a user failure, not a system failure * @throws IllegalStateException * if invoked more than once */ public R execute() { try { return queue().get(); } catch (Exception e) { throw Exceptions.sneakyThrow(decomposeException(e)); } }
@Test public void testInsideHystrixSemaphoreQueue() throws Exception { HystrixCommand<Boolean> command = new HystrixCommand<Boolean>(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("TestUtil")) .andCommandKey(HystrixCommandKey.Factory.asKey("SemaphoreIsolatedCommandName")) .andCommandPropertiesDefaults(HystrixCommandProperties.Setter().withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.SEMAPHORE))) { @Override protected Boolean run() { assertEquals("SemaphoreIsolatedCommandName", Hystrix.getCurrentThreadExecutingCommand().name()); System.out.println("Semaphore Thread : " + Thread.currentThread().getName()); //should be a single execution on the caller thread (since it's busy here) assertEquals(1, Hystrix.getCommandCount()); return Hystrix.getCurrentThreadExecutingCommand() != null; } }; // it should be true for semaphore isolation as well assertTrue(command.queue().get()); // and then be null again once done assertNull(Hystrix.getCurrentThreadExecutingCommand()); }
@Override public Future<Object> queue() { if (this.metadata.isAsynchronous() && this.bulkheadMetricsCollection != null) { this.bulkheadMetricsCollection.getCurrentlyWaiting().incrementAndGet(); this.waitingStartTime = Instant.now(); } return super.queue(); }
/** * Used for synchronous execution of command. * * @return R * Result of {@link #run()} execution or a fallback from {@link #getFallback()} if the command fails for any reason. * @throws HystrixRuntimeException * if a failure occurs and a fallback cannot be retrieved * @throws HystrixBadRequestException * if invalid arguments or state were used representing a user failure, not a system failure * @throws IllegalStateException * if invoked more than once */ public R execute() { try { return queue().get(); } catch (Exception e) { throw Exceptions.sneakyThrow(decomposeException(e)); } }