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


Java MonoProcessor.block方法代码示例

本文整理汇总了Java中reactor.core.publisher.MonoProcessor.block方法的典型用法代码示例。如果您正苦于以下问题:Java MonoProcessor.block方法的具体用法?Java MonoProcessor.block怎么用?Java MonoProcessor.block使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在reactor.core.publisher.MonoProcessor的用法示例。


在下文中一共展示了MonoProcessor.block方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: monoProcessorGetEmployee

import reactor.core.publisher.MonoProcessor; //导入方法依赖的package包/类
@Override
public void monoProcessorGetEmployee(Integer id) {
	MonoProcessor<Integer> future = MonoProcessor.create();
	Consumer<Integer> checkEmp = (rowId) ->{
		if(employeeDaoImpl.getEmployee(rowId) == null){
			System.out.println("Employee with id: " + rowId + " does not exists.");
		}else{
			System.out.println("Employee with id: " + rowId + " exists.");
		}
	};
	
	Mono<Integer> engine = future
		    .doOnNext(checkEmp)
	     	.doOnSuccess(emp -> {
				System.out.println("Employee's age is " + employeeDaoImpl.getEmployee(emp).getAge());
				System.out.println("Employee's dept is: " + employeeDaoImpl.getEmployee(emp).getDeptId());
			})
	        .doOnTerminate((sup, ex) -> System.out.println("Transaction terminated with error: " +ex.getMessage()))
	        .doOnError(ex -> System.out.println("Error: " + ex.getMessage()));
	
	engine.subscribe(System.out::println);
	
	future.onNext(id);
	int valStream = future.block();
	System.out.println("Employee's ID again is: " + valStream);
}
 
开发者ID:PacktPublishing,项目名称:Spring-5.0-Cookbook,代码行数:27,代码来源:EmployeeHotStreamServiceImpl.java

示例2: streamValuesCanBeExploded

import reactor.core.publisher.MonoProcessor; //导入方法依赖的package包/类
@Test
	public void streamValuesCanBeExploded() {
//		Stream"s values can be exploded
//			given: "a source composable with a mapMany function"
		EmitterProcessor<Integer> source = EmitterProcessor.create();
		Flux<Integer> mapped = source
				.log()
				.publishOn(Schedulers.parallel())
				.log()
				.flatMap(v -> Flux.just(v * 2))
				.doOnError(Throwable::printStackTrace);

//			when: "the source accepts a value"
		MonoProcessor<Integer> value = mapped.next()
		                                     .toProcessor();
		value.subscribe();
		source.sink().next(1);

//		then: "the value is mapped"
		int result = value.block(Duration.ofSeconds(5));
		assertThat(result).isEqualTo(2);
	}
 
开发者ID:reactor,项目名称:reactor-core,代码行数:23,代码来源:FluxSpecTests.java

示例3: sendFileSecure

import reactor.core.publisher.MonoProcessor; //导入方法依赖的package包/类
@Test
public void sendFileSecure()
		throws CertificateException, SSLException, InterruptedException, URISyntaxException {
	Path largeFile = Paths.get(getClass().getResource("/largeFile.txt").toURI());
	SelfSignedCertificate ssc = new SelfSignedCertificate();
	SslContext sslServer = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
	SslContext sslClient = SslContextBuilder.forClient().trustManager(ssc.cert()).build();

	NettyContext context =
			TcpServer.create(opt -> opt.sslContext(sslServer))
			         .newHandler((in, out) ->
					         in.receive()
					           .asString()
					           .flatMap(word -> "GOGOGO".equals(word) ?
							           out.sendFile(largeFile).then() :
							           out.sendString(Mono.just("NOPE"))
					           )
			         )
			         .block();

	MonoProcessor<String> m1 = MonoProcessor.create();
	MonoProcessor<String> m2 = MonoProcessor.create();

	NettyContext client1 =
			TcpClient.create(opt -> opt.port(context.address().getPort())
			                           .sslContext(sslClient))
			         .newHandler((in, out) -> {
				         in.receive()
				           .asString()
				           .log("-----------------CLIENT1")
				           .subscribe(m1::onNext);

				         return out.sendString(Mono.just("gogogo"))
						         .neverComplete();
			         })
			         .block();

	NettyContext client2 =
			TcpClient.create(opt -> opt.port(context.address().getPort())
			                           .sslContext(sslClient))
			         .newHandler((in, out) -> {
				         in.receive()
				           .asString(StandardCharsets.UTF_8)
				           .take(2)
				           .reduceWith(String::new, String::concat)
				           .log("-----------------CLIENT2")
				           .subscribe(m2::onNext);

				         return out.sendString(Mono.just("GOGOGO"))
				                   .neverComplete();
			         })
			         .block();

	String client1Response = m1.block();
	String client2Response = m2.block();

	client1.dispose();
	client1.onClose().block();

	client2.dispose();
	client2.onClose().block();

	context.dispose();
	context.onClose().block();

	Assertions.assertThat(client1Response).isEqualTo("NOPE");

	Assertions.assertThat(client2Response)
	          .startsWith("This is an UTF-8 file that is larger than 1024 bytes. " + "It contains accents like é.")
	          .contains("1024 mark here ->")
	          .contains("<- 1024 mark here")
	          .endsWith("End of File");
}
 
开发者ID:reactor,项目名称:reactor-netty,代码行数:74,代码来源:TcpServerTests.java

示例4: assertSendFile

