use of rx.functions.Action0 in project Hystrix by Netflix.
the class HystrixUtilizationStreamTest method testTwoSubscribersOneUnsubscribes.
@Test
public void testTwoSubscribersOneUnsubscribes() throws Exception {
final CountDownLatch latch1 = new CountDownLatch(1);
final CountDownLatch latch2 = new CountDownLatch(1);
final AtomicInteger payloads1 = new AtomicInteger(0);
final AtomicInteger payloads2 = new AtomicInteger(0);
Subscription s1 = stream.observe().take(100).doOnUnsubscribe(new Action0() {
@Override
public void call() {
latch1.countDown();
}
}).subscribe(new Subscriber<HystrixUtilization>() {
@Override
public void onCompleted() {
System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " : Dashboard 1 OnCompleted");
latch1.countDown();
}
@Override
public void onError(Throwable e) {
System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " : Dashboard 1 OnError : " + e);
latch1.countDown();
}
@Override
public void onNext(HystrixUtilization utilization) {
System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " : Dashboard 1 OnNext : " + utilization);
payloads1.incrementAndGet();
}
});
Subscription s2 = stream.observe().take(100).doOnUnsubscribe(new Action0() {
@Override
public void call() {
latch2.countDown();
}
}).subscribe(new Subscriber<HystrixUtilization>() {
@Override
public void onCompleted() {
System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " : Dashboard 2 OnCompleted");
latch2.countDown();
}
@Override
public void onError(Throwable e) {
System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " : Dashboard 2 OnError : " + e);
latch2.countDown();
}
@Override
public void onNext(HystrixUtilization utilization) {
System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " : Dashboard 2 OnNext : " + utilization);
payloads2.incrementAndGet();
}
});
// execute 1 command, then unsubscribe from first stream. then execute the rest
for (int i = 0; i < 50; i++) {
HystrixCommand<Integer> cmd = Command.from(groupKey, commandKey, HystrixEventType.SUCCESS, 50);
cmd.execute();
if (i == 1) {
s1.unsubscribe();
}
}
assertTrue(latch1.await(10000, TimeUnit.MILLISECONDS));
assertTrue(latch2.await(10000, TimeUnit.MILLISECONDS));
System.out.println("s1 got : " + payloads1.get() + ", s2 got : " + payloads2.get());
assertTrue("s1 got data", payloads1.get() > 0);
assertTrue("s2 got data", payloads2.get() > 0);
assertTrue("s1 got less data than s2", payloads2.get() > payloads1.get());
}
use of rx.functions.Action0 in project Hystrix by Netflix.
the class HystrixUtilizationStreamTest method testTwoSubscribersBothUnsubscribe.
@Test
public void testTwoSubscribersBothUnsubscribe() throws Exception {
final CountDownLatch latch1 = new CountDownLatch(1);
final CountDownLatch latch2 = new CountDownLatch(1);
final AtomicInteger payloads1 = new AtomicInteger(0);
final AtomicInteger payloads2 = new AtomicInteger(0);
Subscription s1 = stream.observe().take(100).doOnUnsubscribe(new Action0() {
@Override
public void call() {
latch1.countDown();
}
}).subscribe(new Subscriber<HystrixUtilization>() {
@Override
public void onCompleted() {
System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " : Dashboard 1 OnCompleted");
latch1.countDown();
}
@Override
public void onError(Throwable e) {
System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " : Dashboard 1 OnError : " + e);
latch1.countDown();
}
@Override
public void onNext(HystrixUtilization utilization) {
System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " : Dashboard 1 OnNext : " + utilization);
payloads1.incrementAndGet();
}
});
Subscription s2 = stream.observe().take(100).doOnUnsubscribe(new Action0() {
@Override
public void call() {
latch2.countDown();
}
}).subscribe(new Subscriber<HystrixUtilization>() {
@Override
public void onCompleted() {
System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " : Dashboard 2 OnCompleted");
latch2.countDown();
}
@Override
public void onError(Throwable e) {
System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " : Dashboard 2 OnError : " + e);
latch2.countDown();
}
@Override
public void onNext(HystrixUtilization utilization) {
System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " : Dashboard 2 OnNext : " + utilization);
payloads2.incrementAndGet();
}
});
// execute 2 commands, then unsubscribe from both streams, then execute the rest
for (int i = 0; i < 10; i++) {
HystrixCommand<Integer> cmd = Command.from(groupKey, commandKey, HystrixEventType.SUCCESS, 50);
cmd.execute();
if (i == 2) {
s1.unsubscribe();
s2.unsubscribe();
}
}
// both subscriptions have been cancelled - source should be too
assertFalse(stream.isSourceCurrentlySubscribed());
assertTrue(latch1.await(10000, TimeUnit.MILLISECONDS));
assertTrue(latch2.await(10000, TimeUnit.MILLISECONDS));
System.out.println("s1 got : " + payloads1.get() + ", s2 got : " + payloads2.get());
assertTrue("s1 got data", payloads1.get() > 0);
assertTrue("s2 got data", payloads2.get() > 0);
}
use of rx.functions.Action0 in project Hystrix by Netflix.
the class HystrixCollapserTest method testEarlyUnsubscribeExecutedViaToObservable.
@Test
public void testEarlyUnsubscribeExecutedViaToObservable() throws Exception {
TestCollapserTimer timer = new TestCollapserTimer();
HystrixCollapser<List<String>, String, String> collapser1 = new TestRequestCollapser(timer, 1);
Observable<String> response1 = collapser1.toObservable();
HystrixCollapser<List<String>, String, String> collapser2 = new TestRequestCollapser(timer, 2);
Observable<String> response2 = collapser2.toObservable();
final CountDownLatch latch1 = new CountDownLatch(1);
final CountDownLatch latch2 = new CountDownLatch(1);
final AtomicReference<String> value1 = new AtomicReference<String>(null);
final AtomicReference<String> value2 = new AtomicReference<String>(null);
Subscription s1 = response1.doOnUnsubscribe(new Action0() {
@Override
public void call() {
System.out.println(System.currentTimeMillis() + " : s1 Unsubscribed!");
latch1.countDown();
}
}).subscribe(new Subscriber<String>() {
@Override
public void onCompleted() {
System.out.println(System.currentTimeMillis() + " : s1 OnCompleted");
latch1.countDown();
}
@Override
public void onError(Throwable e) {
System.out.println(System.currentTimeMillis() + " : s1 OnError : " + e);
latch1.countDown();
}
@Override
public void onNext(String s) {
System.out.println(System.currentTimeMillis() + " : s1 OnNext : " + s);
value1.set(s);
}
});
Subscription s2 = response2.doOnUnsubscribe(new Action0() {
@Override
public void call() {
System.out.println(System.currentTimeMillis() + " : s2 Unsubscribed!");
latch2.countDown();
}
}).subscribe(new Subscriber<String>() {
@Override
public void onCompleted() {
System.out.println(System.currentTimeMillis() + " : s2 OnCompleted");
latch2.countDown();
}
@Override
public void onError(Throwable e) {
System.out.println(System.currentTimeMillis() + " : s2 OnError : " + e);
latch2.countDown();
}
@Override
public void onNext(String s) {
System.out.println(System.currentTimeMillis() + " : s2 OnNext : " + s);
value2.set(s);
}
});
s1.unsubscribe();
// let time pass that equals the default delay/period
timer.incrementTime(10);
assertTrue(latch1.await(1000, TimeUnit.MILLISECONDS));
assertTrue(latch2.await(1000, TimeUnit.MILLISECONDS));
assertNull(value1.get());
assertEquals("2", value2.get());
System.out.println("ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString());
assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
HystrixCollapserMetrics metrics = collapser1.getMetrics();
assertSame(metrics, collapser2.getMetrics());
HystrixInvokableInfo<?> command = HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().iterator().next();
// 1 should have been removed from batch
assertEquals(1, command.getNumberCollapsed());
}
use of rx.functions.Action0 in project Hystrix by Netflix.
the class HystrixCollapserTest method testEarlyUnsubscribeExecutedViaObserve.
@Test
public void testEarlyUnsubscribeExecutedViaObserve() throws Exception {
TestCollapserTimer timer = new TestCollapserTimer();
HystrixCollapser<List<String>, String, String> collapser1 = new TestRequestCollapser(timer, 1);
Observable<String> response1 = collapser1.observe();
HystrixCollapser<List<String>, String, String> collapser2 = new TestRequestCollapser(timer, 2);
Observable<String> response2 = collapser2.observe();
final CountDownLatch latch1 = new CountDownLatch(1);
final CountDownLatch latch2 = new CountDownLatch(1);
final AtomicReference<String> value1 = new AtomicReference<String>(null);
final AtomicReference<String> value2 = new AtomicReference<String>(null);
Subscription s1 = response1.doOnUnsubscribe(new Action0() {
@Override
public void call() {
System.out.println(System.currentTimeMillis() + " : s1 Unsubscribed!");
latch1.countDown();
}
}).subscribe(new Subscriber<String>() {
@Override
public void onCompleted() {
System.out.println(System.currentTimeMillis() + " : s1 OnCompleted");
latch1.countDown();
}
@Override
public void onError(Throwable e) {
System.out.println(System.currentTimeMillis() + " : s1 OnError : " + e);
latch1.countDown();
}
@Override
public void onNext(String s) {
System.out.println(System.currentTimeMillis() + " : s1 OnNext : " + s);
value1.set(s);
}
});
Subscription s2 = response2.doOnUnsubscribe(new Action0() {
@Override
public void call() {
System.out.println(System.currentTimeMillis() + " : s2 Unsubscribed!");
latch2.countDown();
}
}).subscribe(new Subscriber<String>() {
@Override
public void onCompleted() {
System.out.println(System.currentTimeMillis() + " : s2 OnCompleted");
latch2.countDown();
}
@Override
public void onError(Throwable e) {
System.out.println(System.currentTimeMillis() + " : s2 OnError : " + e);
latch2.countDown();
}
@Override
public void onNext(String s) {
System.out.println(System.currentTimeMillis() + " : s2 OnNext : " + s);
value2.set(s);
}
});
s1.unsubscribe();
// let time pass that equals the default delay/period
timer.incrementTime(10);
assertTrue(latch1.await(1000, TimeUnit.MILLISECONDS));
assertTrue(latch2.await(1000, TimeUnit.MILLISECONDS));
assertNull(value1.get());
assertEquals("2", value2.get());
System.out.println("ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString());
assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
HystrixCollapserMetrics metrics = collapser1.getMetrics();
assertSame(metrics, collapser2.getMetrics());
HystrixInvokableInfo<?> command = HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().iterator().next();
// 1 should have been removed from batch
assertEquals(1, command.getNumberCollapsed());
}
use of rx.functions.Action0 in project CustomViews by AndroidStudy233.
the class RxSchedulerActivity method changeScheduler.
/**
* 在不指定线程的情况下, RxJava 遵循的是线程不变的原则,即:在哪个线程调用 subscribe(),就在哪个线程生产事件;
* 在哪个线程生产事件,就在哪个线程消费事件。如果需要切换线程,就需要用到 Scheduler (调度器)。
* <p>
* Schedulers.immediate(): 直接在当前线程运行,相当于不指定线程。这是默认的 Scheduler。
* <p>
* Schedulers.newThread(): 总是启用新线程,并在新线程执行操作。
* <p>
* Schedulers.io(): I/O 操作(读写文件、读写数据库、网络信息交互等)所使用的 Scheduler。
* 行为模式和 newThread() 差不多,区别在于 io() 的内部实现是是用一个无数量上限的线程池,
* 可以重用空闲的线程,因此多数情况下 io() 比 newThread() 更有效率。不要把计算工作放在 io() 中,可以避免创建不必要的线程。
* <p>
* Schedulers.computation(): 计算所使用的 Scheduler。这个计算指的是 CPU 密集型计算,即不会被 I/O 等操作限制性能的操作,
* 例如图形的计算。这个 Scheduler 使用的固定的线程池,大小为 CPU 核数。不要把 I/O 操作放在 computation() 中,否则 I/O 操作的等待时间会浪费 CPU。
* <p>
* 另外, Android 还有一个专用的 AndroidSchedulers.mainThread(),它指定的操作将在 Android 主线程运行。
* 有了这几个 Scheduler ,就可以使用 subscribeOn() 和 observeOn() 两个方法来对线程进行控制了。
* <p>
* subscribeOn(): 指定 subscribe() 所发生的线程,即 Observable.OnSubscribe 被激活时所处的线程。或者叫做事件产生的线程。 (我的理解是被观察者事件创建))
* <p>
* observeOn(): 指定 Subscriber 所运行在的线程。或者叫做事件消费的线程。(我的理解是观察者的事件触发)
*/
public void changeScheduler() {
Observable.just(1, 2, 3, 4).subscribeOn(// 指定 subscribe() 发生在 IO 线程
Schedulers.io()).observeOn(// 指定 Subscriber 的回调发生在主线程()
AndroidSchedulers.mainThread()).subscribe(new Action1<Integer>() {
@Override
public void call(Integer number) {
Log.d("aaa", "number:" + number);
}
});
// doOnSubscribe, 就像subscriber的onstart()方法, 但是onstart不能确定是在哪儿执行的,
// 所以会有线程问题, 然而doOnSubscribe后面可以指定线程
// 当然相对应的还有doOnNext().
Observable.just(1, 2, 3, 4).subscribeOn(Schedulers.io()).doOnSubscribe(new Action0() {
@Override
public void call() {
// 比如有这么个操作需要在主线程执行
// progressBar.setVisibility(View.VISIBLE);
}
}).subscribeOn(// 指定doOnSubscribe的操作在主线程
AndroidSchedulers.mainThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Integer>() {
@Override
public void call(Integer integer) {
// 比如这里又要隐藏
// progressBar.setVisibility(View.INVISIBLE);
}
});
Observable.create(new Observable.OnSubscribe<Drawable>() {
@Override
public void call(Subscriber<? super Drawable> subscriber) {
try {
// 这里线程由subscribeOn决定
Thread.sleep(10 * 1000);
Drawable drawable = getResources().getDrawable(R.drawable.pic_sample);
subscriber.onNext(drawable);
subscriber.onCompleted();
} catch (InterruptedException e) {
e.printStackTrace();
subscriber.onError(e);
}
}
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Drawable>() {
// 以下线程由observeOn决定
@Override
public void onCompleted() {
Log.e("GG", "onCompleted");
}
@Override
public void onError(Throwable e) {
Log.e("GG", e.getMessage());
}
@Override
public void onNext(Drawable drawable) {
imageView.setImageDrawable(drawable);
}
});
}
Aggregations