当前位置: 首页>>代码示例>>Java>>正文


Java ConnectableObservable.subscribe方法代码示例

本文整理汇总了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());

    }
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:21,代码来源:ReplayExampleActivity.java

示例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();
}
 
开发者ID:PacktPublishing,项目名称:Learning-RxJava,代码行数:15,代码来源:Ch5_6.java

示例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();
}
 
开发者ID:PacktPublishing,项目名称:Learning-RxJava,代码行数:9,代码来源:Ch5_5.java

示例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();
}
 
开发者ID:PacktPublishing,项目名称:Learning-RxJava,代码行数:8,代码来源:Ch5_2.java

示例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();
    }
 
开发者ID:PacktPublishing,项目名称:Learning-RxJava,代码行数:12,代码来源:Ch5_11.java

示例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();
    }
 
开发者ID:PacktPublishing,项目名称:Learning-RxJava,代码行数:16,代码来源:Ch5_12.java

示例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);
    }
 
开发者ID:PacktPublishing,项目名称:Learning-RxJava,代码行数:14,代码来源:Ch2_19.java

示例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();
    }
 
开发者ID:PacktPublishing,项目名称:Learning-RxJava,代码行数:13,代码来源:Ch2_14.java

示例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;
}
 
开发者ID:PacktPublishing,项目名称:Spring-5.0-Cookbook,代码行数:14,代码来源:EmployeeRxJavaServiceImpl.java

示例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);
}
 
开发者ID:dhinojosa,项目名称:rxjavatraining,代码行数:24,代码来源:HotVsColdObservableTest.java

示例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;
}
 
开发者ID:danrien,项目名称:projectBlue,代码行数:61,代码来源:PlaybackEngine.java

示例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();

	
}
 
开发者ID:PacktPublishing,项目名称:Reactive-Programming-With-Java-9,代码行数:14,代码来源:Demo_Hot_Observable.java

示例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
    }
 
开发者ID:yfain,项目名称:rxjava2,代码行数:28,代码来源:HotObservable.java


注:本文中的io.reactivex.observables.ConnectableObservable.subscribe方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。