Search in sources :

Example 1 with FunctionIterator

use of org.springframework.integration.util.FunctionIterator in project spring-integration by spring-projects.

the class AbstractMessageSplitter method handleRequestMessage.

@Override
@SuppressWarnings("unchecked")
protected final Object handleRequestMessage(Message<?> message) {
    Object result = this.splitMessage(message);
    // return null if 'null'
    if (result == null) {
        return null;
    }
    boolean reactive = getOutputChannel() instanceof ReactiveStreamsSubscribableChannel;
    setAsync(reactive);
    Iterator<Object> iterator = null;
    Flux<Object> flux = null;
    final int sequenceSize;
    if (result instanceof Iterable<?>) {
        Iterable<Object> iterable = (Iterable<Object>) result;
        sequenceSize = obtainSizeIfPossible(iterable);
        if (reactive) {
            flux = Flux.fromIterable(iterable);
        } else {
            iterator = iterable.iterator();
        }
    } else if (result.getClass().isArray()) {
        Object[] items = (Object[]) result;
        sequenceSize = items.length;
        if (reactive) {
            flux = Flux.fromArray(items);
        } else {
            iterator = Arrays.asList(items).iterator();
        }
    } else if (result instanceof Iterator<?>) {
        Iterator<Object> iter = (Iterator<Object>) result;
        sequenceSize = obtainSizeIfPossible(iter);
        if (reactive) {
            flux = Flux.fromIterable(() -> iter);
        } else {
            iterator = iter;
        }
    } else if (result instanceof Stream<?>) {
        Stream<Object> stream = ((Stream<Object>) result);
        sequenceSize = 0;
        if (reactive) {
            flux = Flux.fromStream(stream);
        } else {
            iterator = stream.iterator();
        }
    } else if (result instanceof Publisher<?>) {
        Publisher<Object> publisher = (Publisher<Object>) result;
        sequenceSize = 0;
        if (reactive) {
            flux = Flux.from(publisher);
        } else {
            iterator = Flux.from((Publisher<Object>) result).toIterable().iterator();
        }
    } else {
        sequenceSize = 1;
        if (reactive) {
            flux = Flux.just(result);
        } else {
            iterator = Collections.singleton(result).iterator();
        }
    }
    if (iterator != null && !iterator.hasNext()) {
        return null;
    }
    Map<String, Object> messageHeaders = message.getHeaders();
    if (willAddHeaders(message)) {
        messageHeaders = new HashMap<>(messageHeaders);
        addHeaders(message, messageHeaders);
    }
    final Map<String, Object> headers = messageHeaders;
    final Object correlationId = message.getHeaders().getId();
    final AtomicInteger sequenceNumber = new AtomicInteger(1);
    Function<Object, AbstractIntegrationMessageBuilder<?>> messageBuilderFunction = object -> createBuilder(object, headers, correlationId, sequenceNumber.getAndIncrement(), sequenceSize);
    if (reactive) {
        return flux.map(messageBuilderFunction);
    } else {
        return new FunctionIterator<>(iterator, messageBuilderFunction);
    }
}
Also used : Arrays(java.util.Arrays) Iterator(java.util.Iterator) Collection(java.util.Collection) Publisher(org.reactivestreams.Publisher) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) HashMap(java.util.HashMap) ReactiveStreamsSubscribableChannel(org.springframework.integration.channel.ReactiveStreamsSubscribableChannel) Function(java.util.function.Function) Flux(reactor.core.publisher.Flux) Stream(java.util.stream.Stream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Closeable(java.io.Closeable) Map(java.util.Map) Message(org.springframework.messaging.Message) AbstractIntegrationMessageBuilder(org.springframework.integration.support.AbstractIntegrationMessageBuilder) Collections(java.util.Collections) FunctionIterator(org.springframework.integration.util.FunctionIterator) FunctionIterator(org.springframework.integration.util.FunctionIterator) ReactiveStreamsSubscribableChannel(org.springframework.integration.channel.ReactiveStreamsSubscribableChannel) Publisher(org.reactivestreams.Publisher) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AbstractIntegrationMessageBuilder(org.springframework.integration.support.AbstractIntegrationMessageBuilder) Iterator(java.util.Iterator) FunctionIterator(org.springframework.integration.util.FunctionIterator) Stream(java.util.stream.Stream)

Aggregations

Closeable (java.io.Closeable)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Function (java.util.function.Function)1 Stream (java.util.stream.Stream)1 Publisher (org.reactivestreams.Publisher)1 ReactiveStreamsSubscribableChannel (org.springframework.integration.channel.ReactiveStreamsSubscribableChannel)1 AbstractReplyProducingMessageHandler (org.springframework.integration.handler.AbstractReplyProducingMessageHandler)1 AbstractIntegrationMessageBuilder (org.springframework.integration.support.AbstractIntegrationMessageBuilder)1 FunctionIterator (org.springframework.integration.util.FunctionIterator)1 Message (org.springframework.messaging.Message)1 Flux (reactor.core.publisher.Flux)1