rabbit_borough

Crates.iorabbit_borough
lib.rsrabbit_borough
version0.1.7
sourcesrc
created_at2020-06-03 22:45:53.508871
updated_at2022-10-04 08:54:06.072735
descriptionCreate a RabbitMQ consumer project with minimal effort, by bypassing templating, configuration and complicated resiliency logic
homepage
repositoryhttps://github.com/elasticrash/rabbit-borough
max_upload_size
id249820
size76,424
Stefanos Kouroupis (elasticrash)

documentation

https://docs.rs/rabbit_borough

README

Rabbit-Borough

Rust A rabbit MQ abstraction build upon Lapin

Consumer example

fn main() {
    let config: JSONConfiguration = configuration::reader::read("./config.json").unwrap();
    println!("[{}] - Configuration read", line!(),);

    LocalPool::new().run_until(async {
        consume(&config, &handler).await;
    })
}

fn handler(_delivery: &DeliveredMessage) -> HandleMessageResult {
    // In order to read the message you need to convert the _delivery.data from a u8 vec to a utf8 string :
    // std::str::from_utf8(&_delivery.data))
    return HandleMessageResult::Ack;
}

Publisher example

fn main() {
    let config: JSONConfiguration = configuration::reader::read("./config.json").unwrap();
    println!("[{}] - Configuration read", line!(),);

    LocalPool::new().run_until(async {
        loop {
            let outcome = publish(
                "test".to_string(),
                &config.binding.exchange,
                &config.binding.routing_key,
                config.connection.clone(),
            )
            .await;

            println!("[{}] - {:?}", line!(), outcome);

            let delay = time::Duration::from_millis(500);
            thread::sleep(delay);
        }
    });
}

JSONConfiguration configuration example

The entire configuration supports default implementations. So if the default assumptions are right for you don't need to provide the entire config, only the parts you are interested in.

This is a full example

{
    "connection": {
        "host": "127.0.0.1",
        "port": 5672,
        "vhost": "/",
        "heartbeat": 10,
        "connection_timeout": 1000,
        "username": "secure",
        "password": "secure",
        "retry": 4
    },
    "binding": {
        "queue": "myQueue",
        "exchange": "myExchange",
        "routing_key": "myKey",
        "exchange_declaration_options": {
            "passive": false,
            "durable": true,
            "auto_delete": false
        }
    },
    "declare": {
        "queue": true,
        "exchange": true,
        "binding": true
    }
}

More examples

For more examples go to the repository at examples

Examples include

  • simple consumer
  • consumer using a postgres connection pool
  • consumer with options
  • simple publisher
  • publisher with message type

Idea

The whole idea is basically to be able to create a consumer project with minimal effort, by bypassing templating, configuration and complicated resiliency logic.

But most of the modules are public in this abstraction, so as to left some breathing space for custom composing.

Thoughts

Given that I use rabbitMq daily in nearly every application, this mini library is something that I might benefit in the near future. Luckily someone could find similar benefit as well.

Commit count: 60

cargo fmt