import ballerina/http;
import ballerina/lang.runtime;
import ballerina/log;

http:Listener httpListener = check new (9090);

var helloService =  service object {

    resource function get sayHello(http:Caller caller, http:Request req) {
        // Send a response back to the caller.
        var respondResult = caller->respond("Hello, World!");
        if (respondResult is error) {
            log:printError("Error occurred when responding.", 
                'error = respondResult);
        }
    }

    // The resource function that will shutdown the server.
    resource function get shutDownServer(http:Caller caller, http:Request req) {
        // Send a response back to the caller.
        var respondResult = caller->respond("Shutting down the server");
        // Stop the listener.
        // This will be called automatically if the program exits by means of a system call.
        var stopResult = httpListener.gracefulStop();
        // Deregister the listener dynamically.
        runtime:deregisterListener(httpListener);
        // Handle the errors at the end.
        if (respondResult is error) {
            log:printError("Error occurred when responding.", 
                'error = respondResult);
        } 
        if (stopResult is error) {
            log:printError("Error occurred when stopping the listener. ", 
                'error = stopResult);
        }
    }
};

public function main() returns error? {
    // Attach the service to the listener.
    check httpListener.attach(helloService);
    // Start the listener.
    check httpListener.'start();
    // Register the listener dynamically.
    runtime:registerListener(httpListener);
}

Dynamic Listener

Dynamic listeners allow registering/deregistering a module listener dynamically. This example demonstrates how to register and deregister an HTTP listener and terminate it in the process.

import ballerina/http;
import ballerina/lang.runtime;
import ballerina/log;
http:Listener httpListener = check new (9090);
var helloService =  service object {
    resource function get sayHello(http:Caller caller, http:Request req) {
        var respondResult = caller->respond("Hello, World!");
        if (respondResult is error) {
            log:printError("Error occurred when responding.", 
                'error = respondResult);
        }
    }

Send a response back to the caller.

    resource function get shutDownServer(http:Caller caller, http:Request req) {

The resource function that will shutdown the server.

        var respondResult = caller->respond("Shutting down the server");

Send a response back to the caller.

        var stopResult = httpListener.gracefulStop();

Stop the listener. This will be called automatically if the program exits by means of a system call.

        runtime:deregisterListener(httpListener);

Deregister the listener dynamically.

        if (respondResult is error) {
            log:printError("Error occurred when responding.", 
                'error = respondResult);
        } 
        if (stopResult is error) {
            log:printError("Error occurred when stopping the listener. ", 
                'error = stopResult);
        }
    }
};

Handle the errors at the end.

public function main() returns error? {
    check httpListener.attach(helloService);

Attach the service to the listener.

    check httpListener.'start();

Start the listener.

    runtime:registerListener(httpListener);
}

Register the listener dynamically.

# Navigate to the directory that contains the
# 'dynamic_listener.bal' file, and run the 'bal run' command below.
bal run dynamic_listener.bal
[ballerina/http] started HTTP/WS listener 0.0.0.0:9090
# Invoke the service using "cURL".
curl http://localhost:9090/sayHello
Hello, World!
# Invoke the shutdown resource to deregister the listener.
curl http://localhost:9090/shutDownServer
Shutting down the server