Skip to content

Instantly share code, notes, and snippets.

@DominikMostek
Last active April 3, 2017 18:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DominikMostek/ec9c72052a645d8f73e24055142a6177 to your computer and use it in GitHub Desktop.
Save DominikMostek/ec9c72052a645d8f73e24055142a6177 to your computer and use it in GitHub Desktop.
import com.netflix.config.ConfigurationManager;
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandKey;
import com.netflix.hystrix.HystrixCommandProperties;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import java.io.IOException;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class HttpCallDemo {
public static void main(String... args) throws InterruptedException {
ConfigurationManager.getConfigInstance().setProperty("hystrix.command.customcommand.execution.isolation.thread.timeoutInMilliseconds", 1000);
new HttpCallDemo().start();
}
private final ThreadPoolExecutor pool = new ThreadPoolExecutor(1, 3, 3, TimeUnit.MINUTES,
new SynchronousQueue<>(), new ThreadPoolExecutor.CallerRunsPolicy());
private void start() throws InterruptedException {
pool.execute(() -> {
new CustomCommand().execute();
});
pool.awaitTermination(5, TimeUnit.SECONDS);
pool.shutdown();
}
private static class CustomCommand extends HystrixCommand<String> {
private HttpGet request;
protected CustomCommand() {
super(Setter
.withGroupKey(HystrixCommandGroupKey.Factory.asKey("Demo"))
.andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
.withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.THREAD))
.andCommandKey(HystrixCommandKey.Factory.asKey("customcommand")));
}
protected String run() throws Exception {
System.out.println("Main method runs in " + Thread.currentThread().getName());
String url = "https://httpbin.org/delay/2";
HttpClient client = HttpClientBuilder.create() .build();
this.request = new HttpGet(url);
String response = "NO RESPONSE";
try {
HttpResponse execute = client.execute(request);
response = IOUtils.toString(execute.getEntity().getContent());
} catch (IOException e) {
System.out.println("Exception " + e.getClass().getSimpleName());
}
System.out.println("Supplier returned on " + Thread.currentThread().getName());
System.out.println("Thread" + Thread.currentThread().getName() + " is " + (Thread.currentThread().isInterrupted() ? "interrupted" : "not ainterrupted"));
return response;
}
@Override
protected String getFallback() {
System.out.println("Fallback method runs in " + Thread.currentThread().getName());
System.out.println("Fallback because of " + getExecutionException().getClass().getSimpleName());
return "Fallback";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment