Dependencies

To create an actor, we need the following dependencies:

use super::{ensure_queue, spawn_client};
use actix::fut::wrap_future;
use actix::{Actor, Addr, AsyncContext, Context, Handler, Message, StreamHandler, SystemRunner};
use failure::{format_err, Error};
use futures::Future;
use lapin::channel::{BasicConsumeOptions, BasicProperties, BasicPublishOptions, Channel};
use lapin::error::Error as LapinError;
use lapin::message::Delivery;
use lapin::types::{FieldTable, ShortString};
use log::{debug, warn};
use serde::{Deserialize, Serialize};
use tokio::net::TcpStream;
use uuid::Uuid;

pub type TaskId = ShortString;

First, we use the ensure_queue function from the super module, which creates a new queue, but we will implement it later in this chapter. spawn_client lets us create a new Client connected to a message broker. We will use the wrap_future function, which converts any Future object into an ActorFuture, which can be spawned in the Context environment of the Actix framework.

Let's explore the types from the lapin crate. The Channel struct represents a connection channel with the RabbitMQ instance. BasicConsumeOptions represents options used for the basic_consume method of the Channel call to subscribe to new events in a queue. BasicProperties types are used as parameters for the basic_publish method call of the Channel type for add properties such as correlation IDs to distinct recipients of the message, or set the required quality level for delivery. BasicPublishOptions is used for the basic_publish call to set extra options for a message publishing activity.

We also need the Error type from the lapin crate, but we renamed it LapinError because we also use the generic  Error from the failure crate. The Delivery struct represents an incoming message delivered from a queue. The FieldTable type is used as a parameter for the basic_consume method call of the Channel type. The ShortString type is a simple alias to a String that is used as a name of a queue in the lapin crate. The Uuid type is imported from the uuid crate to generate unique correlation IDs for messages to identify the origin of a message.

Now, we can declare the abstract handler for our messages.