1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use crate::{AmqpProperties, Channel, Result};
use amq_protocol::protocol::basic::{Deliver, GetOk};
/// A message delivered to a consumer.
#[derive(Clone, Debug)]
pub struct Delivery {
channel_id: u16,
delivery_tag: u64,
/// If true, this message has previously been delivered to this or another consumer.
pub redelivered: bool,
/// The name of the exchange this message was originally published to. May be an empty string
/// (the default exhange).
pub exchange: String,
/// The routing key specified when this message was published.
pub routing_key: String,
/// The content body containing the message.
pub body: Vec<u8>,
/// Properties associated with the message.
pub properties: AmqpProperties,
}
impl Delivery {
pub(crate) fn new(
channel_id: u16,
deliver: Deliver,
body: Vec<u8>,
properties: AmqpProperties,
) -> (String, Delivery) {
(
deliver.consumer_tag,
Delivery {
channel_id,
delivery_tag: deliver.delivery_tag,
redelivered: deliver.redelivered,
exchange: deliver.exchange,
routing_key: deliver.routing_key,
body,
properties,
},
)
}
pub(crate) fn new_get_ok(
channel_id: u16,
get_ok: GetOk,
body: Vec<u8>,
properties: AmqpProperties,
) -> Delivery {
Delivery {
channel_id,
delivery_tag: get_ok.delivery_tag,
redelivered: get_ok.redelivered,
exchange: get_ok.exchange,
routing_key: get_ok.routing_key,
body,
properties,
}
}
/// The server-assigned delivery tag for this message. Delivery tags are channel-specific.
#[inline]
pub fn delivery_tag(&self) -> u64 {
self.delivery_tag
}
/// Acknowledge this delivery, which must have been received on the given channel. If
/// `multiple` is true, acks this delivery and all other deliveries received on this channel
/// with smaller [`delivery_tag`](#method.delivery_tag)s.
///
/// # Panics
///
/// This method will attempt to panic if `channel` does not match the channel this delivery was
/// received on. It does this by comparing channel IDs, so it is possible that an incorrect
/// `Delivery`/`Channel` pairing will not be detected at runtime. Always ack deliveries with
/// the channel they were received on; the result of failing to do this is unspecified by the
/// AMQP specification.
#[inline]
pub fn ack(self, channel: &Channel) -> Result<()> {
assert_eq!(
self.channel_id,
channel.channel_id(),
"cannot ack delivery on different channel"
);
channel.basic_ack(self, false)
}
/// Acknowledge this delivery, which must have been received on the given channel, and all
/// other deliveries received on this channel with smaller
/// [`delivery_tag`](#method.delivery_tag)s.
///
/// # Panics
///
/// This method will attempt to panic if `channel` does not match the channel this delivery was
/// received on. It does this by comparing channel IDs, so it is possible that an incorrect
/// `Delivery`/`Channel` pairing will not be detected at runtime. Always ack deliveries with
/// the channel they were received on; the result of failing to do this is unspecified by the
/// AMQP specification.
#[inline]
pub fn ack_multiple(self, channel: &Channel) -> Result<()> {
assert_eq!(
self.channel_id,
channel.channel_id(),
"cannot ack delivery on different channel"
);
channel.basic_ack(self, true)
}
/// Reject this delivery, which must have been received on the given channel. If `requeue` is
/// true, instructs the server to attempt to requeue the message.
///
/// # Panics
///
/// This method will attempt to panic if `channel` does not match the channel this delivery was
/// received on. It does this by comparing channel IDs, so it is possible that an incorrect
/// `Delivery`/`Channel` pairing will not be detected at runtime. Always ack deliveries with
/// the channel they were received on; the result of failing to do this is unspecified by the
/// AMQP specification.
#[inline]
pub fn nack(self, channel: &Channel, requeue: bool) -> Result<()> {
assert_eq!(
self.channel_id,
channel.channel_id(),
"cannot nack delivery on different channel"
);
channel.basic_nack(self, false, requeue)
}
/// Reject this delivery, which must have been received on the given channel, and all other
/// unacknowledged deliveries to this channel with smaller
/// [`delivery_tag`](#method.delivery_tag)s. If `requeue` is true, instructs the server to
/// attempt to requeue the message.
///
/// # Panics
///
/// This method will attempt to panic if `channel` does not match the channel this delivery was
/// received on. It does this by comparing channel IDs, so it is possible that an incorrect
/// `Delivery`/`Channel` pairing will not be detected at runtime. Always ack deliveries with
/// the channel they were received on; the result of failing to do this is unspecified by the
/// AMQP specification.
#[inline]
pub fn nack_multiple(self, channel: &Channel, requeue: bool) -> Result<()> {
assert_eq!(
self.channel_id,
channel.channel_id(),
"cannot nack delivery on different channel"
);
channel.basic_nack(self, true, requeue)
}
/// Reject this delivery, which must have been received on the given channel. If `requeue` is
/// true, instructs the server to attempt to requeue the message.
///
/// # Panics
///
/// This method will attempt to panic if `channel` does not match the channel this delivery was
/// received on. It does this by comparing channel IDs, so it is possible that an incorrect
/// `Delivery`/`Channel` pairing will not be detected at runtime. Always ack deliveries with
/// the channel they were received on; the result of failing to do this is unspecified by the
/// AMQP specification.
#[inline]
pub fn reject(self, channel: &Channel, requeue: bool) -> Result<()> {
assert_eq!(
self.channel_id,
channel.channel_id(),
"cannot reject delivery on different channel"
);
channel.basic_reject(self, requeue)
}
}