Back to Examples

RabbitMQ service - Basic authentication

The RabbitMQ authentication allows securing the client communication with the server. After an application connects to RabbitMQ and before it can perform operations, it must authenticate (i.e., present and prove its identity). In this example, the underlying connection of the listener is secured with Basic Authentication. A secured rabbitmq:Listener is created by using the default host and port or custom configurations and by providing the authentication details using the rabbitmq:Credentials record. Use it to authenticate client connections using a username and password.

import ballerina/log;
import ballerinax/rabbitmq;

public type Order record {
    int orderId;
    string productName;
    decimal price;
    boolean isValid;
};

listener rabbitmq:Listener orderListener = new (rabbitmq:DEFAULT_HOST, 5671,
    // Provide the credentials to secure the listener connections using username/password authentication.
    // with the `rabbitmq:Credentials` record.
    auth = {
        username: "alice",
        password: "alice@123"
    }
);

// The consumer service listens to the `OrderQueue` queue.
service "OrderQueue" on orderListener {

    remote function onMessage(Order 'order) returns error? {
        if 'order.isValid {
            log:printInfo(string `Received valid order for ${'order.productName}`);
        }
    }
}

Prerequisites

Run the service by executing the following command.

$ bal run rabbitmq_service_basic_auth.baltime = 2021-05-20T14:49:11.011+05:30 level = INFO module = "" message = "Received message: Hello from Ballerina"

Tip: You can invoke the above service via the RabbitMQ client - Basic authentication.

Related links

PreviousSSL/TLS
NextSSL/TLS