简体   繁体   English

如何在反应式Java中将新对象添加到现有流中?

[英]How can I add in reactive Java a new Object to an existing stream?

Assuming I've already a reactive stream and now I wanna add one more object to this existing stream. 假设我已经有一个响应流,现在我想向此现有流添加一个对象。 How can I do this? 我怎样才能做到这一点?

This is the approach i found, is this the way to go? 这是我发现的方法 ,这是走的路吗?

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

import reactor.core.publisher.Flux;
import reactor.core.publisher.FluxSink;

/**
 * Created by ton on 10/11/16.
 */
public class Example {

    private List<FluxSink<String>> handlers = new ArrayList<>();

    public Flux<String> getMessagesAsStream() {
        Flux<String> result = Flux.create(sink -> {
                handlers.add(sink);
            sink.setCancellation(() -> handlers.remove(sink));
        });

        return result;
    }

    public void handleMessage(String message) {
        handlers.forEach(han -> han.next(message));
    }

    public static void main(String[] args) {
        Example example = new Example();
        example.getMessagesAsStream().subscribe(req -> System.out.println("req = " + req));
        example.getMessagesAsStream().subscribe(msg -> System.out.println(msg.toUpperCase()));
        example.handleMessage("een");
        example.handleMessage("twee");
        example.handleMessage("drie");
    }
}

Suppose this is your existing stream: 假设这是您现有的流:

Flux<Integer> existingStream = Flux.just(1, 2, 3, 4);

You can concatenate two streams: 您可以连接两个流:

Flux<Integer> appendObjectToStream = Flux.concat(existingStream, Flux.just(5));

This will produce [1, 2, 3, 4, 5] . 这将产生[1, 2, 3, 4, 5]

Alternatively you could merge both streams: 或者,您可以合并两个流:

Flux<Integer> mergeObjectWithStream = Flux.merge(existingStream, Flux.just(5));

That will produce a similar stream, however the 5 element could appear anywhere in the resulting flux. 这将产生类似的流,但是5元素可能会出现在结果磁通量的任何位置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Java中将方法添加到现有对象? - How can I add method to an existing object in Java? 如何在Java中将新键值添加到现有List Map对象 - How to add new key value to existing List Map object in java 如何在Java中将新的类对象添加到现有JSON文件中? - How to add append a new class object to the existing JSON file in Java? How can I copy existing class object to other class new class object without creating copy of original object(in memory) in Java - How can I copy existing class object to other class new class object without creating copy of original object(in memory) in Java 作为if语句的结果,我如何在另一个反应流中使用一个反应流 - How can I use one reactive stream inside another reactive stream as result of if statement How can I use Java Stream in order to iterate on a collection on object and use a specific string field of each object to create a new array? - How can I use Java Stream in order to iterate on a collection on object and use a specific string field of each object to create a new array? Java Reactive stream how to map an object when the object being mapped is also needed on the next step of the stream - Java Reactive stream how to map an object when the object being mapped is also needed on the next step of the stream 如何在现有领域数据库中添加新表? - How can i add new table in existing realm database? 如何在此示例中已存在的新jpanel上添加? - How can I add new jpanel on already existing in this example? 如何在java流中添加我的方法? - How can I add my method inside java stream?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM