Giter Site home page Giter Site logo

ballerina-platform / module-ballerinax-rabbitmq Goto Github PK

View Code? Open in Web Editor NEW
111.0 71.0 27.0 26.57 MB

Ballerina RabbitMQ Module.

Home Page: https://ballerina.io/

License: Apache License 2.0

Java 48.42% Ballerina 51.17% Shell 0.41%
ballerina rabbitmq messaging

module-ballerinax-rabbitmq's Introduction

Ballerina RabbitMQ Library

Build codecov Trivy GraalVM Check GitHub Last Commit

This library provides the capability to send and receive messages by connecting to the RabbitMQ server.

RabbitMQ gives your applications a common platform to send and receive messages and a safe place for your messages to live until received. RabbitMQ is one of the most popular open-source message brokers. It is lightweight and easy to deploy on-premise and in the cloud.

Basic usage

Set up the connection

First, you need to set up the connection with the RabbitMQ server. The following ways can be used to connect to a RabbitMQ server.

  1. Connect to a RabbitMQ node with the default host and port:
    rabbitmq:Client rabbitmqClient = check new(rabbitmq:DEFAULT_HOST, rabbitmq:DEFAULT_PORT);
  1. Connect to a RabbitMQ node with a custom host and port:
    rabbitmq:Client rabbitmqClient = check new("localhost", 5672);
  1. Connect to a RabbitMQ node with host, port, and additional configurations:
    rabbitmq:ConnectionConfiguration config = {
        username: "ballerina",
        password: "password"
    };
    rabbitmq:Client rabbitmqClient = check new("localhost", 5672, configs);

The rabbitmq:Client can now be used to send and receive messages as described in the subsequent sections.

Exchanges and queues

Client applications work with exchanges and queues, which are the high-level building blocks of the AMQP protocol. These must be declared before they can be used. The following code declares an exchange and a server-named queue and then binds them together.

    check rabbitmqClient->exchangeDeclare("MyExchange", rabbitmq:DIRECT_EXCHANGE);
    check rabbitmqClient->queueDeclare("MyQueue");
    check rabbitmqClient->queueBind("MyQueue", "MyExchange", "routing-key");

This sample code will declare,

  • a durable auto-delete exchange of the type rabbitmq:DIRECT_EXCHANGE
  • a non-durable, exclusive auto-delete queue with an auto-generated name

Next, the queueBind function is called to bind the queue to the exchange with the given routing key.

    check rabbitmqClient->exchangeDeclare("MyExchange", rabbitmq:DIRECT_EXCHANGE);
    check rabbitmqClient->queueDeclare("MyQueue", { durable: true,
                                                   exclusive: false,
                                                   autoDelete: false });
    check rabbitmqClient->queueBind("MyQueue", "MyExchange", "routing-key");

This sample code will declare,

  • a durable auto-delete exchange of the type rabbitmq:DIRECT_EXCHANGE
  • a durable, non-exclusive, non-auto-delete queue with a well-known name

Delete entities and purge queues

  • Delete a queue:
    check rabbitmqClient->queueDelete("MyQueue");
  • Delete a queue only if it is empty:
    check rabbitmqClient->queueDelete("MyQueue", false, true);
  • Delete a queue only if it is unused (does not have any consumers):
    check rabbitmqClient->queueDelete("MyQueue", true, false);
  • Delete an exchange:
    check rabbitmqClient->exchangeDelete("MyExchange");
  • Purge a queue (delete all of its messages):
    check rabbitmqClient->queuePurge("MyQueue");

Publish messages

To publish a message to an exchange, use the publishMessage() function as follows:

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

Setting other properties of the message such as routing headers can be done by using the BasicProperties record with the appropriate values.

    rabbitmq:BasicProperties props = {
       replyTo: "reply-queue"  
    };
    string message = "Hello from Ballerina";
    check rabbitmqClient->publishMessage({ content: message.toBytes(), routingKey: queueName, properties: props });

Consume messages using consumer services

The most efficient way to receive messages is to set up a subscription using a Ballerina RabbitMQ rabbitmq:Listener and any number of consumer services. The messages will then be delivered automatically as they arrive rather than having to be explicitly requested. Multiple consumer services can be bound to one Ballerina RabbitMQ rabbitmq:Listener. The queue to which the service is listening is configured in the rabbitmq:ServiceConfig annotation of the service or else as the name of the service.

  1. Listen to incoming messages with the onMessage remote method:
    listener rabbitmq:Listener channelListener= new(rabbitmq:DEFAULT_HOST, rabbitmq:DEFAULT_PORT);
    
    @rabbitmq:ServiceConfig {
        queueName: "MyQueue"
    }
    service rabbitmq:Service on channelListener {
        remote function onMessage(rabbitmq:AnydataMessage message) {
        }
    }
  1. Listen to incoming messages and reply directly with the onRequest remote method:
    listener rabbitmq:Listener channelListener= new(rabbitmq:DEFAULT_HOST, rabbitmq:DEFAULT_PORT);
    
    @rabbitmq:ServiceConfig {
        queueName: "MyQueue"
    }
    service rabbitmq:Service on channelListener {
        remote function onRequest(rabbitmq:AnydataMessage message) returns string {
            return "Hello Back!";
        }
    }

