use of rx.functions.Action0 in project okhttp-OkGo by jeasonlzy.
the class RxCommonActivity method retrofitRequest.
@OnClick(R.id.retrofitRequest)
public void retrofitRequest(View view) {
Subscription subscription = //
ServerApi.getServerModel("aaa", "bbb").doOnSubscribe(new Action0() {
@Override
public void call() {
//开始请求前显示对话框
showLoading();
}
}).map(new Func1<LzyResponse<ServerModel>, ServerModel>() {
@Override
public ServerModel call(LzyResponse<ServerModel> response) {
return response.data;
}
}).observeOn(//切换到主线程
AndroidSchedulers.mainThread()).subscribe(new Action1<ServerModel>() {
@Override
public void call(ServerModel serverModel) {
//请求成功
dismissLoading();
handleResponse(serverModel, null, null);
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
//请求失败
throwable.printStackTrace();
showToast("请求失败");
dismissLoading();
handleError(null, null);
}
});
addSubscribe(subscription);
}
use of rx.functions.Action0 in project okhttp-OkGo by jeasonlzy.
the class RxCommonActivity method commonRequest.
@OnClick(R.id.commonRequest)
public void commonRequest(View view) {
Subscription subscription = //
OkGo.post(Urls.URL_METHOD).headers("aaa", //
"111").params("bbb", //
"222").getCall(StringConvert.create(), //以上为产生请求事件,请求默认发生在IO线程
RxAdapter.<String>create()).doOnSubscribe(new Action0() {
@Override
public void call() {
//开始请求前显示对话框
showLoading();
}
}).observeOn(//切换到主线程
AndroidSchedulers.mainThread()).subscribe(new Action1<String>() {
@Override
public void call(String s) {
//请求成功,关闭对话框
dismissLoading();
handleResponse(s, null, null);
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
throwable.printStackTrace();
//请求失败
dismissLoading();
showToast("请求失败");
handleError(null, null);
}
});
addSubscribe(subscription);
}
use of rx.functions.Action0 in project Hystrix by Netflix.
the class HystrixCommandTest method testEarlyUnsubscribeDuringExecutionViaToObservable.
@Test
public void testEarlyUnsubscribeDuringExecutionViaToObservable() {
class AsyncCommand extends HystrixCommand<Boolean> {
public AsyncCommand() {
super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("ASYNC")));
}
@Override
protected Boolean run() {
try {
Thread.sleep(500);
return true;
} catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
}
}
HystrixCommand<Boolean> cmd = new AsyncCommand();
final CountDownLatch latch = new CountDownLatch(1);
Observable<Boolean> o = cmd.toObservable();
Subscription s = o.doOnUnsubscribe(new Action0() {
@Override
public void call() {
System.out.println("OnUnsubscribe");
latch.countDown();
}
}).subscribe(new Subscriber<Boolean>() {
@Override
public void onCompleted() {
System.out.println("OnCompleted");
}
@Override
public void onError(Throwable e) {
System.out.println("OnError : " + e);
}
@Override
public void onNext(Boolean b) {
System.out.println("OnNext : " + b);
}
});
try {
Thread.sleep(10);
s.unsubscribe();
assertTrue(latch.await(200, TimeUnit.MILLISECONDS));
System.out.println("ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString());
assertEquals("Number of execution semaphores in use", 0, cmd.getExecutionSemaphore().getNumberOfPermitsUsed());
assertEquals("Number of fallback semaphores in use", 0, cmd.getFallbackSemaphore().getNumberOfPermitsUsed());
assertFalse(cmd.isExecutionComplete());
assertEquals(null, cmd.getFailedExecutionException());
assertNull(cmd.getExecutionException());
System.out.println("Execution time : " + cmd.getExecutionTimeInMilliseconds());
assertTrue(cmd.getExecutionTimeInMilliseconds() > -1);
assertFalse(cmd.isSuccessfulExecution());
assertCommandExecutionEvents(cmd, HystrixEventType.CANCELLED);
assertEquals(0, cmd.metrics.getCurrentConcurrentExecutionCount());
assertSaneHystrixRequestLog(1);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
use of rx.functions.Action0 in project Hystrix by Netflix.
the class HystrixThreadPoolTest method testUnsubscribeHystrixThreadPool.
@Test(timeout = 2500)
public void testUnsubscribeHystrixThreadPool() throws InterruptedException {
// methods are package-private so can't test it somewhere else
HystrixThreadPool pool = Factory.getInstance(HystrixThreadPoolKey.Factory.asKey("threadPoolFactoryTest"), HystrixThreadPoolPropertiesTest.getUnitTestPropertiesBuilder());
final AtomicBoolean interrupted = new AtomicBoolean();
final CountDownLatch start = new CountDownLatch(1);
final CountDownLatch end = new CountDownLatch(1);
HystrixContextScheduler hcs = new HystrixContextScheduler(HystrixPlugins.getInstance().getConcurrencyStrategy(), pool);
Scheduler.Worker w = hcs.createWorker();
try {
w.schedule(new Action0() {
@Override
public void call() {
start.countDown();
try {
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
interrupted.set(true);
}
} finally {
end.countDown();
}
}
});
start.await();
w.unsubscribe();
end.await();
Factory.shutdown();
assertTrue(interrupted.get());
} finally {
w.unsubscribe();
}
}
use of rx.functions.Action0 in project Hystrix by Netflix.
the class HystrixContextSchedulerTest method testUnsubscribeWrappedScheduler.
@Test(timeout = 2500)
public void testUnsubscribeWrappedScheduler() throws InterruptedException {
Scheduler s = Schedulers.newThread();
final AtomicBoolean interrupted = new AtomicBoolean();
final CountDownLatch start = new CountDownLatch(1);
final CountDownLatch end = new CountDownLatch(1);
HystrixContextScheduler hcs = new HystrixContextScheduler(s);
Scheduler.Worker w = hcs.createWorker();
try {
w.schedule(new Action0() {
@Override
public void call() {
start.countDown();
try {
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
interrupted.set(true);
}
} finally {
end.countDown();
}
}
});
start.await();
w.unsubscribe();
end.await();
assertTrue(interrupted.get());
} finally {
w.unsubscribe();
}
}
Aggregations