Marco Altieri
Marco Altieri

Reputation: 3818

Apache Camel routing API call to message queue

I have two applications that talk to each other using a REST API.

I would like to know if I can use Apache Camel as a proxy that could "persist" the API calls, for example storing them as messages in ActiveMQ, and then later route the requests to the actual API endpoint.

Practically, I would like to use Apache Camel to "enhance" the API endpoints adding persistence, throttling of requests, etc...

What component do you suggest to use?

Upvotes: 0

Views: 1913

Answers (1)

Jonathan Schoreels
Jonathan Schoreels

Reputation: 1720

You can always try to bridge your HTTP request into a queue, but making the thread wait by forcing the exchangePattern to InOut.

See this example :

import org.apache.activemq.broker.BrokerService;
import org.apache.camel.LoggingLevel;
import org.apache.camel.builder.RouteBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class Main {

    private static final Logger logger = LoggerFactory.getLogger(SimpleRouteBuilder.class);

    public static void main(String[] args) throws Exception {
        org.apache.camel.main.Main main = new org.apache.camel.main.Main();
        main.addRouteBuilder(new SimpleRouteBuilder());
        logger.info("Next call is blocking, ctrl-c to exit\n");
        main.run();
    }
}

class SimpleRouteBuilder extends RouteBuilder {

    private static final Logger logger = LoggerFactory.getLogger(SimpleRouteBuilder.class);

    public void configure() throws Exception {


        // launching an activemq in background
        final BrokerService broker = new BrokerService();
        broker.setBrokerName("activemq");
        broker.addConnector("tcp://localhost:61616");
        Runnable runnable = () -> {
            try {
                broker.start();
            } catch (Exception e) {
                e.printStackTrace();
            }
        };
        runnable.run();

        // receiving http request but queuing them
        from("jetty:http://127.0.0.1:10000/input")
            .log(LoggingLevel.INFO, logger, "received request")
            .to("activemq:queue:persist?exchangePattern=InOut"); // InOut has to be forced with JMS

        // dequeuing and calling backend
        from("activemq:queue:persist")
            .log(LoggingLevel.INFO, logger,"requesting to destination")
            .removeHeaders("CamelHttp*")
            .setHeader("Cache-Control",constant("private, max-age=0,no-store"))
            .to("jetty:http://perdu.com?httpMethod=GET");
    }

}

If you are using maven, here is the pom.xml :

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>be.jschoreels.camel</groupId>
    <artifactId>camel-simple</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-core</artifactId>
            <version>2.19.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-jms</artifactId>
            <version>2.19.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-jetty</artifactId>
            <version>2.19.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-camel</artifactId>
            <version>5.15.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-all</artifactId>
            <version>5.15.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.activemq/activemq-kahadb-store -->
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-kahadb-store</artifactId>
            <version>5.15.3</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.25</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.25</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
    </dependencies>


</project>

Upvotes: 1

Related Questions