The rabbitmq:AnydataMessage record received can be used to retrieve its contents.

Advanced usage

Client acknowledgements

The message consuming is supported by mainly two types of acknowledgement modes, which are auto acknowledgements and client acknowledgements. Client acknowledgements can further be divided into two different types as positive and negative acknowledgements. The default acknowledgement mode is auto-ack (messages are acknowledged immediately after consuming). The following examples show the usage of positive and negative acknowledgements.

WARNING: To ensure the reliability of receiving messages, use the client-ack mode.

  1. Positive client acknowledgement:
    listener rabbitmq:Listener channelListener= new(rabbitmq:DEFAULT_HOST, rabbitmq:DEFAULT_PORT);
    
    @rabbitmq:ServiceConfig {
        queueName: "MyQueue",
        autoAck: false
    }
    service rabbitmq:Service on channelListener {
        remote function onMessage(rabbitmq:AnydataMessage message, rabbitmq:Caller caller) {
            rabbitmq:Error? result = caller->basicAck();
        }
    }
  1. Negative client acknowledgement:
    listener rabbitmq:Listener channelListener= new(rabbitmq:DEFAULT_HOST, rabbitmq:DEFAULT_PORT);
    
    @rabbitmq:ServiceConfig {
        queueName: "MyQueue",
        autoAck: false
    }
    service rabbitmq:Service on channelListener {
        remote function onMessage(rabbitmq:AnydataMessage message) {
            rabbitmq:Error? result = caller->basicNack(true, requeue = false);
        }
    }

The negatively-acknowledged (rejected) messages can be re-queued by setting the requeue to true.

Issues and projects

Issues and Projects tabs are disabled for this repository as this is part of the Ballerina Standard Library. To report bugs, request new features, start new discussions, view project boards, etc. please visit Ballerina Standard Library parent repository.

This repository only contains the source code for the library.

Build from the source

Set up the prerequisites

  • Download and install Java SE Development Kit (JDK) version 17 (from one of the following locations).

    • Oracle

    • OpenJDK

      Note: Set the JAVA_HOME environment variable to the path name of the directory into which you installed JDK.

  1. Download and install Docker as follows. (The RabbitMQ library is tested with a docker-based integration test environment. The before suite initializes the docker container before executing the tests).

    • Installing Docker on Linux

      Note: These commands retrieve content from the get.docker.com website in a quiet output-document mode and installs it.

       wget -qO- https://get.docker.com/ | sh
      
    • For instructions on installing Docker on Mac, go to Get Started with Docker for Mac.

    • For information on installing Docker on Windows, goo to Get Started with Docker for Windows.

Build the source

Execute the commands below to build from source.

  1. To build the library:

    ./gradlew clean build
    
  2. To run the tests:

    ./gradlew clean test
    
  3. To build the library without the tests:

    ./gradlew clean build -x test
    
  4. To debug package implementation:

    ./gradlew clean build -Pdebug=<port>
    
  5. To debug the library with Ballerina language:

    ./gradlew clean build -PbalJavaDebug=<port>
    
  6. Publish ZIP artifact to the local .m2 repository:

    ./gradlew clean build publishToMavenLocal
    
  7. Publish the generated artifacts to the local Ballerina central repository:

    ./gradlew clean build -PpublishToLocalCentral=true
    
  8. Publish the generated artifacts to the Ballerina central repository:

    ./gradlew clean build -PpublishToCentral=true
    

Contribute to Ballerina

As an open source project, Ballerina welcomes contributions from the community.

For more information, go to the contribution guidelines.

Code of conduct

All contributors are encouraged to read the Ballerina Code of Conduct.

Useful links

module-ballerinax-rabbitmq's People

Contributors

aashikam avatar anoukh avatar ballerina-bot avatar buddhiwathsala avatar dilansachi avatar gabilang avatar gimantha avatar hasithaa avatar hevayo avatar hindujab avatar kalaiyarasiganeshalingam avatar keizer619 avatar ldclakmal avatar madhukaharith92 avatar manuranga avatar maryamzi avatar mohamedsabthar avatar nadundesilva avatar niveathika avatar praneesha avatar pubudu91 avatar rdhananjaya avatar riyafa avatar rpjayasekara avatar sasindudilshara avatar shafreenanfar avatar tharmigank avatar thisaruguruge avatar warunalakshitha avatar wggihan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.