import reactor.core.publisher.MonoProcessor; //导入方法依赖的package包/类
private void assertSendFile(Function<NettyOutbound, NettyOutbound> fn)
		throws InterruptedException {



	NettyContext context =
			TcpServer.create()
			         .newHandler((in, out) ->
					         in.receive()
					           .asString()
					           .flatMap(word -> "GOGOGO".equals(word) ?
							           fn.apply(out).then() :
							           out.sendString(Mono.just("NOPE"))
					           )
			         )
			         .block();

	MonoProcessor<String> m1 = MonoProcessor.create();
	MonoProcessor<String> m2 = MonoProcessor.create();

	NettyContext client1 =
			TcpClient.create(opt -> opt.port(context.address().getPort()))
			         .newHandler((in, out) -> {
				         in.receive()
				           .asString()
				           .log("-----------------CLIENT1")
				           .subscribe(m1::onNext);

				         return out.sendString(Mono.just("gogogo"))
				                   .neverComplete();
			         })
			         .block();

	NettyContext client2 =
			TcpClient.create(opt -> opt.port(context.address().getPort()))
			         .newHandler((in, out) -> {
				         in.receive()
				           .asString(StandardCharsets.UTF_8)
				           .take(2)
				           .reduceWith(String::new, String::concat)
				           .log("-----------------CLIENT2")
				           .subscribe(m2::onNext);

				         return out.sendString(Mono.just("GOGOGO"))
				                   .neverComplete();
			         })
			         .block();

	String client1Response = m1.block();
	String client2Response = m2.block();

	client1.dispose();
	client1.onClose().block();

	client2.dispose();
	client2.onClose().block();

	context.dispose();
	context.onClose().block();

	Assertions.assertThat(client1Response).isEqualTo("NOPE");

	Assertions.assertThat(client2Response)
	          .startsWith("This is an UTF-8 file that is larger than 1024 bytes. " + "It contains accents like é.")
	          .contains("1024 mark here ->")
	          .contains("<- 1024 mark here")
	          .endsWith("End of File");
}
 
开发者ID:reactor,项目名称:reactor-netty,代码行数:69,代码来源:TcpServerTests.java

示例5: multiplexUsingDispatchersAndSplit

import reactor.core.publisher.MonoProcessor; //导入方法依赖的package包/类
/**
 * <pre>
 *                 forkStream
 *                 /        \      < - - - int
 *                v          v
 * persistenceStream        computationStream
 *                 \        /      < - - - List< String >
 *                  v      v
 *                 joinStream      < - - - String
 *                 splitStream
 *             observedSplitStream
 * </pre>
    * @throws Exception for convenience
 */
@Test(timeout = TIMEOUT)
public void multiplexUsingDispatchersAndSplit() throws Exception {

	final EmitterProcessor<Integer> forkEmitterProcessor = EmitterProcessor.create();

	final EmitterProcessor<Integer> computationEmitterProcessor = EmitterProcessor.create(false);

	Scheduler computation = Schedulers.newSingle("computation");
	Scheduler persistence = Schedulers.newSingle("persistence");
	Scheduler forkJoin = Schedulers.newParallel("forkJoin", 2);

	final Flux<List<String>> computationStream =
			computationEmitterProcessor.publishOn(computation)
			                      .map(i -> {
				                      final List<String> list = new ArrayList<>(i);
				                      for (int j = 0; j < i; j++) {
					                      list.add("i" + j);
				                      }
				                      return list;
			                      })
			                      .doOnNext(ls -> println("Computed: ", ls))
			                      .log("computation");

	final EmitterProcessor<Integer> persistenceEmitterProcessor = EmitterProcessor.create(false);

	final Flux<List<String>> persistenceStream =
			persistenceEmitterProcessor.publishOn(persistence)
			                      .doOnNext(i -> println("Persisted: ", i))
			                      .map(i -> Collections.singletonList("done" + i))
			                      .log("persistence");

	Flux<Integer> forkStream = forkEmitterProcessor.publishOn(forkJoin)
	                                             .log("fork");

	forkStream.subscribe(computationEmitterProcessor);
	forkStream.subscribe(persistenceEmitterProcessor);

	final Flux<List<String>> joinStream = Flux.zip(computationStream, persistenceStream, (a, b) -> Arrays.asList(a, b))
	                                                .publishOn(forkJoin)
	                                                .map(listOfLists -> {
		                                               listOfLists.get(0)
		                                                          .addAll(listOfLists.get(1));
		                                               return listOfLists.get(0);
	                                               })
	                                                .log("join");

	final Semaphore doneSemaphore = new Semaphore(0);

	final MonoProcessor<List<String>> listPromise = joinStream.flatMap(Flux::fromIterable)
	                                                 .log("resultStream")
	                                                 .collectList()
	                                                 .doOnTerminate(doneSemaphore::release)
	                                                 .toProcessor();
	listPromise.subscribe();

	forkEmitterProcessor.onNext(1);
	forkEmitterProcessor.onNext(2);
	forkEmitterProcessor.onNext(3);
	forkEmitterProcessor.onComplete();

	List<String> res = listPromise.block(Duration.ofSeconds(5));
	assertEquals(Arrays.asList("i0", "done1", "i0", "i1", "done2", "i0", "i1", "i2", "done3"), res);

	forkJoin.dispose();
	persistence.dispose();
	computation.dispose();
}
 
开发者ID:reactor,项目名称:reactor-core,代码行数:82,代码来源:FluxTests.java


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