Superfast subatomic java (Quarkus) — a HelloWorld app
https://quarkus.io/

Superfast subatomic java (Quarkus) — a HelloWorld app

As part of my quest to get my idea into the cloud as an app, I stumbled across this beautiful product called QUARKUS. Going through many pages of material on their site I have convinced my self into building my backend service of the application on Quarkus(Supersonic Subatomic java). Without further delay.

Note: Some images used here are referenced from https://quarkus.io.

Ready…Set…Go

Assuming your development machine has Gradle installed(by the time of writing I am using Gradle 5.3.1

Create a folder 'helloworld'

under the folder create these below three Gradle related files

"build.gradle"


buildscript {
    repositories {
        mavenLocal()
    }
    dependencies {
        classpath "io.quarkus:quarkus-gradle-plugin:${quarkusVersion}"
    }
}


plugins {
    id 'java'
}


apply plugin: 'io.quarkus'


repositories {
     mavenLocal()
     mavenCentral()
}


dependencies {
    implementation enforcedPlatform("io.quarkus:quarkus-bom:${quarkusVersion}")
    implementation 'io.quarkus:quarkus-resteasy'


    testImplementation 'io.quarkus:quarkus-junit5'
    testImplementation 'io.rest-assured:rest-assured'
}



"gradle.properties"

quarkusVersion = 0.24.0


"settings.gradle"



pluginManagement {
    repositories {
        mavenLocal()
        mavenCentral()
        gradlePluginPortal()
    }
    resolutionStrategy {
        eachPlugin {
            if (requested.id.id == 'io.quarkus') {
                useModule("io.quarkus:quarkus-gradle-plugin:${quarkusVersion}")
            }
        }
    }
}

and run below gradle command to create Gradle wrapper

gradle wrapper --gradle-version 5.6.2
No alt text provided for this image

Now we have a basic setup of the project, Let's write a hello world program which takes your name as input as query param and responds on calling via HTTP GET

First set a folder structure as below and create GreetingService.java and GreetingResource.java files.
No alt text provided for this image

Added following code to respective java files, These serves our HTTP requests which we are going to issue in a second.


"GreetingResource.java"


package org.rockey.hello;




import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;


@Path("/hello")
public class GreetingResource {


    @Inject
    GreetingService service;


    @GET
    @Produces(MediaType.TEXT_PLAIN)
    @Path("/greeting/{name}")
    public String greeting(@PathParam("name") String name) {
        return service.greeting(name);
    }


    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String hello() {
        return "hello";
    }
}


"GreetingService.java"

package org.rockey.hello;


import javax.enterprise.context.ApplicationScoped;


@ApplicationScoped
public class GreetingService {


    public String greeting(String name) {
        return "hello " + name;
    }


}


"index.html"

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>getting-started - 1.0-SNAPSHOT</title>
    <style>
        h1, h2, h3, h4, h5, h6 {
            margin-bottom: 0.5rem;
            font-weight: 400;
            line-height: 1.5;
        }


        h1 {
            font-size: 2.5rem;
        }


        h2 {
            font-size: 2rem
        }


        h3 {
            font-size: 1.75rem
        }


        h4 {
            font-size: 1.5rem
        }


        h5 {
            font-size: 1.25rem
        }


        h6 {
            font-size: 1rem
        }


        .lead {
            font-weight: 300;
            font-size: 2rem;
        }


        .banner {
            font-size: 2.7rem;
            margin: 0;
            padding: 2rem 1rem;
            background-color: #00A1E2;
            color: white;
        }


        body {
            margin: 0;
            font-family: -apple-system, system-ui, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";
        }


        code {
            font-family: SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
            font-size: 87.5%;
            color: #e83e8c;
            word-break: break-word;
        }


        .left-column {
            padding: .75rem;
            max-width: 75%;
            min-width: 55%;
        }


        .right-column {
            padding: .75rem;
            max-width: 25%;
        }


        .container {
            display: flex;
            width: 100%;
        }


        li {
            margin: 0.75rem;
        }


        .right-section {
            margin-left: 1rem;
            padding-left: 0.5rem;
        }


        .right-section h3 {
            padding-top: 0;
            font-weight: 200;
        }


        .right-section ul {
            border-left: 0.3rem solid #00A1E2;
            list-style-type: none;
            padding-left: 0;
        }


    </style>
</head>
<body>


<div class="banner lead">
    Your new Cloud-Native application is ready!
</div>


<div class="container">
    <div class="left-column">
        <p class="lead"> Congratulations, you have created a new Quarkus application.</p>


        <h2>Why do you see this?</h2>


        <p>This page is served by Quarkus. The source is in
            <code>src/main/resources/META-INF/resources/index.html</code>.</p>


        <h2>What can I do from here?</h2>


        <p>If not already done, run the application in <em>dev mode</em> using: <code>mvn compile quarkus:dev</code>.
        </p>
        <ul>
            <li>Add REST resources, Servlets, functions and other services in <code>src/main/java</code>.</li>
            <li>Your static assets are located in <code>src/main/resources/META-INF/resources</code>.</li>
            <li>Configure your application in <code>src/main/resources/META-INF/microprofile-config.properties</code>.
            </li>
        </ul>


        <h2>How do I get rid of this page?</h2>
        <p>Just delete the <code>src/main/resources/META-INF/resources/index.html</code> file.</p>
    </div>
    <div class="right-column">
        <div class="right-section">
            <h3>Application</h3>
            <ul>
                <li>GroupId: org.acme</li>
                <li>ArtifactId: getting-started</li>
                <li>Version: 1.0-SNAPSHOT</li>
                <li>Quarkus Version: 0.11.0</li>
            </ul>
        </div>
        <div class="right-section">
            <h3>Next steps</h3>
            <ul>
                <li><a >Setup your IDE</a></li>
                <li><a >Getting started</a></li>
                <li><a >Quarkus Web Site</a></li>
            </ul>
        </div>
    </div>
</div>




</body>
</html>


Now if you issue below command to start Quarkus in dev mode on https://localhost:8080

gradlew quarkusDev

From chrome browser enter below to see responses served from our java code.

https://localhost:8080/hello/greeting/rakesh

or

https://localhost:8080/hello/

One of the advantages of Quarkus is its live reload feature. Every time you make a change in the code our development server reloads the changes instantly. There are many advantages featured in quarkus website which we will uncover at a later point of time in another tutorial.

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

Rakesh Mothukuri的更多文章

  • Building an RSocket based Springboot application

    Building an RSocket based Springboot application

    Introduction RSocket translates to Reactive socket is a messaging protocol that works over TCP or Websockets…

  • The Command Pattern in?Java

    The Command Pattern in?Java

    Introduction You most likely have known about behavioural patterns by now. behavioral patterns are concerns about the…

  • An efficient way to compare strings in Go

    An efficient way to compare strings in Go

    Introduction One of the most overlooked operations is comparing two strings and this might not cause system overhead…

  • 8 Most used functions of strings package in Go

    8 Most used functions of strings package in Go

    Introduction The standard Go library strings package provides many string related functions. This blog lists…

  • Installing Go on Ubuntu

    Installing Go on Ubuntu

    This blog will walk through installing Go 1.14.

  • Multiple Return Values: Golang

    Multiple Return Values: Golang

    In Golang, It is allowed to return multiple values from a function, using a return statement. The type of the return…

    2 条评论
  • go build vs go run

    go build vs go run

    Example : https://gist.github.

  • WSL Ubuntu upgrade to Disco Dingo 19.04

    WSL Ubuntu upgrade to Disco Dingo 19.04

    After installing Ubuntu Microsoft app by default Ubuntu will install the latest stable Ubuntu LTS release. but if you…

社区洞察

其他会员也浏览了