DagR DagR - 1 month ago 9
Java Question

How do timeouts work in Hystrix with Observables?

I'm working on a Hystrix-enabled Spring Boot-app using Reactive Observables and trying to figure out how timeouts works. I assumed that if a timeout happened during execution, Hystrix would immediately return a response (fallback or exception). Now this seems not to be the case given my code below. Instead the call to

myService.getString()
blocks for 5 secs. When it finally returns, the throwable lambda is executed.

Are my assumptions incorrect or is something else wrong below?

@SpringBootApplication
@EnableCircuitBreaker
public class App {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(App.class, args);
}
}

@RestController
public class MyController {

@Autowired
private MyService myService;

@RequestMapping("/")
public DeferredResult index() {

DeferredResult<String> result = new DeferredResult<>();
Observable<String> res = myService.getString();

res.subscribe(s -> {
result.setResult("Got a " + s);
},
throwable -> {
result.setErrorResult("Got a " + throwable.getMessage());
});

return result;
}
}


@Service
public class MyService {

@HystrixCommand() // Default timeout is 1 sec
public Observable<String> getString() {
return Observable.create(subscriber -> {
if (!subscriber.isUnsubscribed()) {

try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
subscriber.onNext("regular response");
subscriber.onCompleted();
}
});
}
}


pom.xml:

http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0

<groupId>dag</groupId>
<artifactId>dag</artifactId>
<version>1.0-SNAPSHOT</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-javanica</artifactId>
<version>1.5.5</version>
</dependency>
</dependencies>

<properties>
<java.version>1.8</java.version>
</properties>


<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>


Thanks for any help!

Answer

Setting execution.isolation.strategy to THREAD seems to solve my issue. The docs states that this should be the default but I double-checked and without this command property, everything is executed in the same thread...

@HystrixCommand(
  commandProperties = {
    @HystrixProperty(name = "execution.isolation.strategy", value = "THREAD")
  }
)
Comments