org.apache.camel.component.rxjava2.engine.RxJavaStreamsService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of camel-rxjava2 Show documentation
Show all versions of camel-rxjava2 Show documentation
RxJava2 based back-end for Camel's reactive streams component
The newest version!
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.component.rxjava2.engine;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Function;
import java.util.function.Supplier;
import io.reactivex.Flowable;
import io.reactivex.Single;
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.reactive.streams.ReactiveStreamsCamelSubscriber;
import org.apache.camel.component.reactive.streams.ReactiveStreamsConsumer;
import org.apache.camel.component.reactive.streams.ReactiveStreamsHelper;
import org.apache.camel.component.reactive.streams.ReactiveStreamsProducer;
import org.apache.camel.component.reactive.streams.api.CamelReactiveStreamsService;
import org.apache.camel.component.reactive.streams.util.BodyConverter;
import org.apache.camel.component.reactive.streams.util.ConvertingPublisher;
import org.apache.camel.component.reactive.streams.util.ConvertingSubscriber;
import org.apache.camel.component.reactive.streams.util.UnwrapStreamProcessor;
import org.apache.camel.spi.Synchronization;
import org.apache.camel.support.service.ServiceSupport;
import org.apache.camel.util.function.Suppliers;
import org.reactivestreams.Publisher;
import org.reactivestreams.Subscriber;
final class RxJavaStreamsService extends ServiceSupport implements CamelReactiveStreamsService {
private final CamelContext context;
private final Supplier unwrapStreamProcessorSupplier;
private final ConcurrentMap publishers;
private final ConcurrentMap subscribers;
private final ConcurrentMap publishedUriToStream;
private final ConcurrentMap requestedUriToStream;
RxJavaStreamsService(CamelContext context) {
this.context = context;
this.publishers = new ConcurrentHashMap<>();
this.subscribers = new ConcurrentHashMap<>();
this.publishedUriToStream = new ConcurrentHashMap<>();
this.requestedUriToStream = new ConcurrentHashMap<>();
this.unwrapStreamProcessorSupplier = Suppliers.memorize(UnwrapStreamProcessor::new);
}
@Override
public String getId() {
return RxJavaStreamsConstants.SERVICE_NAME;
}
// ******************************************
// Lifecycle
// ******************************************
@Override
public void doStart() throws Exception {
}
@Override
public void doStop() throws Exception {
for (RxJavaCamelProcessor processor : publishers.values()) {
processor.close();
}
for (ReactiveStreamsCamelSubscriber subscriber : subscribers.values()) {
subscriber.close();
}
}
// ******************************************
//
// ******************************************
@Override
public Publisher fromStream(String name) {
return getCamelProcessor(name).getPublisher();
}
@Override
public Publisher fromStream(String name, Class type) {
final Publisher publisher = fromStream(name);
if (Exchange.class.isAssignableFrom(type)) {
return Publisher.class.cast(publisher);
}
return Flowable.fromPublisher(publisher).map(BodyConverter.forType(type)::apply);
}
@Override
public ReactiveStreamsCamelSubscriber streamSubscriber(String name) {
return subscribers.computeIfAbsent(name, n -> new ReactiveStreamsCamelSubscriber(name));
}
@SuppressWarnings("unchecked")
public Subscriber streamSubscriber(String name, Class type) {
final Subscriber subscriber = streamSubscriber(name);
if (Exchange.class.equals(type)) {
return Subscriber.class.cast(subscriber);
}
return new ConvertingSubscriber<>(subscriber, context);
}
@Override
public Publisher toStream(String name, Object data) {
return doRequest(
name,
ReactiveStreamsHelper.convertToExchange(context, data)
);
}
@Override
public Function, ? extends Publisher> toStream(String name) {
return data -> toStream(name, data);
}
@Override
public Publisher toStream(String name, Object data, Class type) {
return new ConvertingPublisher<>(toStream(name, data), type);
}
@Override
public Function
© 2015 - 2025 Weber Informatics LLC | Privacy Policy