简体   繁体   中英

How to route the data using camel?

I'm new to camel.

When I get the request to the endpoint, the camel flow should be started. RequestBody is the input to the flow (InputA).

Please let me know how to start this:

InputA -> ProcessA -> OutputA

OutputA -> ProcessB -> OutputB

OutputB -> ProcessC -> OutputC

Just as an example:

public class ProcessA{
    public String methodA(String arg){
        return arg;
    }
}

public class ProcessB{
    public String methodB(String arg){
        return arg;
    }
}

public class ProcessC{
    public String methodC(String arg){
        return arg;
    }
}

How to flow the input and output using Camel data flow.

Any help or links will be appreciated.

I advice you to read the book Camel in action . There are a lot of examples, source code is also awailable. When i started to study camel, I found this book very useful.

Also In addition to Cluas answer i can add an example:

public class Example
{

public static void main(String[] args) throws Exception
{

    CamelContext context = new DefaultCamelContext();

    context.addRoutes(new RouteBuilder()
    {

        @Override
        public void configure() throws Exception
        {
            from("InputA").process(exchange -> {
                //This is process A.
            }).to("OutputA");

            from("OutputA").process(exchange -> {
                //This is process B.
            }).to("OutputB");

            from("OutputB").process(exchange -> {
                //This is process C.
            }).to("OutputC");
        }
    });

    context.start();

    //let camel complite his job
    Thread.sleep(2000);

    context.stop();

}

}

You can build 3 Camel routes, and then use some queue component to separate them, such as the internal direct (no queue by direct method invocation) or seda queues.

Pseudo routes would be something like:

from("someInput").process(...).to("seda:a")

from("seda:a").process(...).to("seda:b");

from("seda:b").process(...).to("seda:c");

Let's say you read the Json from a file and you want to process it inside a processor. Something like this:

from("file:/C:/TEST/")
.process(new MyProcessor())
.to("direct:anotherRouter");

MyProcessor class is a special kind of class that implements Processor. You need to override the process method.

public class MyProcessor implements Processor{

@Override
public void process(Exchange exchange) throws Exception {
}
}

In a camel route, the data is in the body. To get the data in a processor you should get it from the body. In the example, it is something like this

public class MyProcessor implements Processor{

@Override
public void process(Exchange exchange) throws Exception {

    String data = exchange.getIn().getBody(String.class);    
    ObjectMapper mapper = new ObjectMapper();
    JsonNode rootNode = mapper.readTree(data);
    //TODO: All the stuff you want to do with the data.
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM