import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
public class Example {
public static void main(String[] args){
}
}
class CommandHelloWorld extends HystrixCommand<String> {
private final String name;
public CommandHelloWorld(String name) {
super(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"));
this.name = name;
}
@Override
protected String run() {
// a real example would do work like a network call here
try{
Socket socket = new Socket("localhost", 19065);
System.out.println("Started client socket at "
+ socket.getLocalSocketAddress());
BufferedReader socketReader = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
BufferedWriter socketWriter = new BufferedWriter(new OutputStreamWriter(
socket.getOutputStream()));
BufferedReader consoleReader = new BufferedReader(
new InputStreamReader(System.in));
String promptMsg = "Please enter a message (Bye to quit):";
String outMsg = "hello";
System.out.print(promptMsg);
// while ((outMsg = consoleReader.readLine()) != null) {
/* if (outMsg.equalsIgnoreCase("bye")) {
break;
}*/
// Add a new line to the message to the server,
// because the server reads one line at a time.
socketWriter.write(outMsg);
socketWriter.write("\n");
socketWriter.flush();
// Read and display the message from the server
String inMsg = socketReader.readLine();
System.out.println("Server: " + inMsg);
System.out.println(); // Print a blank line
System.out.print(promptMsg);
// }
socket.close();
}catch(Exception e)
{
e.printStackTrace();
}
return "Hello " + name + "!";
}
}
______________________________________________________________________________________________
package com.test.hystrix;
import static org.junit.Assert.*;
import java.util.concurrent.Future;
import org.junit.Test;
import com.netflix.config.ConfigurationManager;
import com.netflix.hystrix.HystrixCommandKey;
import com.netflix.hystrix.HystrixCommandMetrics;
import com.netflix.hystrix.HystrixRequestLog;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import rx.Observable;
import rx.Observer;
import rx.functions.Action1;
public class ExampleTest {
@Test
public void testSynchronous() {
assertEquals("Hello World!", new CommandHelloWorld("World").execute());
assertEquals("Hello Bob!", new CommandHelloWorld("Bob").execute());
}
@Test
public void testAsynchronous1() throws Exception {
assertEquals("Hello World!", new CommandHelloWorld("World").queue().get());
assertEquals("Hello Bob!", new CommandHelloWorld("Bob").queue().get());
}
@Test
public void testAsynchronous2() throws Exception {
Future<String> fWorld = new CommandHelloWorld("World").queue();
Future<String> fBob = new CommandHelloWorld("Bob").queue();
assertEquals("Hello World!", fWorld.get());
assertEquals("Hello Bob!", fBob.get());
}
@Test
public void testObservable() throws Exception {
Observable<String> fWorld = new CommandHelloWorld("World").observe();
Observable<String> fBob = new CommandHelloWorld("Bob").observe();
// blocking
assertEquals("Hello World!", fWorld.toBlocking().single());
assertEquals("Hello Bob!", fBob.toBlocking().single());
// non-blocking
// - this is a verbose anonymous inner-class approach and doesn't do assertions
fWorld.subscribe(new Observer<String>() {
@Override
public void onCompleted() {
// nothing needed here
System.out.println("Completed");
}
@Override
public void onError(Throwable e) {
e.printStackTrace();
}
@Override
public void onNext(String v) {
System.out.println("onNext: " + v);
}
});
fBob.subscribe(new Action1<String>() {
@Override
public void call(String v) {
System.out.println("onNext: " + v);
}
});
}
@Test
public void should_execute_fallback_method_when_circuit_is_open() {
//Initialize HystrixRequestContext to be able to get some metrics
HystrixRequestContext context = HystrixRequestContext.initializeContext();
HystrixCommandMetrics creditCardMetrics = HystrixCommandMetrics.getInstance(HystrixCommandKey.Factory.asKey(CommandHelloWorld.class.getSimpleName()));
//We use Archaius to set the circuit as closed.
ConfigurationManager.getConfigInstance().setProperty("hystrix.command.default.circuitBreaker.forceOpen", false);
String successMessage = new CommandHelloWorld("Heaven").execute();
assertThat(successMessage, is("Hello World"));
//We use Archaius to open the circuit
ConfigurationManager.getConfigInstance().setProperty("hystrix.command.default.circuitBreaker.forceOpen", true);
String failMessage = new CommandHelloWorld("Magnanimous").execute();
assertThat(failMessage, is("Good Bye"));
//Prints Request => HelloWorldRestCommand[SUCCESS][19ms], HelloWorldRestCommand[SHORT_CIRCUITED, FALLBACK_SUCCESS][0ms]
System.out.println("Request => " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString());
assertThat(creditCardMetrics.getHealthCounts().getTotalRequests(), is(2));
assertThat(creditCardMetrics.getHealthCounts().getErrorCount(), is(1));
}
}