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!
Please clarify what's the isolation of your hystrix command and what hystrix version did you use?
"Timeouts now apply to semaphore-isolated commands as well as thread-isolated commands. Before 1.4.x, semaphore-isolated commands could not timeout. They now have a timeout registered on another (HystrixTimer) thread, which triggers the timeout flow. If you use semaphore-isolated commands, they will now see timeouts"
Anyway try to: