springboot webflux mono 使用示例(创建操作)


springboot webflux mono 使用示例(创建操作)

 

 

**************************

just、defer

 

public class Test {

    public static void main(String[] args) throws Exception{
        Mono.defer(() -> Mono.just("hello world "+System.currentTimeMillis()))
                .delaySubscription(Duration.ofSeconds(2))
                .subscribe(s -> {
                    System.out.println(s);
                    System.out.println(System.currentTimeMillis());
                });

        Mono.just("hello world 2 "+System.currentTimeMillis())
                .delaySubscription(Duration.ofSeconds(2))
                .subscribe(s -> {
                    System.out.println(s);
                    System.out.println(System.currentTimeMillis());
                });

        TimeUnit.SECONDS.sleep(6);
    }
}

 

*********************

控制台输出

 

hello world 1593161698708
1593161698721
hello world 2 1593161696692
1593161698722

defer操作时间秒数相同,just操作秒数相差2秒

 

 

**************************

sequenceEqual:比较对象流是否相等

 

public class Test2 {

    public static void main(String[] args){
        Mono.sequenceEqual(Mono.just("hello world"),Mono.just("hello world"))
                .subscribe(System.out::println);

        Mono.sequenceEqual(Mono.just(2),Mono.just(3)).subscribe(System.out::println);
    }
}

 

*********************

控制台输出

 

true
false

 

 

**************************

fromRunnable、fromCallable

 

public class Test3 {

    public static void main(String[] args) {
        Mono.fromRunnable(() -> System.out.println("hello world")).subscribe();
        Mono.fromCallable(() -> 2).subscribe(System.out::println);
    }
}

 

*********************

控制台输出

 

hello world
2

 

 

**************************

using 操作

 

public class Test4 {

    public static void main(String[] args){
        Mono.using(()->2,
                integer->Mono.just(integer+1),
                integer->System.out.println("cleanup "+integer),
                false)
                .subscribe(System.out::println);

        System.out.println("\n***************");
        Mono.using(()->10,
                integer->Mono.just(integer+1),
                integer->System.out.println("cleanup "+integer),
                true)
                .subscribe(System.out::println);

        System.out.println("\n***************");
        Mono.using(() -> 20,
                integer -> Mono.just(integer+1),
                integer -> System.out.println("cleanup "+integer))
                .subscribe(System.out::println);
    }
}

 

*********************

控制台输出

 

3
cleanup 2

***************
cleanup 10
11

***************
cleanup 20
21

 

 

**************************

when 操作

 

public class Test5 {

    public static void main(String[] args){
        Mono.when(subscriber -> subscriber.onSubscribe(new Subscription() {

            @Override
            public void request(long l) {
                System.out.println("request:"+l);
            }

            @Override
            public void cancel() {
                System.out.println("cancel");
            }
        })).subscribe(System.out::println);
    }
}

 

*********************

控制台输出

 

request:9223372036854775807

 

 

**************************

zip 操作

 

public class Test6 {

    public static void main(String[] args){
        Mono.zip(Mono.just(2),Mono.just("Hello world")).subscribe(tuple -> tuple.iterator().forEachRemaining(System.out::println));

        System.out.println("\n****************");
        Mono.zip(Mono.just(2),Mono.just("hello world"),(value1,value2)-> value2+" "+value1)
                .subscribe(System.out::println);
    }
}

 

*********************

控制台输出

 

2
Hello world

****************
hello world 2

 

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好!关于Spring Boot WebFluxWebSocket的使用,我可以为您提供一些基本的信息。在Spring Boot WebFlux中,您可以使用WebSocket来实现实时双向通信。 要使用WebSocket,首先需要添加Spring Boot WebFluxWebSocket的依赖。在您的Maven或Gradle构建文件中,添加以下依赖: Maven: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> ``` Gradle: ```groovy implementation 'org.springframework.boot:spring-boot-starter-webflux' ``` 接下来,您可以创建一个WebSocket处理器类,用于处理客户端的WebSocket请求。这个处理器类需要实现`WebSocketHandler`接口,并实现其中的方法,例如`handle`方法用于处理WebSocket消息。 以下是一个简单的示例: ```java @Component public class MyWebSocketHandler implements WebSocketHandler { @Override public Mono<Void> handle(WebSocketSession session) { // 处理WebSocket消息 Flux<Message> messageFlux = // 处理消息的逻辑 return session.send(messageFlux.map(session::textMessage)); } } ``` 在上面的示例中,`MyWebSocketHandler`类实现了`WebSocketHandler`接口,并重写了`handle`方法来处理WebSocket消息。在这个方法中,您可以根据具体需求编写处理消息的逻辑。 最后,您需要配置一个路由来映射WebSocket请求到您的处理器类。在您的配置类中,添加以下配置: ```java @Configuration public class WebSocketConfig implements WebSocketHandlerRegistry { @Override public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { registry.addHandler(myWebSocketHandler(), "/websocket"); } @Bean public WebSocketHandler myWebSocketHandler() { return new MyWebSocketHandler(); } } ``` 在上面的配置中,`registerWebSocketHandlers`方法用于注册WebSocket处理器,并指定了WebSocket请求的路径。`myWebSocketHandler`方法返回您创建WebSocket处理器实例。 这样,您就可以使用Spring Boot WebFluxWebSocket来实现实时双向通信了。当客户端连接到`/websocket`路径时,请求将被路由到您的处理器类进行处理。 希望这些信息能对您有所帮助!如有任何疑问,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值