I’ve been working with Apache Camel and Quarkus to create a route that returns the file count from a directory on an SFTP server. The route works fine the first time I run the program, as it correctly returns the file count in the directory (5 files), but only once. After that, I need to restart the program because it returns 0 every time I try to get the file count again. As I mentioned, this issue is resolved by restarting the program. Any ideas on how to fix this behavior?
this is my current route:
<code> from("direct:fileCount")
.setHeader("binaryFlag", constant(1))
.setProperty("fileCounter", constant(0))
.loopDoWhile(simple("${header.binaryFlag} == 1"))
.pollEnrich("sftp://[email protected]/uploads?password=password123&noop=true&include=.*",1000)
.choice()
.when(body().isNotNull())
.process(exchange -> {
Integer currentCount = exchange.getProperty("fileCounter", Integer.class);
exchange.setProperty("fileCounter", currentCount + 1);
exchange.getIn().setHeader("binaryFlag", 1); //Keep the loop alive
})
.endChoice()
.otherwise()
.process(exchange ->
exchange.getIn().setHeader("binaryFlag", 0)//Kills the loop
)
.endChoice()
.end()
.end()
.process(exchange -> {
Integer finalCount = exchange.getProperty("fileCounter", Integer.class);
exchange.getIn().setBody("fileCount: " + finalCount);
}) ;
</code>
<code> from("direct:fileCount")
.setHeader("binaryFlag", constant(1))
.setProperty("fileCounter", constant(0))
.loopDoWhile(simple("${header.binaryFlag} == 1"))
.pollEnrich("sftp://[email protected]/uploads?password=password123&noop=true&include=.*",1000)
.choice()
.when(body().isNotNull())
.process(exchange -> {
Integer currentCount = exchange.getProperty("fileCounter", Integer.class);
exchange.setProperty("fileCounter", currentCount + 1);
exchange.getIn().setHeader("binaryFlag", 1); //Keep the loop alive
})
.endChoice()
.otherwise()
.process(exchange ->
exchange.getIn().setHeader("binaryFlag", 0)//Kills the loop
)
.endChoice()
.end()
.end()
.process(exchange -> {
Integer finalCount = exchange.getProperty("fileCounter", Integer.class);
exchange.getIn().setBody("fileCount: " + finalCount);
}) ;
</code>
from("direct:fileCount")
.setHeader("binaryFlag", constant(1))
.setProperty("fileCounter", constant(0))
.loopDoWhile(simple("${header.binaryFlag} == 1"))
.pollEnrich("sftp://[email protected]/uploads?password=password123&noop=true&include=.*",1000)
.choice()
.when(body().isNotNull())
.process(exchange -> {
Integer currentCount = exchange.getProperty("fileCounter", Integer.class);
exchange.setProperty("fileCounter", currentCount + 1);
exchange.getIn().setHeader("binaryFlag", 1); //Keep the loop alive
})
.endChoice()
.otherwise()
.process(exchange ->
exchange.getIn().setHeader("binaryFlag", 0)//Kills the loop
)
.endChoice()
.end()
.end()
.process(exchange -> {
Integer finalCount = exchange.getProperty("fileCounter", Integer.class);
exchange.getIn().setBody("fileCount: " + finalCount);
}) ;
I expose the route via REST
<code> rest(urlBase)
.get("getFileCount")
.produces("application/json")
.to("direct:fileCount");
</code>
<code> rest(urlBase)
.get("getFileCount")
.produces("application/json")
.to("direct:fileCount");
</code>
rest(urlBase)
.get("getFileCount")
.produces("application/json")
.to("direct:fileCount");
2