A Command Line Application with Quarkus 📎
After the execution of the method run
quarkus will automatically shutdown:
import javax.inject.Inject;
import io.quarkus.runtime.QuarkusApplication;
import io.quarkus.runtime.annotations.QuarkusMain;
@QuarkusMain
public class SuperSonicApp implements QuarkusApplication {
@Inject
Messenger supplier;
@Override
public int run(String... args) throws Exception {
System.out.println("Message from messenger: " + this.supplier.message());
return 42; //exit code
}
}
Dependency injection, microprofile features etc. are working as expected.
The message
property defined in application.properties
:
message=hey duke
is injectable:
import javax.enterprise.context.Dependent;
import javax.inject.Inject;
import org.eclipse.microprofile.config.inject.ConfigProperty;
@Dependent
public class Messenger {
@Inject
@ConfigProperty(name = "message")
String message;
public String message() {
return this.message;
}
}
The application generates the following output and stops:
INFO [io.quarkus] (main) supersonic-main 1.0-SNAPSHOT (powered by Quarkus 1.4.2.Final) started in 0.248s.
INFO [io.quarkus] (main) Profile prod activated.
INFO [io.quarkus] (main) Installed features: [cdi]
Message from messenger: hey duke
INFO [io.quarkus] (main) supersonic-main stopped in 0.003s
Command mode already ships with quarkus runtime, there are no additional extensions needed. In the example above CDI and MicroProfile Config were used, which ship with:
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-arc</artifactId>
</dependency>
In code executed from the "main" method there are no incoming requests. Therefore scopes like e.g. @RequestScoped
are not available.
The request scope can be activated with: ActivateRequestContext CDI 2.0 annotation.