Adriano Ferreira
Adriano Ferreira

Reputation: 13

Apache Camel: How to rebuild the url based on the old one or the header

I am using route on the camel that starts a server that is used as an access point to request, re-directions, gateway to database, etc. And I want to redirect a get request to another service that is in another server and compose the url based on the request. I have made a processor that gets the header and puts in the new url. However the new url does not get executed...

Here is the code:

CamelContext context = new DefaultCamelContext();

ConnectionFactory connectionFactory =
    new ActiveMQConnectionFactory("vm://localhost?create=false");
context.addComponent("activemq", JmsComponent.jmsComponentAutoAcknowledge(connectionFactory));
context.start();

Processor Redir = new RedirectProcess();
from("jetty:http://localhost:8080/Middleware")   
    .choice()
    .when(header("redir")).process(Redir)
    .end()

And the Processor

public class RedirectProcess implements Processor {
    String value = null;
    String Head;

    public void process(Exchange inExchange) throws Exception {
        Head = inExchange.getIn().getHeader("redir").toString();
        CamelContext camelContext = new DefaultCamelContext();
        camelContext.addRoutes(route());
        camelContext.start();
        ProducerTemplate template = camelContext.createProducerTemplate();
        template.sendBody("direct:start", "Hello Camel");
        System.out.println(Head);
    }

    public RouteBuilder route() {
        return new RouteBuilder() {
            public void configure() {
                // you can configure the route rule with Java DSL here
                System.out.println("Passed HERE!");
                from("direct:start")
                    .to("http://localhost:8081/FunctLayer/tool/start/tool/" + Head + "");
            }
        };
    }
}

Upvotes: 1

Views: 865

Answers (1)

Sergey
Sergey

Reputation: 1361

It does not work like this. Don't try to create contexts nor routes in runtime. Use Recipient List pattern (http://camel.apache.org/recipient-list.html).

Your code would look like:

    CamelContext context = new DefaultCamelContext();

    ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost?create=false");
                    context.addComponent("activemq",JmsComponent.jmsComponentAutoAcknowledge(connectionFactory));

    context.start();
from("jetty:http://localhost:8080/Middleware")   
    .choice()
        .when(header("redir"))
            .recipientList(simple("http://localhost:8081/FunctLayer/tool/start/tool/${header.redir}"))
        .end()

Upvotes: 1

Related Questions