當前位置: 首頁>>代碼示例>>Java>>正文


Java OnSubscribe類代碼示例

本文整理匯總了Java中rx.Observable.OnSubscribe的典型用法代碼示例。如果您正苦於以下問題:Java OnSubscribe類的具體用法?Java OnSubscribe怎麽用?Java OnSubscribe使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


OnSubscribe類屬於rx.Observable包,在下文中一共展示了OnSubscribe類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: testProducerRequestThroughTake

import rx.Observable.OnSubscribe; //導入依賴的package包/類
@Test
public void testProducerRequestThroughTake() {
    TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
    ts.requestMore(3);
    final AtomicLong requested = new AtomicLong();
    Observable.create(new OnSubscribe<Integer>() {

        @Override
        public void call(Subscriber<? super Integer> s) {
            s.setProducer(new Producer() {

                @Override
                public void request(long n) {
                    requested.set(n);
                }

            });
        }

    }).take(3).subscribe(ts);
    assertEquals(3, requested.get());
}
 
開發者ID:akarnokd,項目名稱:RxJavaFlow,代碼行數:23,代碼來源:OperatorTakeTest.java

示例2: setupHooks

import rx.Observable.OnSubscribe; //導入依賴的package包/類
private void setupHooks() {
    RxJavaHooks.setOnObservableStart(new Func2<Observable, OnSubscribe, OnSubscribe>() {
        @Override
        public OnSubscribe call(Observable observable, OnSubscribe onSubscribe) {
            incrementActiveSubscriptionsCount();
            return onSubscribe;
        }
    });

    RxJavaHooks.setOnObservableSubscribeError(new Func1<Throwable, Throwable>() {
        @Override
        public Throwable call(Throwable throwable) {
            decrementActiveSubscriptionsCount();
            return throwable;
        }
    });

    RxJavaHooks.setOnObservableReturn(new Func1<Subscription, Subscription>() {
        @Override
        public Subscription call(Subscription subscription) {
            decrementActiveSubscriptionsCount();
            return subscription;
        }
    });
}
 
開發者ID:ebridfighter,項目名稱:GongXianSheng,代碼行數:26,代碼來源:RxIdlingResource.java

示例3: get

import rx.Observable.OnSubscribe; //導入依賴的package包/類
public static Observable<HttpResponse> get(String url, Consumer<HttpGet> configureRequest) {
    HttpGet request = new HttpGet(url);
    configureRequest.accept(request);

    return Observable.create(new OnSubscribe<HttpResponse>() {
        @Override public void call(Subscriber<? super HttpResponse> s) {
            try {
                log.debug("GET {}", url);
                s.onNext(getDefaultHttpClient().execute(request));
                s.onCompleted();
            } catch (IOException e) {
                s.onError(e);
            }
        }
    }).subscribeOn(Schedulers.io());
}
 
開發者ID:lukstei,項目名稱:trading-backtest,代碼行數:17,代碼來源:Http.java

示例4: updateCollectionDAO

import rx.Observable.OnSubscribe; //導入依賴的package包/類
private void updateCollectionDAO(String collectionName, String documentID, String updatedValuesJson, String removedValuesJson) {
    Observable.create(new OnSubscribe<CollectionDAO>() {
        @Override
        public void call(Subscriber<? super CollectionDAO> subscriber) {
            try {
                final CollectionDAO dao = CollectionDAO.query(collectionName, documentID);
                if (dao != null) {
                    dao.plusUpdatedValues(updatedValuesJson).lessUpdatedValues(removedValuesJson);
                }
                subscriber.onNext(dao);
                subscriber.onCompleted();
            } catch (Exception e) {
                subscriber.onError(e);
            }
        }
    })
            .subscribeOn(Schedulers.computation())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(dao -> {
                if (dao != null) {
                    dao.update();
                } else {
                    new CollectionDAO(collectionName, documentID, updatedValuesJson).insert();
                }
            });
}
 
開發者ID:lukamarin,項目名稱:Rocket.Chat-android,代碼行數:27,代碼來源:RocketApp.java

