本文整理汇总了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!")
);
}
示例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
}
示例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);
}
示例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);
}
示例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));
}
示例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));
}
示例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());
}
}
示例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);
}
示例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);
}
示例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()));
}
示例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()));
}
示例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);
}
示例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;
}
示例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));
}