Search in sources :

Example 31 with Action1

use of rx.functions.Action1 in project LiYuJapanese by 54wall.

the class MainActivity method initVariable.

@Override
protected void initVariable(@Nullable Bundle savedInstanceState) {
    presenter = new MainActivityPresenterImpl(this);
    if (!registered) {
        busSubscription = RxBus.getDefault().toObserverable(EventContainer.class).subscribe(new Action1<EventContainer>() {

            @Override
            public void call(EventContainer eventContainer) {
                presenter.onBusEventInteraction(eventContainer);
            }
        });
        registered = true;
    }
    initToolbar();
    initRadioButtonView();
    initDrawerLayout();
    initNavigationView();
    initBanner();
// 无效
// mToolbar.setTitle(SharedPreferenceManager.getInstance().getString(Constants.CURRENT_LESSON, Constants.DEFAULT_LESSON));
}
Also used : MainActivityPresenterImpl(pri.weiqiang.liyujapanese.mvp.presenter.MainActivityPresenterImpl) Action1(rx.functions.Action1) EventContainer(pri.weiqiang.liyujapanese.rxbus.event.EventContainer)

Example 32 with Action1

use of rx.functions.Action1 in project LiYuJapanese by 54wall.

the class SettingActivity method initVariable.

@Override
protected void initVariable(@Nullable Bundle savedInstanceState) {
    if (!registered) {
        subscription = RxBus.getDefault().toObserverable(EventContainer.class).subscribe(new Action1<EventContainer>() {

            @Override
            public void call(EventContainer eventContainer) {
                if (eventContainer.getType() == EventContainer.TYPE_SETTING) {
                    SettingEvent event = (SettingEvent) eventContainer.getEvent();
                    showSnackBar(mRootLayout, event.getMsg());
                }
            }
        });
        registered = true;
    }
    initToolbar();
}
Also used : Action1(rx.functions.Action1) EventContainer(pri.weiqiang.liyujapanese.rxbus.event.EventContainer) SettingEvent(pri.weiqiang.liyujapanese.rxbus.event.SettingEvent)

Example 33 with Action1

use of rx.functions.Action1 in project MyJapanese by 54wall.

the class MainActivity method initVariable.

@Override
protected void initVariable(@Nullable Bundle savedInstanceState) {
    presenter = new MainActivityPresenterImpl(this);
    if (!registered) {
        busSubscription = RxBus.getDefault().toObserverable(EventContainer.class).subscribe(new Action1<EventContainer>() {

            @Override
            public void call(EventContainer eventContainer) {
                presenter.onBusEventInteraction(eventContainer);
            }
        });
        registered = true;
    }
    initToolbar();
    initRadioButtonView();
    initDrawerLayout();
    initNavigationView();
    initBanner();
// 无效
// mToolbar.setTitle(SharedPreferenceManager.getInstance().getString(Constants.CURRENT_LESSON, Constants.DEFAULT_LESSON));
}
Also used : MainActivityPresenterImpl(pri.weiqiang.myjapanese.mvp.presenter.MainActivityPresenterImpl) Action1(rx.functions.Action1) EventContainer(pri.weiqiang.myjapanese.rxbus.event.EventContainer)

Example 34 with Action1

use of rx.functions.Action1 in project MyJapanese by 54wall.

the class SettingActivity method initVariable.

@Override
protected void initVariable(@Nullable Bundle savedInstanceState) {
    if (!registered) {
        subscription = RxBus.getDefault().toObserverable(EventContainer.class).subscribe(new Action1<EventContainer>() {

            @Override
            public void call(EventContainer eventContainer) {
                if (eventContainer.getType() == EventContainer.TYPE_SETTING) {
                    SettingEvent event = (SettingEvent) eventContainer.getEvent();
                    showSnackBar(mRootLayout, event.getMsg());
                }
            }
        });
        registered = true;
    }
    initToolbar();
}
Also used : Action1(rx.functions.Action1) EventContainer(pri.weiqiang.myjapanese.rxbus.event.EventContainer) SettingEvent(pri.weiqiang.myjapanese.rxbus.event.SettingEvent)

