Search in sources :

Example 6 with Action0

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);
}
Also used : Action0(rx.functions.Action0) LzyResponse(com.lzy.demo.model.LzyResponse) ServerModel(com.lzy.demo.model.ServerModel) Subscription(rx.Subscription) OnClick(butterknife.OnClick)

Example 7 with Action0

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);
}
Also used : Action0(rx.functions.Action0) Subscription(rx.Subscription) OnClick(butterknife.OnClick)

Example 8 with Action0

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();
    }
}
Also used : Action0(rx.functions.Action0) CountDownLatch(java.util.concurrent.CountDownLatch) HystrixRuntimeException(com.netflix.hystrix.exception.HystrixRuntimeException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Subscription(rx.Subscription) Test(org.junit.Test)

Example 9 with Action0

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();
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Action0(rx.functions.Action0) Scheduler(rx.Scheduler) Test(org.junit.Test)

Example 10 with Action0

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();
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Action0(rx.functions.Action0) Scheduler(rx.Scheduler) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

Action0 (rx.functions.Action0)134 Subscription (rx.Subscription)58 Test (org.junit.Test)56 CountDownLatch (java.util.concurrent.CountDownLatch)50 Action1 (rx.functions.Action1)28 AtomicReference (java.util.concurrent.atomic.AtomicReference)23 ArrayList (java.util.ArrayList)16 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)16 List (java.util.List)15 Func1 (rx.functions.Func1)13 HystrixRuntimeException (com.netflix.hystrix.exception.HystrixRuntimeException)12 Observable (rx.Observable)12 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)11 OnClick (butterknife.OnClick)10 IOException (java.io.IOException)9 CommandStreamTest (com.netflix.hystrix.metric.CommandStreamTest)8 UiThreadTest (android.support.test.annotation.UiThreadTest)7 PluginTestVerifier (com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier)7 TestCollapserTimer (com.netflix.hystrix.HystrixCollapserTest.TestCollapserTimer)7 Method (java.lang.reflect.Method)7