0

I'm getting Failed to create consumer endpoint issue, while asserting against the headers. Can someone help me on this ?

I have this file watcher route which consumes files and validate the file. If its valid file will update the headers and send it to s3 blob. Otherwise it will send it to error directory.

here are my routes:

  from("file-watch:test?events=CREATE&useFileHashing=true&antInclude=**/*.txt&recursive=true")
                .process(fileProcessor)
                .toD("direct:validateFile")
                .choice()
                .when(exchange -> exchange.getIn().getHeader("isValid").equals("valid"))
                .to("direct:updateMessageHeaders")
                .otherwise()
                .toD("direct:processErrorFiles")
                .endChoice()
                .end();

        from("direct:validateFile")
                .routeId("validateFile")
                .choice()
                .when(exchange -> Long.parseLong(exchange.getIn().getHeader("fileSize").toString()) > 0)
                .setHeader("isValid", simple("valid"))
                .otherwise()
                .setHeader("isValid", simple("invalid"))
                .endChoice()
                .end();
 final Processor fileProcessor = exchange -> {
        String fileName = exchange.getIn().getHeader("CamelFileAbsolutePath").toString();
        File gdisFile = new File(fileName);
        exchange.getIn().setHeader("fileSize", gdisFile.length());
    };

TestCase

public class RouteTest extends CamelTestSupport {

    @Override
    public RouteBuilder createRouteBuilder() throws Exception
    {
        return new Route();
    }
    @Test
    public void header_validation() {

        Map<String, Object> headers = new HashMap<>();
        headers.put("fileSize", 100);

        template.sendBodyAndHeaders("direct:validateFile", null, headers);
assertEquals("valid",consumer.receive("direct:validateFile").getIn().getHeader("isValid"));
    }
}

Exception

org.apache.camel.FailedToCreateConsumerException: Failed to create Consumer for endpoint: direct://validateFile. Reason: java.lang.IllegalArgumentException: Cannot add a 2nd consumer to the same endpoint: direct://validateFile. DirectEndpoint only allows one consumer.
at org.apache.camel.support.cache.DefaultConsumerCache.acquirePollingConsumer(DefaultConsumerCache.java:107)

TechGeek
  • 480
  • 1
  • 8
  • 22

2 Answers2

1

When you call

template.sendBodyAndHeaders

Camel performs fire-and-forget - it sends your data into the endpoint and does not wait the result.

You need to use

tamplate.requestBodyAndHeaders

This method waits the data to pass all routes and returns the result back to you.

bvn13
  • 154
  • 11
0

Use requestBodyAndHeader instead of sendBodyAndHeader so you get the response as return value and then you cans assert it.

See https://camel.apache.org/manual/producertemplate.html

Luca Burgazzoli
  • 1,216
  • 7
  • 9
  • Can I expect the response in consumer ? I'm trying something like this `template.requestBodyAndHeader("direct:validateFile", "fileSize", 0);`. I don't see response in either template or consumer. Where can I access it ? – TechGeek Jan 15 '22 at 02:38
  • I've amended the response with a link to the doc – Luca Burgazzoli Jan 15 '22 at 09:16
  • Object response = template.requestBodyAndHeader("direct:validateFile", "fileSize", 0); I tried this way, response returning null. Can you share your example how you tried? – TechGeek Jan 15 '22 at 13:06
  • this is because the requestBodyAndHeader returns the message body, in your case you are sending null and you are not setting anything in the route as a body so the result is null. You may try with something like template.requestBodyAndHeader("direct:validateFile", "fileSize", 0, Exchange.class); – Luca Burgazzoli Jan 15 '22 at 16:40
  • My goal is to test the whether its properly setting the header based on the `fileSize` header value I'm passing via `requestBodyAndHeader`. Is there a way I can assert the HeaderValue ` isValid` ? My business logic is to set `isValid` header `valid` in case `fileSize` header value is `>` 0. otherwise `inValid`. – TechGeek Jan 15 '22 at 16:48
  • I know, please check the javadoc for the producer template, you'll find out that you can get an exchange back (also, try out my suggestion) and then you can assert that the headers are properly set – Luca Burgazzoli Jan 15 '22 at 20:04
  • I did tried your suggestion. It still threw null pointer exception. Exchange exchange = template.requestBodyAndHeader("direct:validateFile", "fileSize", 0, Exchange.class); – TechGeek Jan 15 '22 at 20:19
  • Please check the linked documentation and the ProducerTemplate javadoc, there a re a number of method you can use. To get the exchnage you can use Exchange request(String endpointUri, Processor processor) – Luca Burgazzoli Jan 17 '22 at 08:07