Abstract messages handler

Create the QueueHandler struct in the queue_actor.rs file:

pub trait QueueHandler: 'static {
type Incoming: for<'de> Deserialize<'de>;
type Outgoing: Serialize;

fn incoming(&self) -> &str;
fn outgoing(&self) -> &str;
fn handle(
&self,
id: &TaskId,
incoming: Self::Incoming,
) -> Result<Option<Self::Outgoing>, Error>;
}

QueueHandler is a trait that has two associated types and three methods. It requires a static lifetime for types that will implement the QueueHandler trait, because instances of this trait will be used as fields of actors that have a static lifetime as well.

This trait has the Incoming associated type, which represents the incoming message type and requires the type to implement the Deserialize trait to be deserializable, because RabbitMQ transfers byte arrays only and you have to decide which format to use for serialization. The Outgoing associated type has to implement the Serialize trait to be serializable as a bytes array to be sent as an outgoing message.

The QueueHandler  trait also has incoming and outgoing methods. The first returns the name of a queue to consume incoming messages. The second method returns the name of a queue in which an actor will write sending messages. There is also a handle method, which takes a reference to TaskId and incoming messages of the Self::Incoming associated type. The method returns a Result with an optional Self::Outgoing instance. If the implementation returns None then no messages will be sent to the outgoing channel. However, you can use a special SendMessage type to send a message later. We will declare this type later, after we add the actor's struct.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.106.135