本文整理汇总了Java中io.reactivex.observables.ConnectableObservable.subscribe方法的典型用法代码示例。如果您正苦于以下问题:Java ConnectableObservable.subscribe方法的具体用法?Java ConnectableObservable.subscribe怎么用?Java ConnectableObservable.subscribe使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.reactivex.observables.ConnectableObservable
的用法示例。
在下文中一共展示了ConnectableObservable.subscribe方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: doSomeWork
import io.reactivex.observables.ConnectableObservable; //导入方法依赖的package包/类
private void doSomeWork() {
PublishSubject<Integer> source = PublishSubject.create();
ConnectableObservable<Integer> connectableObservable = source.replay(3); // bufferSize = 3 to retain 3 values to replay
connectableObservable.connect(); // connecting the connectableObservable
connectableObservable.subscribe(getFirstObserver());
source.onNext(1);
source.onNext(2);
source.onNext(3);
source.onNext(4);
source.onComplete();
/*
* it will emit 2, 3, 4 as (count = 3), retains the 3 values for replay
*/
connectableObservable.subscribe(getSecondObserver());
}
示例2: main
import io.reactivex.observables.ConnectableObservable; //导入方法依赖的package包/类
public static void main(String[] args) {
ConnectableObservable<Integer> threeRandoms =
Observable.range(1, 3)
.map(i -> randomInt()).publish();
//Observer 1 - print each random integer
threeRandoms.subscribe(i -> System.out.println("Observer 1: " + i));
//Observer 2 - sum the random integers, then print
threeRandoms.reduce(0, (total, next) -> total + next)
.subscribe(i -> System.out.println("Observer 2: " + i));
threeRandoms.connect();
}
示例3: main
import io.reactivex.observables.ConnectableObservable; //导入方法依赖的package包/类
public static void main(String[] args) {
ConnectableObservable<Integer> threeRandoms =
Observable.range(1, 3)
.map(i -> randomInt()).publish();
threeRandoms.subscribe(i -> System.out.println("Observer 1: " + i));
threeRandoms.subscribe(i -> System.out.println("Observer 2: " + i));
threeRandoms.connect();
}
示例4: main
import io.reactivex.observables.ConnectableObservable; //导入方法依赖的package包/类
public static void main(String[] args) {
ConnectableObservable<Integer> threeIntegers =
Observable.range(1, 3).publish();
threeIntegers.subscribe(i -> System.out.println("Observer One:" + i));
threeIntegers.subscribe(i -> System.out.println("Observer Two:" + i));
threeIntegers.connect();
}
示例5: main
import io.reactivex.observables.ConnectableObservable; //导入方法依赖的package包/类
public static void main(String[] args) {
ConnectableObservable<Integer> threeRandoms =
Observable.range(1, 3)
.map(i -> randomInt()).publish();
threeRandoms.subscribe(i -> System.out.println("Observer 1: " + i));
threeRandoms.subscribe(i -> System.out.println("Observer 2: " + i));
threeRandoms.connect();
}
示例6: main
import io.reactivex.observables.ConnectableObservable; //导入方法依赖的package包/类
public static void main(String[] args) {
ConnectableObservable<Integer> threeRandoms =
Observable.range(1, 3)
.map(i -> randomInt()).publish();
//Observer 1 - print each random integer
threeRandoms.subscribe(i -> System.out.println("Observer 1: " + i));
//Observer 2 - sum the random integers, then print
threeRandoms.reduce(0, (total, next) -> total + next)
.subscribe(i -> System.out.println("Observer 2: " + i));
threeRandoms.connect();
}
示例7: main
import io.reactivex.observables.ConnectableObservable; //导入方法依赖的package包/类
public static void main(String[] args) {
ConnectableObservable<Long> seconds =
Observable.interval(1, TimeUnit.SECONDS).publish();
//observer 1
seconds.subscribe(l -> System.out.println("Observer 1: " + l));
seconds.connect();
//sleep 5 seconds
sleep(5000);
//observer 2
seconds.subscribe(l -> System.out.println("Observer 2: " + l));
//sleep 5 seconds
sleep(5000);
}
示例8: main
import io.reactivex.observables.ConnectableObservable; //导入方法依赖的package包/类
public static void main(String[] args) {
ConnectableObservable<String> source =
Observable.just("Alpha", "Beta", "Gamma", "Delta", "Epsilon")
.publish();
//Set up observer 1
source.subscribe(s -> System.out.println("Observer 1: " + s));
//Set up observer 2
source.map(String::length)
.subscribe(i -> System.out.println("Observer 2: " + i));
//Fire!
source.connect();
}
示例9: freeFlowEmps
import io.reactivex.observables.ConnectableObservable; //导入方法依赖的package包/类
@Override
public ConnectableObservable<String> freeFlowEmps() {
List<String> rosterNames = new ArrayList<>();
Function<Employee, String> familyNames = (emp) -> emp.getLastName().toUpperCase();
ConnectableObservable<String> flowyNames = Observable.fromIterable(employeeDaoImpl.getEmployees()).map(familyNames).cache().publish();
flowyNames.subscribe(System.out::println);
flowyNames.subscribe((name) ->{
rosterNames.add(name);
});
System.out.println(rosterNames);
return flowyNames;
}
示例10: testHotObservable
import io.reactivex.observables.ConnectableObservable; //导入方法依赖的package包/类
@Test
public void testHotObservable() throws Exception {
ConnectableObservable<Long> connectableObservable = Observable.interval(1,
TimeUnit.SECONDS,
Schedulers.from(executorService)).publish();
connectableObservable.subscribe(x -> System.out.println("L1:" + x));
Thread.sleep(2000);
Disposable disposable = connectableObservable.connect(); //Let us begin
connectableObservable.subscribe(x -> System.out.println("L2:" + x));
Thread.sleep(2000);
connectableObservable.subscribe(x -> System.out.println("L3:" + x));
disposable.dispose();
Thread.sleep(10000);
}
示例11: startPlayback
import io.reactivex.observables.ConnectableObservable; //导入方法依赖的package包/类
private ConnectableObservable<PositionedPlaybackFile> startPlayback(PreparedPlayableFileQueue preparedPlaybackQueue, final long filePosition) throws IOException {
if (playbackSubscription != null)
playbackSubscription.dispose();
activePlayer = playbackBootstrapper.startPlayback(preparedPlaybackQueue, filePosition);
isPlaying = true;
final ConnectableObservable<PositionedPlaybackFile> observable = activePlayer.observe();
playbackSubscription = observable.subscribe(
p -> {
isPlaying = true;
positionedPlaybackFile = p;
if (onPlayingFileChanged != null)
onPlayingFileChanged.onPlayingFileChanged(p);
saveStateToLibrary(p);
},
e -> {
if (e instanceof PreparationException) {
final PreparationException preparationException =
(PreparationException)e;
saveStateToLibrary(
new PositionedPlaybackFile(
new EmptyPlaybackHandler(0),
preparationException.getPositionedFile()));
}
if (onPlaylistError != null)
onPlaylistError.runWith(e);
},
() -> {
isPlaying = false;
positionedPlaybackFile = null;
activePlayer = null;
changePosition(0, 0)
.then(positionedFile -> {
if (onPlaylistReset != null)
onPlaylistReset.onPlaylistReset(positionedFile);
if (onPlaybackCompleted != null)
onPlaybackCompleted.onPlaybackCompleted();
return null;
});
});
observable.firstElement()
.subscribe(
p -> {
if (onPlaybackStarted != null)
onPlaybackStarted.onPlaybackStarted(p);
},
e -> {});
return observable;
}
示例12: main
import io.reactivex.observables.ConnectableObservable; //导入方法依赖的package包/类
public static void main(String[] args) {
ConnectableObservable observable = Observable.create(observer -> {
observer.onNext("I am Hot Observable "+Math.random()*100);
observer.onComplete();
}).publish();
observable.subscribe(consumer -> System.out.println("message:-" + consumer));
observable.subscribe(consumer -> System.out.println("message:-" + consumer));
observable.connect();
}
示例13: main
import io.reactivex.observables.ConnectableObservable; //导入方法依赖的package包/类
public static void main(String[] args) throws InterruptedException{
ConnectableObservable<Long> numbers = (ConnectableObservable<Long>) Observable
.interval(1, TimeUnit.SECONDS) // generate numbers
.publish(); // make it hot
numbers.connect(); // create internal subscribtion
Disposable subscriber1 = numbers
.subscribe(n ->System.out.println("First subscriber: "+ n ));
Thread.sleep(3000);
Disposable subscriber2 = numbers
.subscribe(n ->System.out.println(" Second subscriber: "+ n ));
Thread.sleep(5000);
System.out.println(">>> First subscriber goes for lunch break");
subscriber1.dispose();
Thread.sleep(5000);
System.out.println("<<< First subscriber returned from lunch");
subscriber1 = numbers.subscribe(n ->System.out.println("First subscriber: "+ n ));
Thread.sleep(60000); // Just to keep the program running
}