Developing Java in Quarkus, an open source IDE

Developing Java in Quarkus, an open source IDE

In the previous articles, you learned about 6 requirements of cloud-native software and 4 things cloud-native Java must provide in terms of what the cloud-native Java applications look like. Now, you probably wonder which framework & tool enables developers to implement the advanced Java applications in the developer’s local machine without a steep learning curve. In this article, we will walk you through how Quarkus with the open-source Integrated development environment(IDE) - Visual Studio Code(VS Code) accelerates to develop not only traditional cloud-native Java stack but also serverless, reactive applications quicker, easier and more familiar methods.

Quarkus is a Kubernetes-native Java stack tailored for GraalVM and OpenJDK HotSpot. It’s crafted from best-of-breed Java libraries and standards with live coding, unified configuration, superfast to startup, small memory footprint, and unified imperative & reactive development. 

Generate Quarkus Project

Access Start Coding to generate a Quarkus project includes a RESTful endpoint. Leave all variables(i.e. Group, Artifact, Build Tool, Extensions) then click on Generate your application on the right top. 

Note that the RESTEasy JAX-RS extension is selected as default. 

No alt text provided for this image

The zip file will be downloaded automatically on your local machine then you can extract the file via the following command:

No alt text provided for this image

Install VS Code

Download Visual Studio Code binary here along with your operating system then install it on your local machine. Open the unzipped Quarkus project via VS Code command-line tool:

No alt text provided for this image

You will see the Apache Maven project structure with:

  • ExampleResource exposed on /hello
  • Associated JUnit test
  • Accessible landing page via https://localhost:8080
  • Dockerfile files for both Native compilation and JVM Hotspot
  • A unified application configuration file

Let’s add Quarkus tools to your IDE via VS Code extension feature:

No alt text provided for this image

Starting Live Coding

Let’s run the application using Quakrus development mode. To run the application, you need: 

  • JDK 1.8+ installed with JAVA_HOME configured appropriately
  • Apache Maven 3.6.3+

Move to code-with-quarkus directory then type mvn compile quarkus:dev in VS Code Terminal:

No alt text provided for this image

You will see that the Java application is running well with:

  • About 1 second to start-up
  • Live Coding Activated
  • EnabledCDI, RESTEASY features

When you access the endpoint via a web browser, you will the return code, hello:

No alt text provided for this image

Now, let’s change the code!!! Move back to the VS Code then open ExampleResource.java file in src/main/java/org/acme. Replace the return code with “Welcome, Cloud-Native Java with Quarkus!”. Don’t forget to Save the file! 

No alt text provided for this image

Go back to the web browser then Reload the page:

No alt text provided for this image

It’s Magic! Behind the scene, Quarkus did rebuild, package and deploy the application for YOU, developer automatically and magically and it only took a half-second. This is one of the required features in cloud-native Java runtimes in terms of Increasing development productivity:

No alt text provided for this image

Don’t stop running your cloud-native Java application in Quarkus.

Integrate Data Transaction via Quakrus Tool

Press F1 then click on Quarkus: Add extensions to current project to add an in-memory database(H2) transaction capability:

No alt text provided for this image

Type H2 then double-click on JDBC Driver - H2 Data in the search result:

Let’s add the following 3 extensions to simplify your persistence code and return JSON format data:

  • Hibernate ORM with Panache Data
  • JDBC Driver - H2
  • RESTEasy JSON-B Web

Press Enter to add those dependencies:

No alt text provided for this image

Then you will see the following message in a new VS Code Terminal:

? Adding dependency io.quarkus:quarkus-jdbc-h2:jar

? Adding dependency io.quarkus:quarkus-resteasy-jsonb:jar

? Adding dependency io.quarkus:quarkus-hibernate-orm-panache:jar

You also find the following pulled dependencies in POM.xml:

No alt text provided for this image

Create Inventory Entity

With your current project in place, let’s get to work defining the business logic.

The first step is to define the model (entity) of an Inventory object. Since Quarkus uses Hibernate ORM Panache, create Inventory.java file in src.main.java.org.acme directory and paste the following code into it:

No alt text provided for this image

Define the RESTful endpoint of Inventory

You will mirror the abstraction of service so that you can inject the Inventory service into various places (like a RESTful resource endpoint) in the future. Create InventoryResource.java file in src.main.java.org.acme directory and add this code to it:

No alt text provided for this image

Don’t forget to save the above files. Go back to your web browser and access a new endpoint, https://localhost:8080/services/inventory then you will see:

No alt text provided for this image

If you have an issue or error to implement the above example, you can reuse the codes here.

Conclusion

There are more practical and useful guides that show how to develop advanced cloud-native Java applications using Quarkus extensions with event-driven programming, serverless development, Kubernetes deployment.

要查看或添加评论,请登录

社区洞察

其他会员也浏览了