示例5: updateRcSubscription

import rx.Observable.OnSubscribe; //導入依賴的package包/類
private void updateRcSubscription(final String documentID, final String updatedValuesJson) {
    Observable.create(new OnSubscribe<RcSubscriptionDAO>() {
        @Override
        public void call(Subscriber<? super RcSubscriptionDAO> subscriber) {
            RcSubscriptionDAO rcSub = RcSubscriptionDAO.get(documentID);
            subscriber.onNext(rcSub);
            subscriber.onCompleted();
        }
    })
            .subscribeOn(Schedulers.computation())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(rcSub -> {
                if (rcSub != null) {
                    rcSub.update(documentID, updatedValuesJson);
                }
            });
}
 
開發者ID:lukamarin,項目名稱:Rocket.Chat-android,代碼行數:18,代碼來源:RocketApp.java

示例6: fromCallable

import rx.Observable.OnSubscribe; //導入依賴的package包/類
/** Method fromCallable is only 1.0.15. */
static <T> Observable<T> fromCallable(final Callable<T> call) {
    return Observable.create(new OnSubscribe<T>() {
        @Override
        public void call(Subscriber<? super T> t) {
            SingleDelayedProducer<T> p = new SingleDelayedProducer<T>(t);
            t.setProducer(p);
            T v;
            try {
                v = call.call();
            } catch (Throwable ex) {
                t.onError(ex);
                return;
            }
            p.setValue(v);
        }
    }); 
}
 
開發者ID:akarnokd,項目名稱:RxJavaCompletable,代碼行數:19,代碼來源:CompletableTest.java

示例7: registerFunctionOnLifecycle

import rx.Observable.OnSubscribe; //導入依賴的package包/類
private void registerFunctionOnLifecycle(ILifecycleProvider lifecycleProvider,
                                         final OnSubscribe<Object> executable,
                                         ActivityLifecycle activityLifecycle,
                                         FragmentLifecycle fragmentLifecycle,
                                         DialogFragmentLifecycle dialogFragmentLifecycle) {
    Observable wrapper = Observable.defer(new Func0<Observable<Object>>() {
        @Override
        public Observable<Object> call() {
            return Observable.create(executable);
        }
    });

    if (lifecycleProvider instanceof BaseActivity
        && activityLifecycle != ActivityLifecycle.NULL) {
        lifecycleProvider.executeWhen(wrapper, activityLifecycle);
    } else if (lifecycleProvider instanceof BaseFragment
        && fragmentLifecycle != FragmentLifecycle.NULL) {
        lifecycleProvider.executeWhen(wrapper, fragmentLifecycle);
    } else if (lifecycleProvider instanceof BaseDialogFragment
        && dialogFragmentLifecycle != DialogFragmentLifecycle.NULL) {
        lifecycleProvider.executeWhen(wrapper, dialogFragmentLifecycle);
    }
}
 
開發者ID:wingjay,項目名稱:jayAndroid,代碼行數:24,代碼來源:AutoLifecycle.java

示例8: submitRoutedRequest

import rx.Observable.OnSubscribe; //導入依賴的package包/類
@Override
public Observable<AstrixServiceInvocationResponse> submitRoutedRequest(
		final AstrixServiceInvocationRequest request, final RoutingKey routingKey) {
	ClusterMember clusterMember = getTargetMember(routingKey);
	final HttpPost postRequest = new HttpPost(clusterMember.getRemoteEndpointUri());
	postRequest.setEntity(new SerializableEntity(request));
	return Observable.create(new OnSubscribe<AstrixServiceInvocationResponse>() {
		@Override
		public void call(final Subscriber<? super AstrixServiceInvocationResponse> t1) {
			try {
				httpclient.execute(postRequest, serviceResponseCallback(t1));
			} catch (Exception e) {
				t1.onError(e);
			}
		}

	});
}
 
開發者ID:AvanzaBank,項目名稱:astrix,代碼行數:19,代碼來源:HttpRemotingTransport.java

