Writing a Serverless HelloWorld function

If you have been around a while in JavaEE / JakartaEE land you are aware of the Servlet API. In this example we are going to leverage the Servlet API, which will be delivered by Manorrock Piranha, to write a 'Hello World' function. Then we will use GraalVM to create a native image that can be used by your Serverless framework.

Create the Hello World function

We will start by creating a Java file named HelloWorldFunction.java in the helloworld package and add the content as described below.

public class HelloWorldFunction extends HttpServlet {

     * Stores the boolean to terminate.
    private boolean done = false;
     * Process the request.
     * @param request the request.
     * @param response the response.
     * @throws IOException when an I/O error occurs.
     * @throws ServletException when a Servlet error occurs.
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        PrintWriter writer = response.getWriter();
        writer.println("Hello World");
        done = true;

     * Are we done?
     * @return true if we are, false otherwise.
    public boolean isDone() {
        return done;

For anyone familiar writing Servlets there is nothing really exciting happening.

Add Manorrock Piranha boot strapping

Now we are going to make it a bit more exciting by adding the main method as shown below.

     * Main method.
     * @param arguments the arguments.
    public static void main(String[] arguments) {
        DefaultWebApplicationServer server = new DefaultWebApplicationServer();
        HttpServer httpServer = new DefaultHttpServer(8080, server);
        DefaultWebApplication webApp = new DefaultWebApplication();
        webApp.addServlet("function", new Function());
        webApp.addServletMapping("function", "/*");
        while (true) {
            try {
            } catch (InterruptedException ie) {

            if (function.isDone()) {

The code above exposes the Servlet on port 8080 and waits for the a client to connect to it and once the response is done it will terminate.

Create the Maven project to create a standalone JAR

Now we are going create a Maven project where we create a JAR file with all dependencies into one JAR so we can process it later with the GraalVM native image tool.

<project xmlns="http://maven.apache.org/POM/4.0.0"
    <name>A 'Hello World' function</name>

Create the standalone executable using GraalVM

To create the standalone executable we will process it with GraalVM native-image binary. Add the snippet below to the Maven POM file.

                    <exec dir="target" executable="${GRAALVM_HOME}/bin/native-image">
                        <arg value="-cp"/>
                        <arg value="../target/helloworld-jar-with-dependencies.jar"/>
                        <arg value="-H:Name=helloWorld"/>
                        <arg value="-H:Class=helloworld.HelloWorldFunction"/>
                        <arg value="-H:+ReportUnsupportedElementsAtRuntime"/>

Note the GRAALVM_HOME environment variable should be set to point to the GraalVM SDK on your machine.

Building the function binary

Now it is time to generate the binary by executing the command line below.

    mvn clean install

Once the build is done you will find the helloWorld binary in the target directory.

Testing the function binary

From the main project directory you can start it using the command line below


Point your browser to http://localhost:8080/ and you should see the response as shown as below.

    Hello World

You will also notice the helloWorld binary terminates after serving the response. If you do not want that to happen remove the code that deals with and it will stay running to serve more requests until you kill it or your Serverless framework kills it for you.

And voila you now have a simple starting point for writing more Servlet API based serverless functions.

Project resources

If you want a copy of the entire project click here to download it.

Note also read the follow up that changes this example a bit to not host a port, but instead consumes standard input and produces standard output.

Posted December 15th, 2018