All Downloads are FREE. Search and download functionalities are using the official Maven repository.

me.escoffier.fluid.example.CamelComponent Maven / Gradle / Ivy

package me.escoffier.fluid.example;

import io.reactivex.Flowable;
import io.vertx.core.json.JsonObject;
import me.escoffier.fluid.annotations.Inbound;
import me.escoffier.fluid.annotations.Outbound;
import me.escoffier.fluid.annotations.Transformation;
import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.reactive.streams.api.CamelReactiveStreams;
import org.apache.camel.component.reactive.streams.api.CamelReactiveStreamsService;
import org.apache.camel.impl.DefaultCamelContext;
import org.reactivestreams.Publisher;
import org.reactivestreams.Subscriber;

/**
 * @author Clement Escoffier
 */
public class CamelComponent {

  @Transformation
  @Outbound("result")
  public Publisher mediation(@Inbound("sensor") Flowable input) throws Exception {
    CamelContext context = new DefaultCamelContext();
    CamelReactiveStreamsService camel = CamelReactiveStreams.get(context);

    Subscriber elements = camel.streamSubscriber("elements", JsonObject.class);

    Flowable flowable = Flowable.fromPublisher(camel.fromStream("out"))
      .map(exchange -> (Integer) exchange.getIn().getBody());

    context.addRoutes(new RouteBuilder() {
      @Override
      public void configure() {
        from("reactive-streams:elements")
          .setBody().body(json -> ((JsonObject)json).getInteger("data"))
          .to("reactive-streams:out");
      }
    });

    input.subscribe(elements);

    context.start();

    return flowable;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy