Command Mode Applications
Camel Quarkus is typically used for creating integration applications that run as long living processes. In this chapter, we are going to have a look at a slightly different use case: utilizing Camel Quarkus for writing programs that exit by themselves after performing some desired tasks.
Where can this be useful?
The enterprise is full of scheduled batch processing. Say, some system exports some sort of reports daily at 4 a.m. Those need to be transformed (e.g. XML → Excel) and transferred somewhere else (e.g. via mail, FTP, etc.)
Camel is a perfect match for doing the transformation and the transfer. However, having a service running 24/7 for doing something useful just once a day would probably be an overkill. Combining a Cron job (or a similar scheduling technology) with a command line utility may suit much better in situations like this.
How to write a command line tool with Camel Quarkus
There is just one thing where it would differ from a stock Camel Quarkus application: setting an exit condition in application.properties.
The rest of the application - most notably the Route that performs the actual data transformation and transfer - will look the same as with a traditional Camel service.
A minimal Hello World!
Let’s go through a minimal example that just prints Hello World! to the console and exits.
The route definition could look like the following:
import org.apache.camel.builder.RouteBuilder;
public class CamelRoute extends RouteBuilder {
@Override
public void configure() {
from("timer:hello?delay=-1&repeatCount=1")
.setBody().constant("Hello World!")
.to("log:hello");
}
} Note that we use the timer component to trigger the route execution. The URI parameter delay=-1 causes the timer to be triggered with no initial delay and repeatCount=1 ensures that the route is executed just once.
But doing just the above would not make our application exit by itself. The camel.main.durationMax* family of configuration options (available via camel-quarkus-core) offers a way to solve that. E.g. we can set the following in application.properties
camel.main.durationMaxMessages = 1 to make the application exit after the route has processed a single message.
We need to add the dependencies for timer and log components:
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-log</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-timer</artifactId>
</dependency> Build the application with mvn clean package and run it on the command line:
java -jar target/quarkus-app/quarkus-run.jar The hello logger prints an exchange with the body Hello World! and the application exits after the route has processed the message.
Command line parameters
That was really trivial. How about greeting a person whose name is passed via a command line parameter? We can use Quarkus MicroProfile Config for that:
import jakarta.enterprise.context.ApplicationScoped;
import org.apache.camel.builder.RouteBuilder;
import org.eclipse.microprofile.config.inject.ConfigProperty;
@ApplicationScoped
public class CamelRoute extends RouteBuilder {
@ConfigProperty(name = "greeted.subject", defaultValue = "World")
String greetedSubject;
@Override
public void configure() {
from("timer:hello?delay=-1")
.setBody().constant("Hello " + greetedSubject + "!")
.to("log:hello");
}
} For the @ConfigProperty annotation to work, the CamelRoute class needs to be annotated with @ApplicationScoped.
Having the above in place, we can call the application with the -Dgreeted.subject=Joe parameter at the command line:
java -Dgreeted.subject=Joe -jar target/quarkus-app/quarkus-run.jar This time, the logged exchange has the body Hello Joe!.
Compiling the command line utility to a native executable
As usual with Camel Quarkus, the application can be compiled to native executable by activating the native profile. GraalVM with native-image command installed and GRAALVM_HOME environment variable set is required for that, see Building a native executable section of the Quarkus documentation.
export GRAALVM_HOME=...
mvn clean package -Dnative Running it is easy:
target/*-runner -Dgreeted.subject=Joe Source code
The sources of the application used in this section are based on the main-command-mode integration test in the Camel Quarkus source tree: https://github.com/apache/camel-quarkus/tree/main/integration-tests/main-command-mode
Bonus: The integration test module shows a way how to test command line applications.
Full control over starting Camel by invoking CamelMainApplication directly
If you need to perform some custom logic before starting Camel (or even not starting it at all), you can do so by implementing a @QuarkusMain class and starting CamelMainApplication yourself after your custom logic is executed.
E.g. you may want to output a joke instead of starting Camel each year on April 1st:
import io.quarkus.runtime.Quarkus;
import io.quarkus.runtime.annotations.QuarkusMain;
import org.apache.camel.quarkus.main.CamelMainApplication;
import java.time.LocalDate;
@QuarkusMain
public class Main {
public static void main(String... args) {
/* Your custom logic is executed before Camel is started */
LocalDate now = LocalDate.now();
if (now.getDayOfMonth() == 1 && now.getMonthValue() == 4) {
System.out.println("Haha!");
return;
}
/* Start Quarkus and the Camel Quarkus runtime */
Quarkus.run(CamelMainApplication.class, args);
}
} | It is recommended to perform very little logic in Java |
Read more about Quarkus command mode applications in Quarkus command mode guide.