The @QuarkusMain annotation identifies the entry point for a Quarkus command-mode application. Apply it to a class implementing QuarkusApplication, then implement run(String... args) to handle command-line invocation. section 4.10 section 4.8.2
A practical command-line application combines the annotation with:
- A class annotated with @QuarkusMain. section 4.8.2
- The QuarkusApplication interface and its run method, which receives command-line arguments. section 4.8.2
- Application logic such as a gRPC client, with asynchronous calls awaited before CLI invocation returns. section 4.8.2
Inventory command
An inventory CLI can annotate InventoryCommand with @QuarkusMain, implement QuarkusApplication, and invoke the configured inventory gRPC service. After packaging with ./mvnw clean package, run java -jar target/quarkus-app/quarkus-run.jar add KNIGHT Pontiac TransAM to insert a car. section 4.8.2
Listing 4.43 Consuming the gRPC service from the command line
package org.acme.inventory.client;
import io.quarkus.grpc.GrpcClient;
import io.quarkus.runtime.QuarkusApplication;
import io.quarkus.runtime.annotations.QuarkusMain;
import org.acme.inventory.model.InsertCarRequest;
import org.acme.inventory.model.InventoryService;
import org.acme.inventory.model.RemoveCarRequest;
@QuarkusMain #1
public class InventoryCommand
implements QuarkusApplication { #2
private static final String USAGE =
"Usage: inventory <add>|<remove> " +
"<license plate number> <manufacturer> <model>";
@GrpcClient("inventory")
InventoryService inventory;
@Override
public int run(String... args) { #3
String action =
args.length > 0 ? args[0] : null;
if ("add".equals(action) && args.length >= 4) {
add(args[1], args[2], args[3]);
return 0;
} else if ("remove".equals(action) && args.length >= 2) {
remove(args[1]);
return 0;
}
System.err.println(USAGE);
return 1;
}
public void add(String licensePlateNumber, String manufacturer,
String model) {
inventory.add(InsertCarRequest.newBuilder()
.setLicensePlateNumber(licensePlateNumber)
.setManufacturer(manufacturer)
.setModel(model)
.build())
.onItem().invoke(carResponse ->
System.out.println("Inserted new car " + carResponse))
.await().indefinitely();
}
public void remove(String licensePlateNumber) {
inventory.remove(RemoveCarRequest.newBuilder()
.setLicensePlateNumber(licensePlateNumber)
.build())
.onItem().invoke(carResponse ->
System.out.println("Removed car " + carResponse))
.await().indefinitely();
}
}
View the complete InventoryCommand listing, including the annotation, interface implementation, gRPC client, and argument handling. section 4.8.2
