Basic Quarkus Hands-on Lab (Version 2021-02-10)
2Getting Started with Quarkus
In this step, you will create a straightforward application serving a hello
endpoint. To demonstrate dependency injection this endpoint uses a greeting
bean.
This IDE is based on Eclipse Che (which is in turn based on MicroSoft VS Code editor).
You can see icons on the left for navigating between project explorer, search, version control (e.g. Git), debugging, and other plugins. You’ll use these during the course of this workshop. Feel free to click on them and see what they do:
If things get weird or your browser appears, you can simply reload the browser tab to refresh the view. |
Many features of CodeReady Workspaces are accessed via Commands. You can see a few of the commands listed with links on the home page (e.g. New File.., Git Clone.., and others).
If you ever need to run commands that you don’t see in a menu, you can press F1 to open the command window, or the more traditional Control+SHIFT+P (or Command+SHIFT+P on Mac OS X).
Import Project
Let’s import our project. Click on Git Clone.. (or type F1, enter 'git' and click on the auto-completed Git Clone.. )
Step through the prompts, using the following value for Repository URL. If you use FireFox, it may end up pasting extra spaces at the end, so just press backspace after pasting:
https://github.com/RedHat-Middleware-Workshops/quarkus-workshop-m1m2-labs
Click on Select Repository Location then click on Open in New Window. It will reload your web browser immediately:
The project is imported into your workspace and is visible in the project explorer (click on the top-most icon for project explorer):
The Terminal window in CodeReady Workspaces. You can open a terminal window for any of the containers running in your Developer workspace. For the rest of these labs, anytime you need to run a command in a terminal, you can use the >_ New Terminal command on the right: |
IMPORTANT: Check out proper Git branch
To make sure you’re using the right version of the project files, run this command in a CodeReady Terminal:
cd $CHE_PROJECTS_ROOT/quarkus-workshop-m1m2-labs && git checkout ocp-4.6
The project has
The Maven structure
An
org.acme.people.rest.GreetingResource
resource exposed on/hello
, along with a simple testA landing page that is accessible on
http://localhost:8080
after starting the applicationThe application configuration file
Other source files we’ll use later
Navigate to src → main → java → org.acme.people.rest
in the project tree and double click on GreetingResource.java
.
This class has a very simple RESTful endpoint definition:
@Path("/hello")
public class GreetingResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
return "hello";
}
}
It’s a very simple REST endpoint, returning "hello" to requests on /hello
.
Compared to vanilla JAX-RS, with Quarkus there is no need to create an |
Running the Application in Live Coding Mode
Live Coding (also referred to as dev mode) allows us to run the app and make changes on the fly. Quarkus will automatically re-compile and reload the app when changes are made. This is a powerful and efficient style of developing that you will use throughout the lab.
You can always use the mvn
(Maven) commands to run Quarkus apps, but we’ve created a few helpful shortcuts on the right to run various Maven commands.
Start the app by clicking on Live Coding:
This will compile and run the app using mvn compile quarkus:dev
in a Terminal window. Leave this terminal window open throughout the lab! You will complete the entire lab without shutting down Quarkus Live Coding mode, so be careful not to close the tab (if you do, you re-run it). This is very useful for quick expermentation.
The first time you build the app, new dependencies may be downloaded via maven. This should only happen once, after that things will go even faster |
You may see WARNINGs like |
You should see:
2020-11-03 14:27:00,102 INFO [io.quarkus] (Quarkus Main Thread) people 1.0-SNAPSHOT on JVM (powered by Quarkus x.x.x) started in 0.972s. Listening on: http://0.0.0.0:8080
2020-11-03 14:27:00,102 INFO [io.quarkus] (Quarkus Main Thread) Profile dev activated. Live Coding activated.
2020-11-03 14:27:00,103 INFO [io.quarkus] (Quarkus Main Thread) Installed features: [cdi, resteasy]
Note the amazingly fast startup time! The app is now running "locally" (within the Che container in which the workspace is also running). localhost
refers to the Kubernetes pod, not "your" laptop (so therefore opening localhost:8080 in your browser will not do anything).
CodeReady will also detect that the Quarkus app opens port 5005
(for debugging) and 8080
(for web requests). Do not open port 5005, but when prompted, open the port 8080
, which opens a small web browser in CodeReady:
You should see the default Quarkus welcome page (you may need to click the reload icon):
Open a new CodeReady Workspaces Terminal:
and invoke the hello
endpoint using the following curl command:
curl http://localhost:8080/hello
You can also click on the URL link at the upper right to open the same default page in a separate browser tab:
Add /hello
in your browser to see the same result as the curl command:
Now, let’s exercise the live reload capabilities of Quarkus. In CodeReady, open the GreetingResource.java
file (in src/main/java/org/acme/people/rest
) and change return "hello";
to return "hola";
in the editor. After making this change, reload the same brower tab that was showing hello
. It should now show hola
.
Wow, how cool is that? Supersonic Subatomic live reload! Go ahead and change it a few more times and access the endpoint again. And we’re just getting started. Leave the app running so we can continue to change it on the fly in the next section.
|
This will also listen for a debugger on port |
Package the app
Quarkus apps can be packaged as an executable JAR file or a native binary. We’ll cover native binaries later, so for now, let’s package as an executable JAR.
Click on 'Package app for OpenShift':
This produces an executable jar file in the target/
directory:
people-1.0-SNAPSHOT-runner.jar
- being an executable jar. Be aware that it’s not an über-jar as the dependencies are copied into thetarget/lib
directory.
Run the executable JAR
Run the packaged application. In a Terminal, run the following command:
java -Dquarkus.http.port=8081 -jar $CHE_PROJECTS_ROOT/quarkus-workshop-m1m2-labs/target/*-runner.jar
We use |
With the app running, open a separate terminal window, and ensure the app is running by executing a curl
command:
curl http://localhost:8081/hello
You should see:
hola
Cleanup
Go back to the terminal in which you ran the app with java -jar
and stop the app by pressing CTRL+C. Be sure not to close the "Live Coding" terminal!
Congratulations!
You’ve seen how to build a basic app, package it as an executable JAR and start it up very quickly. The JAR file can be used like any other executable JAR file (e.g. running it as-is, packaging as a Linux container, etc.)
In the next step we’ll inject a custom bean to showcase Quarkus' CDI capabilities.
No comments:
Post a Comment