SpringWebFlux03subscribe()

编程

subscribe()方法表示对数据流的订阅动作,subscribe()方法有多个重载的方法,下面介绍几种常见的subscribe()方法:

2、代码示例

2.1 主函数代码

package com.test.reactor;

import reactor.core.publisher.Flux;

import reactor.core.publisher.Mono;

/**

* 第一个Reactor程序

*/

public class FirstReactorDemo {

public static void main(String[] args) {

// 测试Flux

Flux.just(1, 2, 3, 4, 5, 6).subscribe(System.out::print);

System.out.println("

----------------------------");

// 测试Mono

Mono.just(1).subscribe(System.out::println);

System.out.println("----------------------------");

// 测试2个参数的subscribe方法

Flux.just(1, 2, 3, 4, 5, 6).subscribe(System.out::print, System.err::println);

System.out.println("

----------------------------");

// 测试3个参数的subscribe方法

Flux.just(1, 2, 3, 4, 5, 6)

.subscribe(System.out::print, System.err::println,

() -> System.out.println("

complete"));

System.out.println("----------------------------");

// 测试4个参数的subscribe方法

Flux.just(1, 2, 3, 4, 5, 6)

.subscribe(System.out::print, System.err::println,

() -> System.out.println("

complete"), subscription -> {

System.out.println("订阅发生了");

subscription.request(10);

});

}

}

2.2 运行结果

123456

----------------------------

1

----------------------------

123456

----------------------------

123456

complete

----------------------------

订阅发生了

123456

complete

3、响应式异步代码测试工具 StepVerifier

3.1 主函数代码

package com.test.reactor;

import reactor.core.publisher.Flux;

import reactor.core.publisher.Mono;

import reactor.test.StepVerifier;

/**

* StepVerifier测试案例

*/

public class StepVerifierDemo {

public static void main(String[] args) {

Flux flux = Flux.just(1, 2, 3, 4, 5, 6);

// 使用StepVerifier测试Flux,应该正常

StepVerifier.create(flux)

//测试下一个期望的数据元素

.expectNext(1, 2, 3, 4, 5, 6)

//测试下一个元素是否为完成信号

.expectComplete()

.verify();

Mono mono = Mono.error(new Exception("some error"));

// 使用StepVerifier测试Mono,应该会出现异常

StepVerifier.create(mono)

//测试下一个元素是否为完成信号

.expectComplete()

.verify();

}

}

3.2 运行结果,结果不过,抛出异常

Exception in thread "main" java.lang.AssertionError: expectation "expectComplete" failed (expected: onComplete(); actual: onError(java.lang.Exception: some error))

at reactor.test.ErrorFormatter.assertionError(ErrorFormatter.java:105)

at reactor.test.ErrorFormatter.failPrefix(ErrorFormatter.java:94)

at reactor.test.ErrorFormatter.fail(ErrorFormatter.java:64)

at reactor.test.ErrorFormatter.failOptional(ErrorFormatter.java:79)

at reactor.test.DefaultStepVerifierBuilder.lambda$expectComplete$4(DefaultStepVerifierBuilder.java:323)

at reactor.test.DefaultStepVerifierBuilder$SignalEvent.test(DefaultStepVerifierBuilder.java:2112)

at reactor.test.DefaultStepVerifierBuilder$DefaultVerifySubscriber.onSignal(DefaultStepVerifierBuilder.java:1408)

at reactor.test.DefaultStepVerifierBuilder$DefaultVerifySubscriber.onExpectation(DefaultStepVerifierBuilder.java:1356)

at reactor.test.DefaultStepVerifierBuilder$DefaultVerifySubscriber.onError(DefaultStepVerifierBuilder.java:1030)

at reactor.core.publisher.Operators.error(Operators.java:181)

at reactor.core.publisher.MonoError.subscribe(MonoError.java:52)

at reactor.core.publisher.Mono.subscribe(Mono.java:3695)

at reactor.test.DefaultStepVerifierBuilder$DefaultStepVerifier.verify(DefaultStepVerifierBuilder.java:801)

at reactor.test.DefaultStepVerifierBuilder$DefaultStepVerifier.verify(DefaultStepVerifierBuilder.java:772)

at com.test.reactor.StepVerifierDemo.main(StepVerifierDemo.java:26)

Suppressed: java.lang.Exception: some error

at com.test.reactor.StepVerifierDemo.main(StepVerifierDemo.java:21)

 

 

以上是 SpringWebFlux03subscribe() 的全部内容, 来源链接: utcz.com/z/513216.html

回到顶部