示例9: submitBroadcastRequest

import rx.Observable.OnSubscribe; //導入依賴的package包/類
@Override
public Observable<List<AstrixServiceInvocationResponse>> submitBroadcastRequest(
		AstrixServiceInvocationRequest request) {
	Observable<AstrixServiceInvocationResponse> result = Observable.empty();
	for (ClusterMember clusterMember : getAllClusterMembers()) {
		final HttpPost postRequest = new HttpPost(clusterMember.getRemoteEndpointUri());
		postRequest.setEntity(new SerializableEntity(request));
		result = result.mergeWith(Observable.create(new OnSubscribe<AstrixServiceInvocationResponse>() {
			@Override
			public void call(final Subscriber<? super AstrixServiceInvocationResponse> t1) {
				try {
					httpclient.execute(postRequest, serviceResponseCallback(t1));
				} catch (Exception e) {
					t1.onError(e);
				}
			}

		}));
	}
	return result.toList();
}
 
開發者ID:AvanzaBank,項目名稱:astrix,代碼行數:22,代碼來源:HttpRemotingTransport.java

示例10: throwsServiceUnavailableOnTimeouts

import rx.Observable.OnSubscribe; //導入依賴的package包/類
@Test
public void throwsServiceUnavailableOnTimeouts() throws Exception {
	pingServer.setResult(Observable.create(new OnSubscribe<String>() {
		@Override
		public void call(Subscriber<? super String> t1) {
			// Simulate timeout by not invoking subscriber
		}
	}));
	try {
		ping.ping().toBlocking().first();
		fail("All ServiceUnavailableException should be thrown on timeout");
	} catch (ServiceUnavailableException e) {
		// Expcected
	}
	eventually(() -> {
		assertEquals(0, getEventCountForCommand(HystrixRollingNumberEvent.SUCCESS));
		assertEquals(1, getEventCountForCommand(HystrixRollingNumberEvent.TIMEOUT));
		assertEquals(0, getEventCountForCommand(HystrixRollingNumberEvent.SEMAPHORE_REJECTED));
	});
}
 
開發者ID:AvanzaBank,項目名稱:astrix,代碼行數:21,代碼來源:HystrixObservableCommandFacadeTest.java

示例11: semaphoreRejectedCountsAsFailure

import rx.Observable.OnSubscribe; //導入依賴的package包/類
@Test
public void semaphoreRejectedCountsAsFailure() throws Exception {
	pingServer.setResult(Observable.create(new OnSubscribe<String>() {
		@Override
		public void call(Subscriber<? super String> t1) {
			// Simulate timeout by not invoking subscriber
		}
	}));
	Observable<String> ftObservable1 = ping.ping();
	Observable<String> ftObservable2 = ping.ping();

	// Subscribe to observables, ignore emitted items/errors
	ftObservable1.subscribe((item) -> {}, (exception) -> {});
	ftObservable2.subscribe((item) -> {}, (exception) -> {});

	eventually(() -> {
		assertEquals(0, getEventCountForCommand(HystrixRollingNumberEvent.SUCCESS));
		assertEquals(1, getEventCountForCommand(HystrixRollingNumberEvent.SEMAPHORE_REJECTED));
	});
}
 
開發者ID:AvanzaBank,項目名稱:astrix,代碼行數:21,代碼來源:HystrixObservableCommandFacadeTest.java

示例12: doesNotInvokeSupplierWhenBulkHeadIsFull

