Search code examples
javaspring-bootapache-camelpolling

Apache Camel in Spring Boot - Queue polling consumer on request


I am struggling to find a fully fledged example of how to use Apache Camel in Spring Boot framework for the purpose of a polling consumer.

I have looked at this: https://camel.apache.org/manual/latest/polling-consumer.html as well as this: https://camel.apache.org/components/latest/timer-component.html but the code examples are not wide enough for me to understand what it is that I need to do to accomplish my task in Java.

I'm typically a C# developer, so a lot of these small references to things don't make sense.

I am seeking an example of the following to do in Java including all the imports and other dependencies that are required to get this to work.

What I am trying to do, is the following

  • A web request is made to an endpoint, which should trigger the start of a polling consumer
  • The polling consumer needs to poll another web endpoint with a provided "ID" that needs to be sent to the consumer at the time that it is trigger.
  • The polling consumer should poll every X seconds (let's say 5 seconds).
  • Once a specific successful response is received from the endpoint we are polling, the consumer should stop polling and send a message to another web endpoint.

I would like to know if this is possible, and if so, can you provide a small example of everything that is needed to achieve this (as the documentation from the Camel website is extremely sparse in terms of imports and class structure etc.)?


Solution

  • After discussions with some fellow Java colleagues, they have assured me that this use case is not one that Camel is designed for. This is reason it was so difficult to find anything on the internet before I posted this question.

    For those that are seeking this answer via Google, the best suggested approach is to use a different tool or just use standard java.

    In my case, I ended up using plain old Java thread to achieve what was required. Once the request is received I simply start a new Runnable thread, that handles the checking of the result from the other service, sleeps for X seconds, and terminates when the response is successful.

    A simple example is below:

    Runnable runner = new Runnable() {
        @Override
        public void run() {
            boolean cont = true;
            while (cont) {
               cont = getResponseFromServer();
               try {
                  Thread.sleep(5000);
               } catch (Exception e) {
                  // we don't care about this, it just means this time it didn't sleep
               }
            }
        }
    }
    new Thread(runner).start();