22 releases (breaking)

✓ Uses Rust 2018 edition

0.18.0 Mar 3, 2019
0.16.0 Feb 1, 2019
0.15.0 Dec 5, 2018
0.14.1 Nov 16, 2018
0.6.0 Mar 30, 2017

#7 in Database interfaces

Download history 147/week @ 2018-12-20 79/week @ 2018-12-27 324/week @ 2019-01-03 199/week @ 2019-01-10 284/week @ 2019-01-17 298/week @ 2019-01-24 283/week @ 2019-01-31 338/week @ 2019-02-07 305/week @ 2019-02-14 439/week @ 2019-02-21 446/week @ 2019-02-28 522/week @ 2019-03-07 339/week @ 2019-03-14 612/week @ 2019-03-21 858/week @ 2019-03-28

1,711 downloads per month
Used in 11 crates (5 directly)

MIT license

198KB
4K 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::Future;
use lapin_futures as lapin;
use crate::lapin::channel::{BasicPublishOptions, BasicProperties, QueueDeclareOptions};
use crate::lapin::client::ConnectionOptions;
use crate::lapin::types::FieldTable;
use log::info;
use tokio;
use tokio::net::TcpStream;
use tokio::runtime::Runtime;

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

  let addr = "127.0.0.1:5672".parse().unwrap();

  Runtime::new().unwrap().block_on_all(
    TcpStream::connect(&addr).map_err(Error::from).and_then(|stream| {

      // connect() returns a future of an AMQP Client
      // that resolves once the handshake is done
      lapin::client::Client::connect(stream, ConnectionOptions::default()).map_err(Error::from)
   }).and_then(|(client, _ /* heartbeat */)| {

      // create_channel returns a future that is resolved
      // once the channel is successfully created
      client.create_channel().map_err(Error::from)
    }).and_then(|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::new()).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::client::ConnectionOptions;
use crate::lapin::channel::{BasicConsumeOptions, QueueDeclareOptions};
use crate::lapin::types::FieldTable;
use log::{debug, info};
use tokio;
use tokio::net::TcpStream;
use tokio::runtime::Runtime;

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

  let addr = "127.0.0.1:5672".parse().unwrap();

  Runtime::new().unwrap().block_on_all(
    TcpStream::connect(&addr).map_err(Error::from).and_then(|stream| {

      // connect() returns a future of an AMQP Client
      // that resolves once the handshake is done
      lapin::client::Client::connect(stream, ConnectionOptions::default()).map_err(Error::from)
   }).and_then(|(client, heartbeat)| {
     // The heartbeat future should be run in a dedicated thread so that nothing can prevent it from
     // dispatching events on time.
     // If we ran it as part of the "main" chain of futures, we might end up not sending
     // some heartbeats if we don't poll often enough (because of some blocking task or such).
     tokio::spawn(heartbeat.map_err(|_| ()));

      // create_channel returns a future that is resolved
      // once the channel is successfully created
      client.create_channel().map_err(Error::from)
    }).and_then(|channel| {
      let id = channel.id;
      info!("created channel with id: {}", id);

      let ch = channel.clone();
      channel.queue_declare("hello", QueueDeclareOptions::default(), FieldTable::new()).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::new())
      }).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

~9.5MB
~174K SLoC