Example 35 with Action1

use of rx.functions.Action1 in project Hystrix by Netflix.

the class AbstractCommand method getFallbackOrThrowException.

/**
 * Execute <code>getFallback()</code> within protection of a semaphore that limits number of concurrent executions.
 * <p>
 * Fallback implementations shouldn't perform anything that can be blocking, but we protect against it anyways in case someone doesn't abide by the contract.
 * <p>
 * If something in the <code>getFallback()</code> implementation is latent (such as a network call) then the semaphore will cause us to start rejecting requests rather than allowing potentially
 * all threads to pile up and block.
 *
 * @return K
 * @throws UnsupportedOperationException
 *             if getFallback() not implemented
 * @throws HystrixRuntimeException
 *             if getFallback() fails (throws an Exception) or is rejected by the semaphore
 */
private Observable<R> getFallbackOrThrowException(final AbstractCommand<R> _cmd, final HystrixEventType eventType, final FailureType failureType, final String message, final Exception originalException) {
    final HystrixRequestContext requestContext = HystrixRequestContext.getContextForCurrentThread();
    long latency = System.currentTimeMillis() - executionResult.getStartTimestamp();
    // record the executionResult
    // do this before executing fallback so it can be queried from within getFallback (see See https://github.com/Netflix/Hystrix/pull/144)
    executionResult = executionResult.addEvent((int) latency, eventType);
    if (isUnrecoverable(originalException)) {
        logger.error("Unrecoverable Error for HystrixCommand so will throw HystrixRuntimeException and not apply fallback. ", originalException);
        /* executionHook for all errors */
        Exception e = wrapWithOnErrorHook(failureType, originalException);
        return Observable.error(new HystrixRuntimeException(failureType, this.getClass(), getLogMessagePrefix() + " " + message + " and encountered unrecoverable error.", e, null));
    } else {
        if (isRecoverableError(originalException)) {
            logger.warn("Recovered from java.lang.Error by serving Hystrix fallback", originalException);
        }
        if (properties.fallbackEnabled().get()) {
            /* fallback behavior is permitted so attempt */
            final Action1<Notification<? super R>> setRequestContext = new Action1<Notification<? super R>>() {

                @Override
                public void call(Notification<? super R> rNotification) {
                    setRequestContextIfNeeded(requestContext);
                }
            };
            final Action1<R> markFallbackEmit = new Action1<R>() {

                @Override
                public void call(R r) {
                    if (shouldOutputOnNextEvents()) {
                        executionResult = executionResult.addEvent(HystrixEventType.FALLBACK_EMIT);
                        eventNotifier.markEvent(HystrixEventType.FALLBACK_EMIT, commandKey);
                    }
                }
            };
            final Action0 markFallbackCompleted = new Action0() {

                @Override
                public void call() {
                    long latency = System.currentTimeMillis() - executionResult.getStartTimestamp();
                    eventNotifier.markEvent(HystrixEventType.FALLBACK_SUCCESS, commandKey);
                    executionResult = executionResult.addEvent((int) latency, HystrixEventType.FALLBACK_SUCCESS);
                }
            };
            final Func1<Throwable, Observable<R>> handleFallbackError = new Func1<Throwable, Observable<R>>() {

                @Override
                public Observable<R> call(Throwable t) {
                    /* executionHook for all errors */
                    Exception e = wrapWithOnErrorHook(failureType, originalException);
                    Exception fe = getExceptionFromThrowable(t);
                    long latency = System.currentTimeMillis() - executionResult.getStartTimestamp();
                    Exception toEmit;
                    if (fe instanceof UnsupportedOperationException) {
                        // debug only since we're throwing the exception and someone higher will do something with it
                        logger.debug("No fallback for HystrixCommand. ", fe);
                        eventNotifier.markEvent(HystrixEventType.FALLBACK_MISSING, commandKey);
                        executionResult = executionResult.addEvent((int) latency, HystrixEventType.FALLBACK_MISSING);
                        toEmit = new HystrixRuntimeException(failureType, _cmd.getClass(), getLogMessagePrefix() + " " + message + " and no fallback available.", e, fe);
                    } else {
                        logger.debug("HystrixCommand execution " + failureType.name() + " and fallback failed.", fe);
                        eventNotifier.markEvent(HystrixEventType.FALLBACK_FAILURE, commandKey);
                        executionResult = executionResult.addEvent((int) latency, HystrixEventType.FALLBACK_FAILURE);
                        toEmit = new HystrixRuntimeException(failureType, _cmd.getClass(), getLogMessagePrefix() + " " + message + " and fallback failed.", e, fe);
                    }
                    // NOTE: we're suppressing fallback exception here
                    if (shouldNotBeWrapped(originalException)) {
                        return Observable.error(e);
                    }
                    return Observable.error(toEmit);
                }
            };
            final TryableSemaphore fallbackSemaphore = getFallbackSemaphore();
            final AtomicBoolean semaphoreHasBeenReleased = new AtomicBoolean(false);
            final Action0 singleSemaphoreRelease = new Action0() {

                @Override
                public void call() {
                    if (semaphoreHasBeenReleased.compareAndSet(false, true)) {
                        fallbackSemaphore.release();
                    }
                }
            };
            Observable<R> fallbackExecutionChain;
            // acquire a permit
            if (fallbackSemaphore.tryAcquire()) {
                try {
                    if (isFallbackUserDefined()) {
                        executionHook.onFallbackStart(this);
                        fallbackExecutionChain = getFallbackObservable();
                    } else {
                        // same logic as above without the hook invocation
                        fallbackExecutionChain = getFallbackObservable();
                    }
                } catch (Throwable ex) {
                    // If hook or user-fallback throws, then use that as the result of the fallback lookup
                    fallbackExecutionChain = Observable.error(ex);
                }
                return fallbackExecutionChain.doOnEach(setRequestContext).lift(new FallbackHookApplication(_cmd)).lift(new DeprecatedOnFallbackHookApplication(_cmd)).doOnNext(markFallbackEmit).doOnCompleted(markFallbackCompleted).onErrorResumeNext(handleFallbackError).doOnTerminate(singleSemaphoreRelease).doOnUnsubscribe(singleSemaphoreRelease);
            } else {
                return handleFallbackRejectionByEmittingError();
            }
        } else {
            return handleFallbackDisabledByEmittingError(originalException, failureType, message);
        }
    }
}
Also used : Action0(rx.functions.Action0) Action1(rx.functions.Action1) HystrixRuntimeException(com.netflix.hystrix.exception.HystrixRuntimeException) TimeoutException(java.util.concurrent.TimeoutException) HystrixRuntimeException(com.netflix.hystrix.exception.HystrixRuntimeException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) HystrixBadRequestException(com.netflix.hystrix.exception.HystrixBadRequestException) HystrixTimeoutException(com.netflix.hystrix.exception.HystrixTimeoutException) Notification(rx.Notification) Observable(rx.Observable) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HystrixRequestContext(com.netflix.hystrix.strategy.concurrency.HystrixRequestContext) Func1(rx.functions.Func1)

Aggregations

Action1 (rx.functions.Action1)108 Test (org.junit.Test)33 Action0 (rx.functions.Action0)28 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)24 UiThreadTest (android.support.test.annotation.UiThreadTest)20 Observable (rx.Observable)20 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)12 HystrixRuntimeException (com.netflix.hystrix.exception.HystrixRuntimeException)11 ArrayList (java.util.ArrayList)11 CountDownLatch (java.util.concurrent.CountDownLatch)11 TestSubscriber (rx.observers.TestSubscriber)10 AllTypes (io.realm.entities.AllTypes)9 List (java.util.List)9 TestCircuitBreaker (com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker)7 RunTestInLooperThread (io.realm.rule.RunTestInLooperThread)6 IOException (java.io.IOException)6 Func1 (rx.functions.Func1)6 PluginTestVerifier (com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier)5 Method (java.lang.reflect.Method)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5