Back to EIP

PatternAssure the health of messaging components by sending test messages. Note: Instead of the original pattern, we have selected a modern version where infrastructure polls for the health of the components.
How Ballerina helps

Ballerina provides built-in support for creating, propagating(check keyword), and handling errors. Error handling logic can be performed after testing if a given value is an error using the is keyword.

Test MessageMessage FilterMessage
Copy
import ballerina/http;
import ballerina/sql;
import ballerinax/mysql;
import ballerinax/mysql.driver as _;

service /customer on new http:Listener(8080) {
    private mysql:Client? db = null;
    boolean dbConnected = false;

    function init() {
        mysql:Client|error dbClient = new ("localhost", "admin", "adminpass", "CUSTOMER", 3000);
        if dbClient is mysql:Client {
            self.db = dbClient;
            self.dbConnected = true;
        }
    }

    resource function get phoneNumber(string id) returns string|http:InternalServerError|http:NotFound|error {
        mysql:Client? db = self.db;
        if db !is mysql:Client {
            return http:INTERNAL_SERVER_ERROR;
        }

        string|error result = db->queryRow(`SELECT number FROM customers WHERE id = ${id}`);
        if result is sql:NoRowsError {
            return http:NOT_FOUND;
        }
        return result;
    }

    resource function get heartbeat() returns http:Ok|http:InternalServerError {
        return self.dbConnected ? http:OK : http:INTERNAL_SERVER_ERROR;
    }
}