Can Camel be replaced with Reactor, if yes, then how can it be done correctly?

时间:2017-03-22 18:40:26

标签: java apache-camel jms reactive-programming project-reactor

I wonder if Apache Camel can be replaced by Reactor or some other Reactive Streams (RS) library.

I'm new to Reactor and RS world, but seems that I managed to prepare some Flux that is listening to JMS messages, although I'm not sure how correct my approach is (this project helped me do it - https://github.com/tonvanbart/gs-messaging-jms-reactive). Main component:

@Component
public class PublisherMessageListener {

    private final DefaultMessageListenerContainer jmsContainer;

    public PublisherMessageListener(DefaultMessageListenerContainer jmsContainer, EventProcessor processor) {
        this.jmsContainer = jmsContainer;
        publisher().subscribe(processor::process);
    }

    private Flux<String> publisher() {
        return Flux.from(subscriber -> {
            MessageListener listener = message -> {
                if (message instanceof TextMessage) {
                    TextMessage textMessage = (TextMessage) message;
                    try {
                        subscriber.onNext(textMessage.getText());
                    } catch (Exception e) {
                        subscriber.onError(new RuntimeException(e));
                    }
                }
            };
            jmsContainer.setMessageListener(listener);
        });
    }
}

I send messages using jmsTemplate.convertAndSend(DESTINATION_NAME, "{ 'type': 'CUSTOMER', 'id': 1 }") and EventProcessor processes each message.

Is reactive part correct?

I'd like to also understand, if exception handling with redeliveries can be done in Reactor as well as aggregation of messages. These things:

onException(RuntimeException.class)
    .maximumRedeliveries(2)
    .to(ERROR_URI);

from(URI)
    .aggregate(expression(this::getKey), AggregationStrategies.useLatest())
    .completionTimeout(DELAY_MILLIS)

I started playing with Reactor as it will be in Spring at some point. Akka Streams seem more Scala oriented to me (don't know about its Java support). Reactor also seemed more Java 8 oriented than RxJava as well as I liked Reactor's Flux and Mono instead of only one Flowable. But if you can provide good arguments why specific library is better, i'd be happy to hear them.

1 个答案:

答案 0 :(得分:2)

从camel 2.11开始,有一个camel-rx模块可以迭代RxJava API。 http://camel.apache.org/rx.html