use of com.netflix.hystrix.strategy.concurrency.HystrixRequestContext in project Hystrix by Netflix.
the class HystrixRequestCacheTest method testCache.
@Test
public void testCache() {
HystrixConcurrencyStrategy strategy = HystrixConcurrencyStrategyDefault.getInstance();
HystrixRequestContext context = HystrixRequestContext.initializeContext();
try {
HystrixRequestCache cache1 = HystrixRequestCache.getInstance(HystrixCommandKey.Factory.asKey("command1"), strategy);
cache1.putIfAbsent("valueA", new TestObservable("a1"));
cache1.putIfAbsent("valueA", new TestObservable("a2"));
cache1.putIfAbsent("valueB", new TestObservable("b1"));
HystrixRequestCache cache2 = HystrixRequestCache.getInstance(HystrixCommandKey.Factory.asKey("command2"), strategy);
cache2.putIfAbsent("valueA", new TestObservable("a3"));
assertEquals("a1", cache1.get("valueA").toObservable().toBlocking().last());
assertEquals("b1", cache1.get("valueB").toObservable().toBlocking().last());
assertEquals("a3", cache2.get("valueA").toObservable().toBlocking().last());
assertNull(cache2.get("valueB"));
} catch (Exception e) {
fail("Exception: " + e.getMessage());
e.printStackTrace();
} finally {
context.shutdown();
}
context = HystrixRequestContext.initializeContext();
try {
// with a new context the instance should have nothing in it
HystrixRequestCache cache = HystrixRequestCache.getInstance(HystrixCommandKey.Factory.asKey("command1"), strategy);
assertNull(cache.get("valueA"));
assertNull(cache.get("valueB"));
} finally {
context.shutdown();
}
}
use of com.netflix.hystrix.strategy.concurrency.HystrixRequestContext in project Hystrix by Netflix.
the class HystrixRequestCacheTest method testClearCache.
@Test
public void testClearCache() {
HystrixConcurrencyStrategy strategy = HystrixConcurrencyStrategyDefault.getInstance();
HystrixRequestContext context = HystrixRequestContext.initializeContext();
try {
HystrixRequestCache cache1 = HystrixRequestCache.getInstance(HystrixCommandKey.Factory.asKey("command1"), strategy);
cache1.putIfAbsent("valueA", new TestObservable("a1"));
assertEquals("a1", cache1.get("valueA").toObservable().toBlocking().last());
cache1.clear("valueA");
assertNull(cache1.get("valueA"));
} catch (Exception e) {
fail("Exception: " + e.getMessage());
e.printStackTrace();
} finally {
context.shutdown();
}
}
use of com.netflix.hystrix.strategy.concurrency.HystrixRequestContext in project Hystrix by Netflix.
the class HystrixCommandAsyncDemo method startDemo.
public void startDemo(final boolean shouldLog) {
startMetricsMonitor(shouldLog);
while (true) {
final HystrixRequestContext context = HystrixRequestContext.initializeContext();
Observable<CreditCardAuthorizationResult> o = observeSimulatedUserRequestForOrderConfirmationAndCreditCardPayment();
final CountDownLatch latch = new CountDownLatch(1);
o.subscribe(new Subscriber<CreditCardAuthorizationResult>() {
@Override
public void onCompleted() {
latch.countDown();
context.shutdown();
}
@Override
public void onError(Throwable e) {
e.printStackTrace();
latch.countDown();
context.shutdown();
}
@Override
public void onNext(CreditCardAuthorizationResult creditCardAuthorizationResult) {
if (shouldLog) {
System.out.println("Request => " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString());
}
}
});
try {
latch.await(5000, TimeUnit.MILLISECONDS);
} catch (InterruptedException ex) {
System.out.println("INTERRUPTED!");
}
}
}
use of com.netflix.hystrix.strategy.concurrency.HystrixRequestContext in project ribbon by Netflix.
the class RibbonTest method testHystrixCache.
@Test
public void testHystrixCache() throws IOException {
// LogManager.getRootLogger().setLevel((Level)Level.DEBUG);
MockWebServer server = new MockWebServer();
String content = "Hello world";
MockResponse response = new MockResponse().setResponseCode(200).setHeader("Content-type", "text/plain").setBody(content);
server.enqueue(response);
server.enqueue(response);
server.play();
HttpResourceGroup group = Ribbon.createHttpResourceGroupBuilder("myclient").build();
HttpRequestTemplate<ByteBuf> template = group.newTemplateBuilder("test", ByteBuf.class).withUriTemplate("http://localhost:" + server.getPort()).withMethod("GET").withRequestCacheKey("xyz").build();
RibbonRequest<ByteBuf> request = template.requestBuilder().build();
HystrixRequestContext context = HystrixRequestContext.initializeContext();
try {
RibbonResponse<ByteBuf> ribbonResponse = request.withMetadata().execute();
assertFalse(ribbonResponse.getHystrixInfo().isResponseFromCache());
ribbonResponse = request.withMetadata().execute();
assertTrue(ribbonResponse.getHystrixInfo().isResponseFromCache());
} finally {
context.shutdown();
}
}
Aggregations