Skip to content

Instantly share code, notes, and snippets.

@aspyker
Created July 15, 2014 01:04
Show Gist options
  • Save aspyker/517d4ceaccdeaa6087de to your computer and use it in GitHub Desktop.
Save aspyker/517d4ceaccdeaa6087de to your computer and use it in GitHub Desktop.
Try #2 on RxJava + Ribbon 2.0 in Acme Air
package com.acmeair.tests.ribbon;
import java.nio.charset.Charset;
import java.util.Map;
import rx.Observable;
import rx.functions.Func1;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.UnpooledByteBufAllocator;
import com.netflix.hystrix.Hystrix;
import com.netflix.hystrix.HystrixExecutableInfo;
import com.netflix.ribbon.ClientOptions;
import com.netflix.ribbon.Ribbon;
import com.netflix.ribbon.http.HttpRequestTemplate;
import com.netflix.ribbon.http.HttpResourceGroup;
import com.netflix.ribbon.hystrix.FallbackHandler;
public class Test {
private final HttpResourceGroup httpResourceGroup;
private final HttpRequestTemplate<ByteBuf> createAuthTokenTemplate;
private final HttpRequestTemplate<ByteBuf> validateAuthTokenTemplate;
public Test(int port) {
httpResourceGroup = Ribbon.createHttpResourceGroup("authServiceClient",
ClientOptions.create()
.withMaxAutoRetriesNextServer(3)
.withConfigurationBasedServerList("localhost:" + port));
createAuthTokenTemplate = httpResourceGroup.newRequestTemplate("createAuthToken", ByteBuf.class)
.withMethod("POST")
.withUriTemplate("/rest/api/authtoken/byuserid/{userId}")
.withFallbackProvider(new CreateAuthTokenFallbackHandler())
.withHeader("SomeHeader", "SomeHeaderValue"); // TODO: Handle headers of interest
validateAuthTokenTemplate = httpResourceGroup.newRequestTemplate("validateAuthToken", ByteBuf.class)
.withMethod("GET")
.withUriTemplate("/rest/api/authtoken/{tokenId}")
.withFallbackProvider(new ValidateAuthTokenFallbackHandler());
}
public void makeBlockingRequests() {
Observable<ByteBuf> oCreateToken = createAuthTokenTemplate.requestBuilder().withRequestProperty("userId", "user1").build().toObservable();
String result1 = oCreateToken.map(new Func1<ByteBuf, String>() {
@Override
public String call(ByteBuf buf) {
String string = buf.toString(Charset.defaultCharset());
return string;
}
}).toBlocking().first();
System.out.println("result1 = " + result1);
Observable<ByteBuf> oValidateToken = validateAuthTokenTemplate.requestBuilder().withRequestProperty("tokenId", "0d2b6031-0bec-417e-8f42-b77a8bf6544f").build().toObservable();
String result2 = oValidateToken.map(new Func1<ByteBuf, String>() {
@Override
public String call(ByteBuf buf) {
String string = buf.toString(Charset.defaultCharset());
return string;
}
}).toBlocking().first();
System.out.println("result2 = " + result2);
Hystrix.reset();
}
public static void main(String args[]) {
Test t = new Test(8888);
t.makeBlockingRequests();
}
private class ValidateAuthTokenFallbackHandler implements FallbackHandler<ByteBuf> {
@Override
public Observable<ByteBuf> getFallback(HystrixExecutableInfo<?> hystrixInfo, Map<String, Object> requestProperties) {
// TODO: Do something more useful
byte[] bytes = "{}".getBytes(Charset.defaultCharset());
ByteBuf byteBuf = UnpooledByteBufAllocator.DEFAULT.buffer(bytes.length);
byteBuf.writeBytes(bytes);
return Observable.just(byteBuf);
}
}
private class CreateAuthTokenFallbackHandler implements FallbackHandler<ByteBuf> {
@Override
public Observable<ByteBuf> getFallback(HystrixExecutableInfo<?> hystrixInfo, Map<String, Object> requestProperties) {
// TODO: Do something more useful
byte[] bytes = "{}".getBytes(Charset.defaultCharset());
ByteBuf byteBuf = UnpooledByteBufAllocator.DEFAULT.buffer(bytes.length);
byteBuf.writeBytes(bytes);
return Observable.just(byteBuf);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment