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


Java Subject.subscribe方法代碼示例

本文整理匯總了Java中io.reactivex.subjects.Subject.subscribe方法的典型用法代碼示例。如果您正苦於以下問題:Java Subject.subscribe方法的具體用法?Java Subject.subscribe怎麽用?Java Subject.subscribe使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在io.reactivex.subjects.Subject的用法示例。


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

示例1: main

import io.reactivex.subjects.Subject; //導入方法依賴的package包/類
public static void main(String[] args) {
    Subject<String> subject =
            AsyncSubject.create();
    subject.subscribe(s ->
                    System.out.println("Observer 1: " + s),
            Throwable::printStackTrace,
            () -> System.out.println("Observer 1 done!")
    );
    subject.onNext("Alpha");
    subject.onNext("Beta");
    subject.onNext("Gamma");
    subject.onComplete();
    subject.subscribe(s ->
                    System.out.println("Observer 2: " + s),
            Throwable::printStackTrace,
            () -> System.out.println("Observer 2 done!")
    );
}
 
開發者ID:PacktPublishing,項目名稱:Learning-RxJava,代碼行數:19,代碼來源:Ch5_25.java

示例2: testExecuteEvaluatesJobInputsAsExpected

import io.reactivex.subjects.Subject; //導入方法依賴的package包/類
@Test
public void testExecuteEvaluatesJobInputsAsExpected() throws InterruptedException {
    final JobExecutor jobExecutor = getInstance();
    final PersistedJob req =
            standardRequestWithCommand("echo", "${inputs.foo}");
    final AtomicReference<byte[]> bytesEchoedToStdout = new AtomicReference<>(new byte[]{});
    final Subject<byte[]> stdoutSubject = PublishSubject.create();

    stdoutSubject.subscribe(bytes ->
            bytesEchoedToStdout.getAndUpdate(existingBytes ->
                    Bytes.concat(existingBytes, bytes)));

    final Semaphore s = new Semaphore(1);
    s.acquire();
    stdoutSubject.doOnComplete(s::release).subscribe();

    final JobEventListeners listeners =
            createStdoutListener(stdoutSubject);

    jobExecutor.execute(req, listeners);

    s.tryAcquire(TestConstants.DEFAULT_TIMEOUT, MILLISECONDS);

    final String stringFromStdout = new String(bytesEchoedToStdout.get()).trim();
    assertThat(stringFromStdout).isEqualTo("a"); // from spec
}
 
開發者ID:adamkewley,項目名稱:jobson,代碼行數:27,代碼來源:JobExecutorTest.java

示例3: main

import io.reactivex.subjects.Subject; //導入方法依賴的package包/類
public static void main(String[] args) {
    Subject<String> subject =
            UnicastSubject.create();
    Observable.interval(300, TimeUnit.MILLISECONDS)
            .map(l -> ((l + 1) * 300) + " milliseconds")
            .subscribe(subject);
    sleep(2000);
    subject.subscribe(s -> System.out.println("Observer 1: " +
            s));
    sleep(2000);
}
 
開發者ID:PacktPublishing,項目名稱:Learning-RxJava,代碼行數:12,代碼來源:Ch5_26.java

示例4: main

import io.reactivex.subjects.Subject; //導入方法依賴的package包/類
public static void main(String[] args) {
    Observable<String> source1 =
            Observable.interval(1, TimeUnit.SECONDS)
                    .map(l -> (l + 1) + " seconds");
    Observable<String> source2 =
            Observable.interval(300, TimeUnit.MILLISECONDS)
                    .map(l -> ((l + 1) * 300) + " milliseconds");
    Subject<String> subject = PublishSubject.create();
    subject.subscribe(System.out::println);
    source1.subscribe(subject);
    source2.subscribe(subject);
    sleep(3000);
}
 
開發者ID:PacktPublishing,項目名稱:Learning-RxJava,代碼行數:14,代碼來源:Ch5_21.java

示例5: main

import io.reactivex.subjects.Subject; //導入方法依賴的package包/類
public static void main(String[] args) {
    Subject<String> subject =
            ReplaySubject.create();
    subject.subscribe(s -> System.out.println("Observer 1: " +
            s));
    subject.onNext("Alpha");
    subject.onNext("Beta");
    subject.onNext("Gamma");
    subject.onComplete();
    subject.subscribe(s -> System.out.println("Observer 2: " +
            s));
}
 
開發者ID:PacktPublishing,項目名稱:Learning-RxJava,代碼行數:13,代碼來源:Ch5_24.java

示例6: main

import io.reactivex.subjects.Subject; //導入方法依賴的package包/類
public static void main(String[] args) {
    Subject<String> subject =
            BehaviorSubject.create();
    subject.subscribe(s -> System.out.println("Observer 1: " +
            s));
    subject.onNext("Alpha");
    subject.onNext("Beta");
    subject.onNext("Gamma");
    subject.subscribe(s -> System.out.println("Observer 2: " +
            s));
}
 
開發者ID:PacktPublishing,項目名稱:Learning-RxJava,代碼行數:12,代碼來源:Ch5_23.java

示例7: useSubject

import io.reactivex.subjects.Subject; //導入方法依賴的package包/類
/**
 * Subjects can be both observables and observers
 * Registration of the subscribers is important, a observer is only be notified if
 * a new events occurs
 * */
@Test
public void useSubject() {
	Subject<String> subject = PublishSubject.<String>create().toSerialized();
	subject.subscribe(e-> result+=e);
	subject.onNext("Hello");
	subject.onNext("Man");
	subject.onNext("Test");

	subject.subscribe(e-> doIt());
	subject.onNext("Test");
	assertThat(result).isEqualTo(_____);

}
 
開發者ID:vogellacompany,項目名稱:code-examples-android-expert,代碼行數:19,代碼來源:lessonA_CreatingObservableStreams.java

示例8: advanceJobQueue

import io.reactivex.subjects.Subject; //導入方法依賴的package包/類
private void advanceJobQueue() {
    final QueuedJob queuedJob = jobQueue.poll();

    if (queuedJob == null) return;

    final Subject<byte[]> stdout = PublishSubject.create();
    final Subject<byte[]> stderr = PublishSubject.create();

    jobDAO.appendStdout(queuedJob.getId(), stdout);
    jobDAO.appendStderr(queuedJob.getId(), stderr);

    stdout.subscribe(queuedJob.getQueuedListeners().getOnStdoutListener());
    stderr.subscribe(queuedJob.getQueuedListeners().getOnStderrListener());

    try {
        final CancelablePromise<JobExecutionResult> executionPromise =
                jobExecutor.execute(queuedJob, JobEventListeners.create(stdout, stderr));

        final ExecutingJob executingJob =
                ExecutingJob.fromQueuedJob(queuedJob, now(), stdout, stderr);

        executingJobs.put(executingJob.getId(), executingJob);

        updateJobStatus(queuedJob.getId(), RUNNING, "Submitted to executor");

        executionPromise.thenAccept(res -> {
            onExecutionFinished(executingJob, res);
        });

        executingJob.getCompletionPromise().onCancel(() -> {
            executionPromise.cancel(true);
        });
    } catch (Throwable ex) {
        log.error("Error starting job execution: " + ex.toString());
        updateJobStatus(queuedJob.getId(), FATAL_ERROR, "Error executing job: " + ex.toString());
    }
}
 
開發者ID:adamkewley,項目名稱:jobson,代碼行數:38,代碼來源:JobManager.java

示例9: testExecuteWritesStdoutToTheStdoutListener

import io.reactivex.subjects.Subject; //導入方法依賴的package包/類
@Test
public void testExecuteWritesStdoutToTheStdoutListener() throws Throwable {
    final JobExecutor jobExecutor = getInstance();
    final String msgSuppliedToEcho = generateRandomString();
    final PersistedJob req =
            standardRequestWithCommand("echo", msgSuppliedToEcho);
    final AtomicReference<byte[]> bytesEchoedToStdout = new AtomicReference<>(new byte[]{});
    final Subject<byte[]> stdoutSubject = PublishSubject.create();

    stdoutSubject.subscribe(bytes ->
            bytesEchoedToStdout.getAndUpdate(existingBytes ->
                    Bytes.concat(existingBytes, bytes)));

    final Semaphore s = new Semaphore(1);
    s.acquire();
    stdoutSubject.doOnComplete(s::release).subscribe();

    final JobEventListeners listeners =
            createStdoutListener(stdoutSubject);

    jobExecutor.execute(req, listeners);

    s.tryAcquire(TestConstants.DEFAULT_TIMEOUT, MILLISECONDS);

    final String stringFromStdout = new String(bytesEchoedToStdout.get()).trim();
    assertThat(stringFromStdout).isEqualTo(msgSuppliedToEcho);
}
 
開發者ID:adamkewley,項目名稱:jobson,代碼行數:28,代碼來源:JobExecutorTest.java

示例10: testExecuteWritesStderrToTheStderrListener

import io.reactivex.subjects.Subject; //導入方法依賴的package包/類
@Test
public void testExecuteWritesStderrToTheStderrListener() throws Throwable {
    final JobExecutor jobExecutor = getInstance();
    final String msgSuppliedToEcho = generateRandomString();
    final String bashArg = "echo " + msgSuppliedToEcho + " 1>&2"; // TODO: Naughty.
    final PersistedJob req =
            standardRequestWithCommand("bash", "-c", bashArg);
    final AtomicReference<byte[]> bytesEchoedToStderr = new AtomicReference<>(new byte[]{});
    final Subject<byte[]> stderrSubject = PublishSubject.create();

    stderrSubject.subscribe(bytes ->
            bytesEchoedToStderr.getAndUpdate(existingBytes ->
                    Bytes.concat(existingBytes, bytes)));

    final Semaphore s = new Semaphore(1);
    s.acquire();
    stderrSubject.doOnComplete(s::release).subscribe();

    final JobEventListeners listeners =
            createStderrListener(stderrSubject);

    jobExecutor.execute(req, listeners);

    s.tryAcquire(TestConstants.DEFAULT_TIMEOUT, MILLISECONDS);

    final String stringFromStderr = new String(bytesEchoedToStderr.get()).trim();
    assertThat(stringFromStderr).isEqualTo(msgSuppliedToEcho);
}
 
開發者ID:adamkewley,項目名稱:jobson,代碼行數:29,代碼來源:JobExecutorTest.java

示例11: testExecuteEvaluatesToJSONFunctionAsExpected

import io.reactivex.subjects.Subject; //導入方法依賴的package包/類
@Test
public void testExecuteEvaluatesToJSONFunctionAsExpected() throws InterruptedException {
    final JobExecutor jobExecutor = getInstance();
    final PersistedJob req =
            standardRequestWithCommand("echo", "${toJSON(inputs)}");
    final AtomicReference<byte[]> bytesEchoedToStdout = new AtomicReference<>(new byte[]{});
    final Subject<byte[]> stdoutSubject = PublishSubject.create();

    stdoutSubject.subscribe(bytes ->
            bytesEchoedToStdout.getAndUpdate(existingBytes ->
                    Bytes.concat(existingBytes, bytes)));

    final Semaphore s = new Semaphore(1);
    s.acquire();
    stdoutSubject.doOnComplete(s::release).subscribe();

    final JobEventListeners listeners =
            createStdoutListener(stdoutSubject);

    jobExecutor.execute(req, listeners);

    s.tryAcquire(TestConstants.DEFAULT_TIMEOUT, MILLISECONDS);

    final String stringFromStdout = new String(bytesEchoedToStdout.get()).trim();

    assertThat(stringFromStdout).isEqualTo(toJSON(STANDARD_VALID_REQUEST.getInputs()));
}
 
開發者ID:adamkewley,項目名稱:jobson,代碼行數:28,代碼來源:JobExecutorTest.java

示例12: testExecuteEvaluatesToFileAsExpected

import io.reactivex.subjects.Subject; //導入方法依賴的package包/類
@Test
public void testExecuteEvaluatesToFileAsExpected() throws InterruptedException, IOException {
    final JobExecutor jobExecutor = getInstance();
    final PersistedJob req =
            standardRequestWithCommand("echo", "${toFile(toJSON(inputs))}");
    final AtomicReference<byte[]> bytesEchoedToStdout = new AtomicReference<>(new byte[]{});
    final Subject<byte[]> stdoutSubject = PublishSubject.create();

    stdoutSubject.subscribe(bytes ->
            bytesEchoedToStdout.getAndUpdate(existingBytes ->
                    Bytes.concat(existingBytes, bytes)));

    final Semaphore s = new Semaphore(1);
    s.acquire();
    stdoutSubject.doOnComplete(s::release).subscribe();

    final JobEventListeners listeners =
            createStdoutListener(stdoutSubject);

    jobExecutor.execute(req, listeners);

    s.tryAcquire(TestConstants.DEFAULT_TIMEOUT, MILLISECONDS);

    final String stringFromStdout = new String(bytesEchoedToStdout.get()).trim();
    final Path p = Paths.get(stringFromStdout);

    assertThat(p.toFile().exists());

    final String loadedJson = new String(Files.readAllBytes(p));

    assertThat(loadedJson).isEqualTo(toJSON(STANDARD_VALID_REQUEST.getInputs()));
}
 
開發者ID:adamkewley,項目名稱:jobson,代碼行數:33,代碼來源:JobExecutorTest.java

示例13: demo0

import io.reactivex.subjects.Subject; //導入方法依賴的package包/類
private static void demo0() {
    Subject<String> subject = null;

    subject.subscribe(v -> log(v));

    Observable.just("1")
            .subscribe(subject);
}
 
開發者ID:PacktPublishing,項目名稱:Reactive-Android-Programming,代碼行數:9,代碼來源:Sandbox.java

示例14: subscribeTo

import io.reactivex.subjects.Subject; //導入方法依賴的package包/類
/**
 * Subscribe this method observer to a Subject
 *
 * @param subject Subject to be observed
 * @return Subscription
 */
public Disposable subscribeTo(@NonNull Subject<T> subject) {
    if (mDisposable == null) {
        subject.subscribe(this);
    }

    return mDisposable;
}
 
開發者ID:analizer1,項目名稱:RxBusLib,代碼行數:14,代碼來源:SourceMethod.java

示例15: demo5

import io.reactivex.subjects.Subject; //導入方法依賴的package包/類
private static void demo5() throws InterruptedException {
    Subject<String> subject = AsyncSubject.create();

    Observable.interval(0, 1, TimeUnit.SECONDS)
            .take(4)
            .map(Objects::toString)
            .subscribe(subject);

    subject.subscribe(v -> log(v));

    Thread.sleep(5100);


    subject.subscribe(v -> log(v));
}
 
開發者ID:PacktPublishing,項目名稱:Reactive-Android-Programming,代碼行數:16,代碼來源:Sandbox.java


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