11use crate :: flow_queue:: connection:: { get_flow_channel, FlowChannel , FlowQueue } ;
2+ use crate :: flow_queue:: delegate:: { Delegate , QueueDelegate } ;
23use crate :: flow_queue:: name:: { QueueName , QueuePrefix , QueueProtocol } ;
34use lapin:: message:: { Delivery , DeliveryResult } ;
45use lapin:: options:: { BasicConsumeOptions , BasicPublishOptions , QueueDeclareOptions } ;
@@ -36,6 +37,7 @@ pub async fn declare_queues(flow_channel: FlowChannel, names: Vec<QueueName>) {
3637 }
3738}
3839
40+ /// Sends a message into a queue
3941pub async fn send_message (
4042 flow_channel : FlowChannel ,
4143 queue_name : QueueName ,
@@ -60,19 +62,59 @@ pub async fn send_message(
6062 }
6163}
6264
63- pub async fn consume_message ( channel : FlowChannel , queue_protocol : QueueProtocol ) {
64- let name = QueuePrefix :: Send + queue_protocol;
65+ /// Consumes a message
66+ ///
67+ /// Creates a delegate that waits on messages and consumes them.
68+ ///
69+ /// # Params
70+ /// - channel: FlowChannel of the send message
71+ /// - queue_name: Name of the Queue that should be listened to
72+ /// - delegate: Consumer delegate of the message
73+ ///
74+ /// # Example
75+ /// ```
76+ /// use lapin::message::Delivery;
77+ /// use code0_flow::flow_queue::delegate::Delegate;
78+ /// use code0_flow::flow_queue::connection::get_flow_channel;
79+ /// use code0_flow::flow_queue::name::{QueueName, QueuePrefix, QueueProtocol};
80+ /// use code0_flow::flow_queue::handler::consume_message;
81+ ///
82+ /// struct HttpDelegate;
83+ ///
84+ /// impl Delegate for HttpDelegate {
85+ /// fn handle_delivery(&self, delivery: Delivery) {
86+ /// todo!("Handle delivery!")
87+ /// }
88+ /// }
89+ ///
90+ /// async fn main() {
91+ /// let uri = "abc";
92+ /// let channel = get_flow_channel(uri).await;
93+ /// let queue_name = QueueName {
94+ /// prefix: QueuePrefix::Send,
95+ /// protocol: QueueProtocol::Rest,
96+ /// };
97+ ///
98+ /// consume_message(channel, queue_name, HttpDelegate).await;
99+ /// }
100+ /// ```
101+ pub async fn consume_message < T : Delegate > (
102+ channel : FlowChannel ,
103+ queue_name : QueueName ,
104+ delegate : T ,
105+ ) {
106+ let name = queue_name. prefix + queue_name. protocol ;
65107 let channel_arc = channel. lock ( ) . await ;
66108
67109 let mut consumer = channel_arc
68110 . basic_consume (
69111 & * name,
70- "my_consumer " ,
112+ "" ,
71113 BasicConsumeOptions :: default ( ) ,
72114 FieldTable :: default ( ) ,
73115 )
74116 . await
75117 . unwrap ( ) ;
76118
77- consumer. set_delegate ( SendQueueDelegate ) ;
78- }
119+ consumer. set_delegate ( QueueDelegate { delegate } ) ;
120+ }
0 commit comments