Sending a new message

QueueActor has to send a new message, because we will use this actor to send tasks to a worker. Add the corresponding struct:

pub struct SendMessage<T>(pub T);

Implement the Message trait for this type:

impl<T> Message for SendMessage<T> {
type Result = TaskId;
}

We need to set the Result type to TaskId, because we will generate a new task ID for a new message to process the response with a handler later. If you are not familiar with the Actix framework and message, return to Chapter 11, Involving Concurrency with Actors and Actix Crate.

The Handler of this message type will generate a new UUID and convert it into a String. Then, the method will use the send_message method to send a message to an outgoing queue:

impl<T: QueueHandler> Handler<SendMessage<T::Outgoing>> for QueueActor<T> {
type Result = TaskId;

fn handle(&mut self, msg: SendMessage<T::Outgoing>, ctx: &mut Self::Context) -> Self::Result {
let corr_id = Uuid::new_v4().to_simple().to_string();
self.send_message(corr_id.clone(), msg.0, ctx);
corr_id
}
}

Now, we have to implement the process_message and send_message methods of QueueActor.

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

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