Rust and RabbitMQ

A very simple example

Start Docker Container for RabbitMQ

version: "3"
services:
 rabbitmq:
    image: "rabbitmq:3-management"
    ports:
      - "5672:5672"
      - "15672:15672"
    volumes:
      - 'rabbitmq_data:/data'

volumes:
  rabbitmq_data:

Cargo.toml dependency

Add the amqp dependency to the cargo.toml file


[dependencies.amqp]
version="0.1.3"

Sample Code

Import the crate to your application. In this instance, main.rs

extern crate amqp;
use amqp::Session;

Simple Publish Function


fn publish_message() -> () {
    use crate::amqp::Basic;
    let mut session = Session::open_url("amqp://localhost//").unwrap();
    let mut channel = session.open_channel(1).unwrap();
    let queue_declare = channel.queue_declare("my_queue_name", false, true, false, false, false, 
        amqp::Table::new());
    channel.basic_publish("", "my_queue_name", 
        true, false, 
        amqp::protocol::basic::BasicProperties {
            content_type: Some("text".to_string()), ..Default::default()
        },
        (b"Hello World").to_vec());
}

Invoke this function in main function

fn main() {
    publish_message();

Go to RabbitMQ Monitoring site

http://localhost:15672

Login into the system

username: guest password: guest

RabbitMQ Overview page

RabbitMQ Queue RabbitMQ Delivery Block