Back to Examples

RabbitMQ service - SSL/TLS

The rabbitmq:Listener can be configured to connect to the server via SSL/TLS by providing a certificate file. The certificate can be provided through the secureSocket field of the connection configuration. Use this to secure the communication between the client and the server.

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,
    // To secure the client connection using TLS/SSL, the client needs to be configured with
    // a certificate file of the server.
    secureSocket = {
        cert: "../resource/path/to/public.crt"
    }
);

// 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_secure_connection.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 - SSL/TLS.

Related links

PreviousConstraint validation
NextBasic authentication