Search in sources :

Example 11 with Action1

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

the class HttpMetaRequest method convertToRibbonResponse.

private Observable<RibbonResponse<Observable<T>>> convertToRibbonResponse(final HystrixObservableCommandChain<T> commandChain, final Observable<ResultCommandPair<T>> hystrixNotificationObservable) {
    return Observable.create(new OnSubscribe<RibbonResponse<Observable<T>>>() {

        @Override
        public void call(final Subscriber<? super RibbonResponse<Observable<T>>> t1) {
            final Subject<T, T> subject = ReplaySubject.create();
            hystrixNotificationObservable.materialize().subscribe(new Action1<Notification<ResultCommandPair<T>>>() {

                AtomicBoolean first = new AtomicBoolean(true);

                @Override
                public void call(Notification<ResultCommandPair<T>> notification) {
                    if (first.compareAndSet(true, false)) {
                        HystrixObservableCommand<T> command = notification.isOnError() ? commandChain.getLastCommand() : notification.getValue().getCommand();
                        t1.onNext(new ResponseWithSubject<T>(subject, command));
                        t1.onCompleted();
                    }
                    if (notification.isOnNext()) {
                        subject.onNext(notification.getValue().getResult());
                    } else if (notification.isOnCompleted()) {
                        subject.onCompleted();
                    } else {
                        // onError
                        subject.onError(notification.getThrowable());
                    }
                }
            });
        }
    });
}
Also used : Action1(rx.functions.Action1) Observable(rx.Observable) Subject(rx.subjects.Subject) ReplaySubject(rx.subjects.ReplaySubject) Notification(rx.Notification) RibbonResponse(com.netflix.ribbon.RibbonResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ResultCommandPair(com.netflix.ribbon.hystrix.ResultCommandPair)

Example 12 with Action1

use of rx.functions.Action1 in project azure-tools-for-java by Microsoft.

the class AzureDockerUtils method refreshDockerSubscriptions.

public static Map<String, AzureDockerSubscription> refreshDockerSubscriptions(AzureManager azureAuthManager) {
    Map<String, AzureDockerSubscription> subsMap = new HashMap<>();
    try {
        if (DEBUG)
            System.out.format("Get AzureDockerHostsManage subscription details: %s\n", new Date().toString());
        SubscriptionManager subscriptionManager = azureAuthManager.getSubscriptionManager();
        List<SubscriptionDetail> subscriptions = subscriptionManager.getSubscriptionDetails();
        if (subscriptions != null) {
            if (DEBUG)
                System.out.format("Get AzureDockerHostsManage Docker subscription details: %s\n", new Date().toString());
            Observable.from(subscriptions).flatMap(subscriptionDetail -> {
                return Observable.create(new Observable.OnSubscribe<AzureDockerSubscription>() {

                    @Override
                    public void call(Subscriber<? super AzureDockerSubscription> dockerSubscriptionSubscriber) {
                        if (subscriptionDetail.isSelected()) {
                            AzureDockerSubscription dockerSubscription = new AzureDockerSubscription();
                            dockerSubscription.id = subscriptionDetail.getSubscriptionId();
                            try {
                                if (DEBUG)
                                    System.out.format("\tGet AzureDockerHostsManage Docker subscription: %s at %s\n", dockerSubscription.id, new Date().toString());
                                dockerSubscription.tid = subscriptionDetail.getTenantId();
                                dockerSubscription.name = subscriptionDetail.getSubscriptionName();
                                dockerSubscription.azureClient = azureAuthManager.getAzure(dockerSubscription.id);
                                dockerSubscription.keyVaultClient = azureAuthManager.getKeyVaultClient(subscriptionDetail.getTenantId());
                                dockerSubscription.isSelected = true;
                                if (AzureDockerUtils.hasServicePrincipalAzureManager(azureAuthManager)) {
                                    dockerSubscription.userId = null;
                                    dockerSubscription.servicePrincipalId = azureAuthManager.getCurrentUserId();
                                } else {
                                    dockerSubscription.userId = azureAuthManager.getCurrentUserId();
                                    dockerSubscription.servicePrincipalId = null;
                                }
                                dockerSubscriptionSubscriber.onNext(dockerSubscription);
                            } catch (Exception e) {
                                e.printStackTrace();
                                DefaultLoader.getUIHelper().showError(e.getMessage(), "Error Loading Subscription Details for " + dockerSubscription.id);
                            }
                        }
                        dockerSubscriptionSubscriber.onCompleted();
                    }
                }).subscribeOn(Schedulers.io());
            }).subscribeOn(Schedulers.io()).toBlocking().subscribe(new Action1<AzureDockerSubscription>() {

                @Override
                public void call(AzureDockerSubscription dockerSubscription) {
                    subsMap.put(dockerSubscription.id, dockerSubscription);
                }
            });
        }
        if (DEBUG)
            System.out.format("Get AzureDockerHostsManage locations: %s\n", new Date().toString());
        List<Subscription> azureSubscriptionList = azureAuthManager.getSubscriptions();
        for (Subscription subscription : azureSubscriptionList) {
            AzureDockerSubscription dockerSubscription = subsMap.get(subscription.subscriptionId());
            if (dockerSubscription != null) {
                List<String> locations = subscription.listLocations().stream().sorted(Comparator.comparing(Location::displayName)).map(o -> o.name().toLowerCase()).collect(Collectors.toList());
                dockerSubscription.locations = locations;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        DefaultLoader.getUIHelper().showError(e.getMessage(), "Error loading subscription details");
    }
    return subsMap;
}
Also used : HttpURLConnection(java.net.HttpURLConnection) java.util(java.util) StorageAccount(com.microsoft.azure.management.storage.StorageAccount) URL(java.net.URL) Subnet(com.microsoft.azure.management.network.Subnet) AzureManager(com.microsoft.azuretools.sdkmanage.AzureManager) Callable(java.util.concurrent.Callable) Action1(rx.functions.Action1) Location(com.microsoft.azure.management.resources.Location) Observable(rx.Observable) Azure(com.microsoft.azure.management.Azure) Network(com.microsoft.azure.management.network.Network) Func1(rx.functions.Func1) ServicePrincipalAzureManager(com.microsoft.azuretools.sdkmanage.ServicePrincipalAzureManager) Schedulers(rx.schedulers.Schedulers) DefaultLoader(com.microsoft.tooling.msservices.components.DefaultLoader) ResourceGroup(com.microsoft.azure.management.resources.ResourceGroup) SubscriptionDetail(com.microsoft.azuretools.authmanage.models.SubscriptionDetail) Vault(com.microsoft.azure.management.keyvault.Vault) Subscription(com.microsoft.azure.management.resources.Subscription) SubscriptionManager(com.microsoft.azuretools.authmanage.SubscriptionManager) Subscriber(rx.Subscriber) AzureDockerCertVaultOps(com.microsoft.azure.docker.ops.AzureDockerCertVaultOps) AzureRegisterProviderNamespaces(com.microsoft.azuretools.utils.AzureRegisterProviderNamespaces) UnknownHostException(java.net.UnknownHostException) Collectors(java.util.stream.Collectors) Pair(com.microsoft.azuretools.utils.Pair) CountDownLatch(java.util.concurrent.CountDownLatch) Action0(rx.functions.Action0) com.microsoft.azure.docker.model(com.microsoft.azure.docker.model) AzureDockerVMOps(com.microsoft.azure.docker.ops.AzureDockerVMOps) KeyVaultClient(com.microsoft.azure.keyvault.KeyVaultClient) SubscriptionManager(com.microsoft.azuretools.authmanage.SubscriptionManager) Observable(rx.Observable) UnknownHostException(java.net.UnknownHostException) SubscriptionDetail(com.microsoft.azuretools.authmanage.models.SubscriptionDetail) Subscription(com.microsoft.azure.management.resources.Subscription)

Example 13 with Action1

use of rx.functions.Action1 in project azure-tools-for-java by Microsoft.

the class AzureDockerCertVaultOps method getVault.

public static AzureDockerCertVault getVault(AzureDockerCertVault certVault, KeyVaultClient keyVaultClient) throws AzureDockerException {
    if (certVault == null || keyVaultClient == null || certVault.uri == null) {
        throw new AzureDockerException("Unexpected argument values; azureClient, vault name and resourceGroupName cannot be null");
    }
    String vaultUri = certVault.uri;
    try {
        SecretBundle secret = keyVaultClient.getSecret(vaultUri, SECRETENTRY_DOCKERHOSTNAMES);
        if (secret != null) {
            certVault.hostName = secret.value();
        } else {
            certVault.hostName = null;
            return null;
        }
    } catch (Exception e) {
        return null;
    }
    //Execute Key Vault Secret read in parallel
    Map<String, String> secretNamesAndValueMap = new HashMap<>();
    Observable.from(DOCKERHOST_SECRETS).flatMap(secretName -> {
        return Observable.create(new Observable.OnSubscribe<Pair<String, String>>() {

            @Override
            public void call(Subscriber<? super Pair<String, String>> subscriber) {
                keyVaultClient.getSecretAsync(vaultUri, secretName, new ServiceCallback<SecretBundle>() {

                    @Override
                    public void failure(Throwable throwable) {
                        subscriber.onCompleted();
                    }

                    @Override
                    public void success(SecretBundle secretBundle) {
                        if (secretBundle != null) {
                            subscriber.onNext(new Pair<>(secretName, secretBundle.value()));
                        }
                        subscriber.onCompleted();
                    }
                });
            }
        }).subscribeOn(Schedulers.io());
    }, 5).subscribeOn(Schedulers.io()).toBlocking().subscribe(new Action1<Pair<String, String>>() {

        @Override
        public void call(Pair<String, String> secretNameAndValue) {
            secretNamesAndValueMap.put(secretNameAndValue.first(), secretNameAndValue.second());
        }
    });
    String currentSecretValue;
    currentSecretValue = secretNamesAndValueMap.get("vmUsername");
    if (currentSecretValue != null && !currentSecretValue.isEmpty()) {
        certVault.vmUsername = currentSecretValue;
    }
    currentSecretValue = secretNamesAndValueMap.get("vmPwd");
    if (currentSecretValue != null && !currentSecretValue.isEmpty()) {
        certVault.vmPwd = currentSecretValue;
    }
    currentSecretValue = secretNamesAndValueMap.get("sshKey");
    if (currentSecretValue != null && !currentSecretValue.isEmpty()) {
        certVault.sshKey = currentSecretValue;
    }
    currentSecretValue = secretNamesAndValueMap.get("sshPubKey");
    if (currentSecretValue != null && !currentSecretValue.isEmpty()) {
        certVault.sshPubKey = currentSecretValue;
    }
    currentSecretValue = secretNamesAndValueMap.get("tlsCACert");
    if (currentSecretValue != null && !currentSecretValue.isEmpty()) {
        certVault.tlsCACert = currentSecretValue;
    }
    currentSecretValue = secretNamesAndValueMap.get("tlsCAKey");
    if (currentSecretValue != null && !currentSecretValue.isEmpty()) {
        certVault.tlsCAKey = currentSecretValue;
    }
    currentSecretValue = secretNamesAndValueMap.get("tlsClientCert");
    if (currentSecretValue != null && !currentSecretValue.isEmpty()) {
        certVault.tlsClientCert = currentSecretValue;
    }
    currentSecretValue = secretNamesAndValueMap.get("tlsClientKey");
    if (currentSecretValue != null && !currentSecretValue.isEmpty()) {
        certVault.tlsClientKey = currentSecretValue;
    }
    currentSecretValue = secretNamesAndValueMap.get("tlsServerCert");
    if (currentSecretValue != null && !currentSecretValue.isEmpty()) {
        certVault.tlsServerCert = currentSecretValue;
    }
    currentSecretValue = secretNamesAndValueMap.get("tlsServerKey");
    if (currentSecretValue != null && !currentSecretValue.isEmpty()) {
        certVault.tlsServerKey = currentSecretValue;
    }
    return certVault;
}
Also used : KeyPair(com.jcraft.jsch.KeyPair) JSch(com.jcraft.jsch.JSch) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HashMap(java.util.HashMap) Action1(rx.functions.Action1) DEBUG(com.microsoft.azure.docker.ops.utils.AzureDockerUtils.DEBUG) SecretBundle(com.microsoft.azure.keyvault.models.SecretBundle) Observable(rx.Observable) Azure(com.microsoft.azure.management.Azure) Map(java.util.Map) Schedulers(rx.schedulers.Schedulers) DefaultLoader(com.microsoft.tooling.msservices.components.DefaultLoader) ResourceGroup(com.microsoft.azure.management.resources.ResourceGroup) Vault(com.microsoft.azure.management.keyvault.Vault) Subscriber(rx.Subscriber) ServiceCallback(com.microsoft.rest.ServiceCallback) Files(java.nio.file.Files) AzureDockerUtils(com.microsoft.azure.docker.ops.utils.AzureDockerUtils) FileWriter(java.io.FileWriter) Pair(com.microsoft.azuretools.utils.Pair) SetSecretRequest(com.microsoft.azure.keyvault.requests.SetSecretRequest) SecretPermissions(com.microsoft.azure.management.keyvault.SecretPermissions) List(java.util.List) AzureDockerException(com.microsoft.azure.docker.model.AzureDockerException) AzureDockerCertVault(com.microsoft.azure.docker.model.AzureDockerCertVault) Paths(java.nio.file.Paths) CloudException(com.microsoft.azure.CloudException) KeyVaultClient(com.microsoft.azure.keyvault.KeyVaultClient) HashMap(java.util.HashMap) AzureDockerException(com.microsoft.azure.docker.model.AzureDockerException) CloudException(com.microsoft.azure.CloudException) Observable(rx.Observable) SecretBundle(com.microsoft.azure.keyvault.models.SecretBundle) ServiceCallback(com.microsoft.rest.ServiceCallback) AzureDockerException(com.microsoft.azure.docker.model.AzureDockerException) KeyPair(com.jcraft.jsch.KeyPair) Pair(com.microsoft.azuretools.utils.Pair)

Example 14 with Action1

use of rx.functions.Action1 in project mosby by sockeqwe.

the class MailProviderTest method createMail.

@Test
public void createMail() {
    final Mail mail = new Mail();
    mail.id(-1);
    final int id = mailProvider.getLastMailId();
    final AtomicBoolean successCalled = new AtomicBoolean(false);
    mailProvider.addMailWithDelay(mail).subscribe(new Subscriber<Mail>() {

        @Override
        public void onCompleted() {
        }

        @Override
        public void onError(Throwable e) {
            e.printStackTrace();
            Assert.fail("error occcurred");
        }

        @Override
        public void onNext(Mail created) {
            Assert.assertEquals(mail, created);
            Assert.assertEquals(created.getId(), id + 1);
            successCalled.set(true);
            mailProvider.getMail(id).subscribe(new Action1<Mail>() {

                @Override
                public void call(Mail queried) {
                    Assert.assertEquals(queried.getId(), id);
                }
            });
        }
    });
    Assert.assertTrue(successCalled.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Action1(rx.functions.Action1) Test(org.junit.Test)

Example 15 with Action1

use of rx.functions.Action1 in project frodo by android10.

the class LogStreamObservable method get.

@Override
<T> Observable<T> get(T type) throws Throwable {
    final StopWatch stopWatch = new StopWatch();
    final Counter emittedItems = new Counter(joinPoint.getMethodName());
    return ((Observable<T>) joinPoint.proceed()).doOnSubscribe(new Action0() {

        @Override
        public void call() {
            stopWatch.start();
        }
    }).doOnNext(new Action1<T>() {

        @Override
        public void call(T value) {
            emittedItems.increment();
            messageManager.printObservableOnNextWithValue(observableInfo, value);
        }
    }).doOnError(new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            messageManager.printObservableOnError(observableInfo, throwable);
        }
    }).doOnTerminate(new Action0() {

        @Override
        public void call() {
            stopWatch.stop();
            observableInfo.setTotalExecutionTime(stopWatch.getTotalTimeMillis());
            observableInfo.setTotalEmittedItems(emittedItems.tally());
            messageManager.printObservableItemTimeInfo(observableInfo);
        }
    });
}
Also used : Action0(rx.functions.Action0) Counter(com.fernandocejas.frodo.internal.Counter) Action1(rx.functions.Action1) StopWatch(com.fernandocejas.frodo.internal.StopWatch)

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