34 releases (20 breaking)

✓ Uses Rust 2018 edition

new 0.21.1 Jun 16, 2019
0.20.0 Jun 14, 2019
0.18.0 Mar 3, 2019
0.15.0 Dec 5, 2018
0.6.0 Mar 30, 2017

#7 in Database interfaces

Download history 427/week @ 2019-03-02 533/week @ 2019-03-09 352/week @ 2019-03-16 743/week @ 2019-03-23 809/week @ 2019-03-30 671/week @ 2019-04-06 791/week @ 2019-04-13 768/week @ 2019-04-20 693/week @ 2019-04-27 647/week @ 2019-05-04 432/week @ 2019-05-11 594/week @ 2019-05-18 823/week @ 2019-05-25 770/week @ 2019-06-01 827/week @ 2019-06-08

2,953 downloads per month
Used in 11 crates (5 directly)

MIT license

119KB
3K SLoC

lapin-futures

This library offers a futures based API over the lapin-async library. It leverages the tokio-io and futures library, so you can use it with tokio, futures-cpupool or any other reactor.

The library is designed so it does not own the socket, so you can use any TCP, TLS or unix socket based stream.

Calls to the underlying stream are guarded by a mutex, so you could use one connection from multiple threads.

There's an example available using tokio.

Publishing a message

use env_logger;
use failure::Error;
use futures::future;
use futures::future::Future;
use lapin_futures as lapin;
use crate::lapin::{BasicProperties, Client, ConnectionProperties};
use crate::lapin::options::{BasicPublishOptions, QueueDeclareOptions};
use crate::lapin::types::FieldTable;
use log::info;
use tokio;
use tokio::runtime::Runtime;

fn main() {
  env_logger::init();

  let addr = std::env::var("AMQP_ADDR").unwrap_or_else(|_| "amqp://127.0.0.1:5672/%2f".into());

  Runtime::new().unwrap().block_on_all(
   Client::connect(&addr, ConnectionProperties::default()).map_err(Error::from).and_then(|client| {
      // create_channel returns a future that is resolved
      // once the channel is successfully created
      client.create_channel().map_err(Error::from)
    }).and_then(|mut channel| {
      let id = channel.id();
      info!("created channel with id: {}", id);

      // we using a "move" closure to reuse the channel
      // once the queue is declared. We could also clone
      // the channel
      channel.queue_declare("hello", QueueDeclareOptions::default(), FieldTable::default()).and_then(move |_| {
        info!("channel {} declared queue {}", id, "hello");

        channel.basic_publish("", "hello", b"hello from tokio".to_vec(), BasicPublishOptions::default(), BasicProperties::default())
      }).map_err(Error::from)
    })
  ).expect("runtime failure");
}

Creating a consumer

use env_logger;
use failure::Error;
use futures::{future, Future, Stream};
use lapin_futures as lapin;
use crate::lapin::{BasicProperties, Client, ConnectionProperties};
use crate::lapin::options::{BasicConsumeOptions, QueueDeclareOptions};
use crate::lapin::types::FieldTable;
use log::{debug, info};
use tokio;
use tokio::runtime::Runtime;

fn main() {
  env_logger::init();

  let addr = std::env::var("AMQP_ADDR").unwrap_or_else(|_| "amqp://127.0.0.1:5672/%2f".into());

  Runtime::new().unwrap().block_on_all(
   Client::connect(&addr, ConnectionProperties::default()).map_err(Error::from).and_then(|client| {
      // create_channel returns a future that is resolved
      // once the channel is successfully created
      client.create_channel().map_err(Error::from)
    }).and_then(|mut channel| {
      let id = channel.id();
      info!("created channel with id: {}", id);

      let mut ch = channel.clone();
      channel.queue_declare("hello", QueueDeclareOptions::default(), FieldTable::default()).and_then(move |queue| {
        info!("channel {} declared queue {}", id, "hello");

        // basic_consume returns a future of a message
        // stream. Any time a message arrives for this consumer,
        // the for_each method would be called
        channel.basic_consume(&queue, "my_consumer", BasicConsumeOptions::default(), FieldTable::default())
      }).and_then(|stream| {
        info!("got consumer stream");

        stream.for_each(move |message| {
          debug!("got message: {:?}", message);
          info!("decoded message: {:?}", std::str::from_utf8(&message.data).unwrap());
          ch.basic_ack(message.delivery_tag, false)
        })
      }).map_err(Error::from)
    })
  ).expect("runtime failure");
}

Dependencies

~6.5MB
~127K SLoC