import rx.Observable.OnSubscribe; //導入依賴的package包/類
@Test
public void doesNotInvokeSupplierWhenBulkHeadIsFull() throws Exception {
	final AtomicInteger supplierInvocationCount = new AtomicInteger();
	Supplier<Observable<String>> timeoutCommandSupplier = new Supplier<Observable<String>>() {
		@Override
		public Observable<String> get() {
			supplierInvocationCount.incrementAndGet();
			return Observable.create(new OnSubscribe<String>() {
				@Override
				public void call(Subscriber<? super String> t1) {
					// Simulate timeout by not invoking subscriber
				}
			});
		}
	};
	Observable<String> ftObservable1 = HystrixObservableCommandFacade.observe(timeoutCommandSupplier, commandSettings);
	final Observable<String> ftObservable2 = HystrixObservableCommandFacade.observe(timeoutCommandSupplier, commandSettings);
	
	ftObservable1.subscribe(); // Ignore
	
	assertEquals(1, supplierInvocationCount.get());
	AstrixTestUtil.serviceInvocationException(() -> ftObservable2.toBlocking().first(), AstrixTestUtil.isExceptionOfType(ServiceUnavailableException.class));
	assertEquals(1, supplierInvocationCount.get());
}
 
開發者ID:AvanzaBank,項目名稱:astrix,代碼行數:25,代碼來源:HystrixObservableCommandFacadeTest.java

示例13: asyncResultListToObservable

import rx.Observable.OnSubscribe; //導入依賴的package包/類
public static <T> Func1<List<AsyncResult<T>>, Observable<T>> asyncResultListToObservable() {
	return new Func1<List<AsyncResult<T>>, Observable<T>>() {
		@Override
		public Observable<T> call(final List<AsyncResult<T>> asyncRresults) {
			return Observable.create(new OnSubscribe<T>() {
				@Override
				public void call(Subscriber<? super T> subscriber) {
					for (AsyncResult<T> asyncInvocationResponse : asyncRresults) {
						if (asyncInvocationResponse.getException() != null) {
							subscriber.onError(asyncInvocationResponse.getException());
							return;
						}
						subscriber.onNext(asyncInvocationResponse.getResult());
					}
					subscriber.onCompleted();
				}
			});
		}};
}
 
開發者ID:AvanzaBank,項目名稱:astrix,代碼行數:20,代碼來源:GsUtil.java

示例14: getMessages

import rx.Observable.OnSubscribe; //導入依賴的package包/類
public static Observable<ActorMessage<?>> getMessages(final Queue<ActorMessage<?>> stash) {
	return Observable.create(new OnSubscribe<ActorMessage<?>>() {
		@Override
		public void call(final Subscriber<? super ActorMessage<?>> subscriber) {
			subscriber.setProducer(new Producer() {
				@Override
				public void request(long count) {
					try {
						ActorMessage<?> message;
						for (int i=0; i<count && (message=stash.poll())!=null; i++) 
							subscriber.onNext(message);
						subscriber.onCompleted();
					}
					catch (Exception e) {
						subscriber.onError(e);
					}
				}
			});
		}
	});
}
 
開發者ID:relvaner,項目名稱:actor4j-core,代碼行數:22,代碼來源:ActorMessageObservable.java

示例15: testSingleOrDefaultUnsubscribe

import rx.Observable.OnSubscribe; //導入依賴的package包/類
@Test
public void testSingleOrDefaultUnsubscribe() throws InterruptedException {
    final CountDownLatch unsubscribe = new CountDownLatch(1);
    Observable<Integer> o = Observable.create(new OnSubscribe<Integer>() {
        @Override
        public void call(Subscriber<? super Integer> subscriber) {
            subscriber.add(Subscriptions.create(new Action0() {
                @Override
                public void call() {
                    unsubscribe.countDown();
                }
            }));
            subscriber.onNext(1);
            subscriber.onNext(2);
            // Don't call `onComplete()` to emulate an infinite stream
        }
    }).subscribeOn(Schedulers.newThread());
    try {
        o.toBlocking().singleOrDefault(-1);
        fail("Expected IllegalArgumentException because there are 2 elements");
    } catch (IllegalArgumentException e) {
        // Expected
    }
    assertTrue("Timeout means `unsubscribe` is not called", unsubscribe.await(30, TimeUnit.SECONDS));
}
 
開發者ID:akarnokd,項目名稱:RxJavaFlow,代碼行數:26,代碼來源:BlockingObservableTest.java


注:本文中的rx.Observable.OnSubscribe類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。