
Quarkus Cheat-Sheet What is Quarkus? Or in Kotlin: Command mode Quarkus is a Kubernetes Native Java stack tailored for GraalVM & plugins { You can dene the main CLI method to start Quarkus. There are two OpenJDK HotSpot, crafted from the best of breed Java libraries and java ways, implementing io.quarkus.runtime.QuarkusApplication standards. Also focused on developer experience, making things } interface or use the Java main method to launch Quarkus. just work with little to no conguration and allowing to do live apply(plugin = "io.quarkus") coding. @io.quarkus.runtime.annotations.QuarkusMain repositories { public class HelloWorldMain implements QuarkusApplication { Cheat-sheet tested with Quarkus 1.13.0.Final. mavenCentral() @Override } public int run(String... args) throws Exception { Getting Started System.out.println("Hello World"); dependencies { return 10; Quarkus comes with a Maven archetype to scaffold a very simple implementation(enforcedPlatform("io.quarkus:quarkus-bo } starting project. m:0.26.1")) } implementation("io.quarkus:quarkus-resteasy") } mvn io.quarkus:quarkus-maven-plugin:1.13.0.Final:create \ -DprojectGroupId=org.acme \ run method called when Quarkus starts, and stops when it nishes. -DprojectArtifactId=getting-started \ -DclassName="org.acme.quickstart.GreetingResource" \ Packaging As Java main : -Dpath="/hello" mvn clean package @QuarkusMain public class JavaMain { This creates a simple JAX-RS resource called GreetingResource . public static void main(String... args) { Quarkus.run(HelloWorldMain.class, args); You need to distribute the -runner.jar le together with quarkus- @Path("/hello") } app directory. public class GreetingResource { } If quarkus.package.uber-jar property is set to true, then a uber-jar is @GET created with all dependencies bundled inside the JAR. @Produces(MediaType.TEXT_PLAIN) public String hello() { By default, Quarkus uses fast jar packaging, if quarkus.package.type Use @QuarkusMain in only one place. return "hello"; property is set to legacy-jar then Quarkus creates the old standard } jar le. Use Quarkus.waitForExit() from the main thread if you want to run } application.properties some logic on startup, and then run like a normal application (i.e. not exit). quarkus.package.uber-jar=true Gradle quarkus.package.type=legacy-jar You can inject command line arguments by using @CommandLineArguments annotation: There is no way to scaffold a project in Gradle but you only need to do: To compile to native, you need to set GRAALVM_HOME environment @CommandLineArguments variable and run the native prole. String[] args; plugins { id 'java' mvn clean package -Pnative id 'io.quarkus' version '0.26.1' Picocli } ./gradlew build -Dquarkus.package.type=native You can use Picocli to implement CLI applications: repositories { mavenCentral() ./mvnw quarkus:add-extension Possible quarkus.package.type are: jar , legacy-jar , uber-jar and } -Dextensions="picocli" native . dependencies { AppCDS implementation enforcedPlatform('io.quarkus:quarkus-bo m:0.26.1') Automatically generate AppCDS as part of the build process set the implementation 'io.quarkus:quarkus-resteasy' next property: quarkus.package.create-appcds=true . } To make use of it, just run java -jar -XX:SharedArchiveFile=app- cds.jsa myapp.jar . @CommandLine.Command @CommandLine.Command Dev Mode public class HelloCommand implements Runnable { public class EntryCommand implements Runnable { @CommandLine.Option(names = "-c", description = "JDBC c ./mvnw compile quarkus:dev @CommandLine.Option(names = {"-n", "--name"}, descripti onnection string") on = "Who will we greet?", defaultValue = "World") String connectionString; ./gradlew quarkusDev String name; @Inject private final GreetingService greetingService; DataSource dataSource; } Endpoints are registered automatically to provide some basic public HelloCommand(GreetingService greetingService) { debug info in dev mode: this.greetingService = greetingService; @ApplicationScoped HTTP GET /quarkus/arc/beans } class DatasourceConfiguration { Query Parameters: scope , beanClass , kind . @Override @Produces public void run() { @ApplicationScoped HTTP GET /quarkus/arc/observers greetingService.sayHello(name); DataSource dataSource(CommandLine.ParseResult parseResu } lt) { Dev UI } System.out.println(parseResult.matchedOption("c").g etValue().toString()); Quarkus adds a Dev UI console to expose extension features. } } All classes annotated with picocli.CommandLine.Command are The Dev UI is available in dev mode only and accessible at the registered as CDI beans. /q/dev endpoint by default. If only one class annotated with picocli.CommandLine.Command it will Extensions Adding Conguration Parameters be used as entry point. If you want to provide your own @QuarkusMain : Quarkus comes with extensions to integrate with some libraries To add conguration to your application, Quarkus relies on such as JSON-B, Camel or MicroProle spec. To list all available MicroProle Cong spec. @QuarkusMain extensions just run: @CommandLine.Command(name = "demo", mixinStandardHelpOption @ConfigProperty(name = "greetings.message") s = true) ./mvnw quarkus:list-extensions String message; public class ExampleApp implements Runnable, QuarkusApplica tion { @ConfigProperty(name = "greetings.message", defaultValue = "Hello") @Inject You can use -DsearchPattern=panache to lter out all String messageWithDefault; CommandLine.IFactory factory; extensions except the ones matching the expression. @ConfigProperty(name = "greetings.message") @Override And to register the extensions into build tool: Optional<String> optionalMessage; public void run() { } ./mvnw quarkus:add-extension -Dextensions="" @Override Properties can be set (in decreasing priority) as: ./mvnw quarkus:remove-extension -Dextensions="" public int run(String... args) throws Exception { return new CommandLine(this, factory).execute(arg System properties ( -Dgreetings.message ). s); } extensions property supports CSV format to register more Environment variables ( GREETINGS_MESSAGE ). } than one extension at once. Environment le named .env placed in the current working directory ( GREETING_MESSAGE= ). Application Lifecycle Use quarkus.picocli.native-image.processing.enable to false to use External cong directory under the current working directory: the picocli-codegen annotation processor instead of build steps. You can be notied when the application starts/stops by observing config/application.properties . StartupEvent and ShutdownEvent events. You can also congure CDI beans with PicoCLI arguments: Resources src/main/resources/application.properties . @ApplicationScoped public class ApplicationLifecycle { greetings.message = Hello World void onStart(@Observes StartupEvent event) {} void onStop(@Observes ShutdownEvent event) {} } Array , List and Set are supported. The delimiter is comma ( , ) char and \ is the escape char. Quarkus supports graceful shutdown. By default there is no timeout Conguration Proles but can be set by using the quarkus.shutdown.timeout cong Quarkus allow you to have multiple conguration in the same le @ConfigProperty(name = "quarkus.application.name") @ConfigProperties(prefix = "greeting", namingStrategy=Namin ( application.properties ). String applicationName; gStrategy.KEBAB_CASE) public class GreetingConfiguration { The syntax for this is %{profile}.config.key=value . public String message; Additional locations public HiddenConfig hidden; quarkus.http.port=9090 %dev.quarkus.http.port=8181 You can use smallrye.config.locations property to set additional public static class HiddenConfig { conguration les. public List<String> recipients; } HTTP port will be 9090, unless the 'dev' prole is active. } smallrye.config.locations=config.properties Default proles are: And an application.properties mapping previous class: dev : Activated when in development mode ( quarkus:dev ). or test : Activated when running tests. greeting.message = hello java -jar -Dsmallrye.config.locations=config.properties greeting.hidden.recipients=Jane,John prod : The default prole when not running in development or test mode You can embed conguration les inside a dependency by adding Bean Validation is also supported so properties are validated at You can create custom prole names by enabling the prole either META-INF/microprofile.properties inside the JAR. When dependency startup time, for example @Size(min = 20) public String message; . setting quarkus.profile system property or QUARKUS_PROFILE is added to the application, conguration properties are merged environment variable. with current conguration. prefix attribute is not mandatory. If not provided, attribute is determined by class name (ie GreeetingConfiguration is quarkus.http.port=9090 @CongProperties translated to greeting or GreetingExtraConfiguration to %staging.quarkus.http.port=9999 As an alternative to injecting multiple related conguration values, greeting-extra ). The sux of the class is always removed. you can also use the @io.quarkus.arc.config.ConfigProperties annotation to group properties. Naming strategy can be changed with property namingStrategy . And enable it quarkus.profile=staging . KEBAB_CASE (whatever.foo-bar) or VERBATIM (whatever.fooBar). @ConfigProperties(prefix = "greeting", namingStrategy=Namin To get the active prole programmatically use @io.quarkus.arc.config.ConfigIgnore annotation can be used to gStrategy.KEBAB_CASE) io.quarkus.runtime.configuration.ProfileManager.getActiveProfile() . ignore the injection of conguration elements. public class GreetingConfiguration { private String
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages62 Page
-
File Size-