use of rx.functions.Action0 in project azure-sdk-for-java by Azure.
the class FunctionAppImpl method submitAppSettings.
@Override
Observable<SiteInner> submitAppSettings(final SiteInner site) {
if (storageAccountCreatable != null && createdResource(storageAccountCreatable.key()) != null) {
storageAccountToSet = (StorageAccount) createdResource(storageAccountCreatable.key());
}
if (storageAccountToSet == null) {
return super.submitAppSettings(site);
} else {
return storageAccountToSet.getKeysAsync().flatMapIterable(new Func1<List<StorageAccountKey>, Iterable<StorageAccountKey>>() {
@Override
public Iterable<StorageAccountKey> call(List<StorageAccountKey> storageAccountKeys) {
return storageAccountKeys;
}
}).first().flatMap(new Func1<StorageAccountKey, Observable<SiteInner>>() {
@Override
public Observable<SiteInner> call(StorageAccountKey storageAccountKey) {
String connectionString = String.format("DefaultEndpointsProtocol=https;AccountName=%s;AccountKey=%s", storageAccountToSet.name(), storageAccountKey.value());
withAppSetting("AzureWebJobsStorage", connectionString);
withAppSetting("AzureWebJobsDashboard", connectionString);
withAppSetting("WEBSITE_CONTENTAZUREFILECONNECTIONSTRING", connectionString);
withAppSetting("WEBSITE_CONTENTSHARE", SdkContext.randomResourceName(name(), 32));
return FunctionAppImpl.super.submitAppSettings(site);
}
}).doOnCompleted(new Action0() {
@Override
public void call() {
currentStorageAccount = storageAccountToSet;
storageAccountToSet = null;
storageAccountCreatable = null;
}
});
}
}
use of rx.functions.Action0 in project java-chassis by ServiceComb.
the class BizkeeperHandlerDelegate method forceFallbackCommand.
protected HystrixObservable<Response> forceFallbackCommand(Invocation invocation) {
return new HystrixObservable<Response>() {
@Override
public Observable<Response> observe() {
ReplaySubject<Response> subject = ReplaySubject.create();
final Subscription sourceSubscription = toObservable().subscribe(subject);
return subject.doOnUnsubscribe(new Action0() {
@Override
public void call() {
sourceSubscription.unsubscribe();
}
});
}
@Override
public Observable<Response> toObservable() {
return Observable.create(f -> {
try {
if (Configuration.FALLBACKPOLICY_POLICY_RETURN.equals(Configuration.INSTANCE.getFallbackPolicyPolicy(handler.groupname, invocation.getMicroserviceName(), invocation.getOperationMeta().getMicroserviceQualifiedName()))) {
f.onNext(Response.succResp(null));
} else {
f.onNext(Response.failResp(invocation.getInvocationType(), BizkeeperExceptionUtils.createBizkeeperException(BizkeeperExceptionUtils.CSE_HANDLER_BK_FALLBACK, null, invocation.getOperationMeta().getMicroserviceQualifiedName())));
}
} catch (Exception e) {
f.onError(e);
}
});
}
;
};
}
use of rx.functions.Action0 in project CustomViews by AndroidStudy233.
the class MainActivity method subscriberImp.
/**
* 除了 subscribe(Observer) 和 subscribe(Subscriber) ,
* subscribe() 还支持不完整定义的回调,RxJava 会自动根据定义创建出 Subscriber 。形式如下:
*/
public void subscriberImp() {
/**
* 简单解释一下这段代码中出现的 Action1 和 Action0。 Action0 是 RxJava 的一个接口,它只有一个方法 call(),
* 这个方法是无参无返回值的;由于 onCompleted() 方法也是无参无返回值的,因此 Action0 可以被当成一个包装对象,
* 将 onCompleted() 的内容打包起来将自己作为一个参数传入 subscribe() 以实现不完整定义的回调。这样其实也可以
* 看做将 onCompleted() 方法作为参数传进了 subscribe(),相当于其他某些语言中的『闭包』。 Action1 也是一个接口,
* 它同样只有一个方法 call(T param),这个方法也无返回值,但有一个参数;与 Action0 同理,由于 onNext(T obj) 和
* onError(Throwable error) 也是单参数无返回值的,因此 Action1 可以将 onNext(obj) 和 onError(error) 打包起来传入
* subscribe() 以实现不完整定义的回调。事实上,虽然 Action0 和 Action1 在 API 中使用最广泛,
* 但 RxJava 是提供了多个 ActionX 形式的接口 (例如 Action2, Action3) 的,它们可以被用以包装不同的无返回值的方法
*/
Action1<String> action1 = new Action1<String>() {
@Override
public void call(String s) {
MyLogUtil.e("onNext" + s);
}
};
Action1<Throwable> error = new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
MyLogUtil.e("onError");
}
};
Action0 complete = new Action0() {
@Override
public void call() {
MyLogUtil.e("onComplete");
}
};
// onNext
observable.subscribe(action1);
// onNext onError
observable.subscribe(action1, error);
// onNext onError complete
observable.subscribe(action1, error, complete);
// 或者直接 observable.subscribe(observer);
}
use of rx.functions.Action0 in project Hystrix by Netflix.
the class AbstractCommand method executeCommandWithSpecifiedIsolation.
private Observable<R> executeCommandWithSpecifiedIsolation(final AbstractCommand<R> _cmd) {
if (properties.executionIsolationStrategy().get() == ExecutionIsolationStrategy.THREAD) {
// mark that we are executing in a thread (even if we end up being rejected we still were a THREAD execution and not SEMAPHORE)
return Observable.defer(new Func0<Observable<R>>() {
@Override
public Observable<R> call() {
executionResult = executionResult.setExecutionOccurred();
if (!commandState.compareAndSet(CommandState.OBSERVABLE_CHAIN_CREATED, CommandState.USER_CODE_EXECUTED)) {
return Observable.error(new IllegalStateException("execution attempted while in state : " + commandState.get().name()));
}
metrics.markCommandStart(commandKey, threadPoolKey, ExecutionIsolationStrategy.THREAD);
if (isCommandTimedOut.get() == TimedOutStatus.TIMED_OUT) {
// and not increment any of the counters below or other such logic
return Observable.error(new RuntimeException("timed out before executing run()"));
}
if (threadState.compareAndSet(ThreadState.NOT_USING_THREAD, ThreadState.STARTED)) {
// we have not been unsubscribed, so should proceed
HystrixCounters.incrementGlobalConcurrentThreads();
threadPool.markThreadExecution();
// store the command that is being run
endCurrentThreadExecutingCommand = Hystrix.startCurrentThreadExecutingCommand(getCommandKey());
executionResult = executionResult.setExecutedInThread();
/**
* If any of these hooks throw an exception, then it appears as if the actual execution threw an error
*/
try {
executionHook.onThreadStart(_cmd);
executionHook.onRunStart(_cmd);
executionHook.onExecutionStart(_cmd);
return getUserExecutionObservable(_cmd);
} catch (Throwable ex) {
return Observable.error(ex);
}
} else {
// command has already been unsubscribed, so return immediately
return Observable.empty();
}
}
}).doOnTerminate(new Action0() {
@Override
public void call() {
if (threadState.compareAndSet(ThreadState.STARTED, ThreadState.TERMINAL)) {
handleThreadEnd(_cmd);
}
if (threadState.compareAndSet(ThreadState.NOT_USING_THREAD, ThreadState.TERMINAL)) {
// if it was never started and received terminal, then no need to clean up (I don't think this is possible)
}
// if it was unsubscribed, then other cleanup handled it
}
}).doOnUnsubscribe(new Action0() {
@Override
public void call() {
if (threadState.compareAndSet(ThreadState.STARTED, ThreadState.UNSUBSCRIBED)) {
handleThreadEnd(_cmd);
}
if (threadState.compareAndSet(ThreadState.NOT_USING_THREAD, ThreadState.UNSUBSCRIBED)) {
// if it was never started and was cancelled, then no need to clean up
}
// if it was terminal, then other cleanup handled it
}
}).subscribeOn(threadPool.getScheduler(new Func0<Boolean>() {
@Override
public Boolean call() {
return properties.executionIsolationThreadInterruptOnTimeout().get() && _cmd.isCommandTimedOut.get() == TimedOutStatus.TIMED_OUT;
}
}));
} else {
return Observable.defer(new Func0<Observable<R>>() {
@Override
public Observable<R> call() {
executionResult = executionResult.setExecutionOccurred();
if (!commandState.compareAndSet(CommandState.OBSERVABLE_CHAIN_CREATED, CommandState.USER_CODE_EXECUTED)) {
return Observable.error(new IllegalStateException("execution attempted while in state : " + commandState.get().name()));
}
metrics.markCommandStart(commandKey, threadPoolKey, ExecutionIsolationStrategy.SEMAPHORE);
// semaphore isolated
// store the command that is being run
endCurrentThreadExecutingCommand = Hystrix.startCurrentThreadExecutingCommand(getCommandKey());
try {
executionHook.onRunStart(_cmd);
executionHook.onExecutionStart(_cmd);
// the getUserExecutionObservable method already wraps sync exceptions, so this shouldn't throw
return getUserExecutionObservable(_cmd);
} catch (Throwable ex) {
// If the above hooks throw, then use that as the result of the run method
return Observable.error(ex);
}
}
});
}
}
use of rx.functions.Action0 in project Hystrix by Netflix.
the class AbstractCommand method applyHystrixSemantics.
private Observable<R> applyHystrixSemantics(final AbstractCommand<R> _cmd) {
// mark that we're starting execution on the ExecutionHook
// if this hook throws an exception, then a fast-fail occurs with no fallback. No state is left inconsistent
executionHook.onStart(_cmd);
/* determine if we're allowed to execute */
if (circuitBreaker.attemptExecution()) {
final TryableSemaphore executionSemaphore = getExecutionSemaphore();
final AtomicBoolean semaphoreHasBeenReleased = new AtomicBoolean(false);
final Action0 singleSemaphoreRelease = new Action0() {
@Override
public void call() {
if (semaphoreHasBeenReleased.compareAndSet(false, true)) {
executionSemaphore.release();
}
}
};
final Action1<Throwable> markExceptionThrown = new Action1<Throwable>() {
@Override
public void call(Throwable t) {
eventNotifier.markEvent(HystrixEventType.EXCEPTION_THROWN, commandKey);
}
};
if (executionSemaphore.tryAcquire()) {
try {
/* used to track userThreadExecutionTime */
executionResult = executionResult.setInvocationStartTime(System.currentTimeMillis());
return executeCommandAndObserve(_cmd).doOnError(markExceptionThrown).doOnTerminate(singleSemaphoreRelease).doOnUnsubscribe(singleSemaphoreRelease);
} catch (RuntimeException e) {
return Observable.error(e);
}
} else {
return handleSemaphoreRejectionViaFallback();
}
} else {
return handleShortCircuitViaFallback();
}
}
Aggregations