import ballerina/log;
import ballerinax/rabbitmq;

listener rabbitmq:Listener securedEP = new(rabbitmq:DEFAULT_HOST, 5671,

    // To secure the client connections using username/password authentication, provide the credentials
    // with the [`rabbitmq:Credentials`](https://docs.central.ballerina.io/ballerinax/rabbitmq/latest/records/Credentials) record.
    auth = {
         username: "alice",
         password: "alice@123"
    },

    // To secure the client connection using TLS/SSL, the client needs to be configured with
    // a certificate file of the server.
    // The [`rabbitmq:SecureSocket`](https://docs.central.ballerina.io/ballerinax/rabbitmq/latest/records/SecureSocket)
    // record provides the SSL-related configurations of the client.
    secureSocket = {
        cert: "../resource/path/to/public.crt"
    }
);

@rabbitmq:ServiceConfig {
    queueName: "Secured"
}
// Attaches the service to the listener.
service rabbitmq:Service on securedEP {
    remote function onMessage(rabbitmq:Message message) {
        string|error messageContent = string:fromBytes(message.content);
        if messageContent is string {
            log:printInfo("Received message: " + messageContent);
        }
    }
}
import ballerinax/rabbitmq;

public function main() returns error? {
    // Creates a ballerina RabbitMQ client with TLS/SSL and username/password authentication.
    rabbitmq:Client rabbitmqClient = check new(rabbitmq:DEFAULT_HOST, 5671,

        // To secure the client connections using username/password authentication, provide the credentials
        // with the [`rabbitmq:Credentials`](https://docs.central.ballerina.io/ballerinax/rabbitmq/latest/records/Credentials) record.
        auth = {
             username: "alice",
             password: "alice@123"
        },

        // To secure the client connection using TLS/SSL, the client needs to be configured with
        // a certificate file of the server.
        // The [`rabbitmq:SecureSocket`](https://docs.central.ballerina.io/ballerinax/rabbitmq/latest/records/SecureSocket)
        // record provides the SSL-related configurations of the client.
        secureSocket = {
            cert: "../resource/path/to/public.crt"
        }
    );

    // Declares the queue, Secured.
    check rabbitmqClient->queueDeclare("Secured");

    // Publishes the message using the `rabbitmqClient` and the routing key named `Secured`.
    string message = "Hello from Ballerina";
    check rabbitmqClient->publishMessage({ content: message.toBytes(),
                                            routingKey: "Secured" });
}

Secured connection

In this example, the underlying connections of the consumer and the producer are secured with TLS/SSL and basic authentication.

For more information on the underlying module, see the RabbitMQ module.

import ballerina/log;
import ballerinax/rabbitmq;
listener rabbitmq:Listener securedEP = new(rabbitmq:DEFAULT_HOST, 5671,
    auth = {
         username: "alice",
         password: "alice@123"
    },

To secure the client connections using username/password authentication, provide the credentials with the rabbitmq:Credentials record.

    secureSocket = {
        cert: "../resource/path/to/public.crt"
    }
);

To secure the client connection using TLS/SSL, the client needs to be configured with a certificate file of the server. The rabbitmq:SecureSocket record provides the SSL-related configurations of the client.

@rabbitmq:ServiceConfig {
    queueName: "Secured"
}
service rabbitmq:Service on securedEP {
    remote function onMessage(rabbitmq:Message message) {
        string|error messageContent = string:fromBytes(message.content);
        if messageContent is string {
            log:printInfo("Received message: " + messageContent);
        }
    }
}

Attaches the service to the listener.

bal run consumer.bal
time = 2021-05-20T14:49:11.011+05:30 level = INFO module = "" message = "Received message: Hello from Ballerina"
import ballerinax/rabbitmq;
public function main() returns error? {
    rabbitmq:Client rabbitmqClient = check new(rabbitmq:DEFAULT_HOST, 5671,

Creates a ballerina RabbitMQ client with TLS/SSL and username/password authentication.

        auth = {
             username: "alice",
             password: "alice@123"
        },

To secure the client connections using username/password authentication, provide the credentials with the rabbitmq:Credentials record.

        secureSocket = {
            cert: "../resource/path/to/public.crt"
        }
    );

To secure the client connection using TLS/SSL, the client needs to be configured with a certificate file of the server. The rabbitmq:SecureSocket record provides the SSL-related configurations of the client.

    check rabbitmqClient->queueDeclare("Secured");

Declares the queue, Secured.

    string message = "Hello from Ballerina";
    check rabbitmqClient->publishMessage({ content: message.toBytes(),
                                            routingKey: "Secured" });
}

Publishes the message using the rabbitmqClient and the routing key named Secured.

bal run producer.bal