单军华
2016-12-09 1a11fb042ac01b8c045d48e6ee3abbf153bf1c36
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
// This source code is dual-licensed under the Mozilla Public License ("MPL"),
// version 1.1 and the Apache License ("ASL"), version 2.0.
//
// The ASL v2.0:
//
// ---------------------------------------------------------------------------
// Copyright 2016 Pivotal Software, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ---------------------------------------------------------------------------
//
// The MPL v1.1:
//
// ---------------------------------------------------------------------------
// The contents of this file are subject to the Mozilla Public License
// Version 1.1 (the "License"); you may not use this file except in
// compliance with the License. You may obtain a copy of the License at
// https://www.mozilla.org/MPL/
//
// Software distributed under the License is distributed on an "AS IS"
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
// License for the specific language governing rights and limitations
// under the License.
//
// The Original Code is RabbitMQ
//
// The Initial Developer of the Original Code is Pivotal Software, Inc.
// All Rights Reserved.
//
// Alternatively, the contents of this file may be used under the terms
// of the Apache Standard license (the "ASL License"), in which case the
// provisions of the ASL License are applicable instead of those
// above. If you wish to allow use of your version of this file only
// under the terms of the ASL License and not to allow others to use
// your version of this file under the MPL, indicate your decision by
// deleting the provisions above and replace them with the notice and
// other provisions required by the ASL License. If you do not delete
// the provisions above, a recipient may use your version of this file
// under either the MPL or the ASL License.
// ---------------------------------------------------------------------------
 
#import "RMQBasicProperties+MergeDefaults.h"
#import "RMQErrors.h"
#import "RMQMethods+Convenience.h"
#import "RMQAllocatedChannel.h"
#import "RMQConnectionDelegate.h"
 
@interface RMQAllocatedChannel ()
@property (nonatomic, copy, readwrite) NSNumber *channelNumber;
@property (nonatomic, readwrite) NSNumber *contentBodySize;
@property (nonatomic, readwrite) id <RMQDispatcher> dispatcher;
@property (nonatomic, readwrite) NSMutableDictionary *consumers;
@property (nonatomic, readwrite) NSMutableDictionary *exchanges;
@property (nonatomic, readwrite) NSMutableDictionary *exchangeBindings;
@property (nonatomic, readwrite) NSMutableDictionary *queues;
@property (nonatomic, readwrite) NSMutableDictionary *queueBindings;
@property (nonatomic, readwrite) id<RMQConfirmations> confirmations;
@property (nonatomic, readwrite) NSNumber *prefetchCountPerConsumer;
@property (nonatomic, readwrite) NSNumber *prefetchCountPerChannel;
@property (nonatomic, readwrite) id<RMQConnectionDelegate> delegate;
@property (nonatomic, readwrite) id<RMQNameGenerator> nameGenerator;
@property (nonatomic, readwrite) id<RMQChannelAllocator> allocator;
@end
 
@implementation RMQAllocatedChannel
 
- (instancetype)init:(NSNumber *)channelNumber
     contentBodySize:(NSNumber *)contentBodySize
          dispatcher:(id<RMQDispatcher>)dispatcher
       nameGenerator:(id<RMQNameGenerator>)nameGenerator
           allocator:(nonnull id<RMQChannelAllocator>)allocator
       confirmations:(id<RMQConfirmations>)confirmations {
    self = [super init];
    if (self) {
        self.channelNumber = channelNumber;
        self.contentBodySize = contentBodySize;
        self.dispatcher = dispatcher;
        self.consumers = [NSMutableDictionary new];
        self.exchanges = [NSMutableDictionary new];
        self.exchangeBindings = [NSMutableDictionary new];
        self.queues = [NSMutableDictionary new];
        self.queueBindings = [NSMutableDictionary new];
        self.confirmations = confirmations;
        self.prefetchCountPerConsumer = nil;
        self.prefetchCountPerChannel = nil;
        self.delegate = nil;
        self.nameGenerator = nameGenerator;
        self.allocator = allocator;
    }
    return self;
}
 
- (instancetype)init
{
    [self doesNotRecognizeSelector:_cmd];
    return nil;
}
 
- (RMQExchange *)defaultExchange {
    return [[RMQExchange alloc] initWithName:@""
                                        type:@"direct"
                                     options:RMQExchangeDeclareNoOptions
                                     channel:self];
}
 
- (void)activateWithDelegate:(id<RMQConnectionDelegate>)delegate {
    [self.dispatcher activateWithChannel:self delegate:delegate];
    self.delegate = delegate;
}
 
- (void)open {
    [self.dispatcher sendSyncMethod:[RMQChannelOpen new]];
}
 
- (void)close {
    [self.dispatcher sendSyncMethod:[RMQChannelClose new]
                  completionHandler:^(RMQFrameset *frameset) {
                      [self.allocator releaseChannelNumber:self.channelNumber];
                  }];
}
 
- (void)blockingClose {
    [self.dispatcher sendSyncMethodBlocking:[RMQChannelClose new]];
    [self.allocator releaseChannelNumber:self.channelNumber];
}
 
- (void)prepareForRecovery {
    [self.dispatcher disable];
}
 
- (void)recover {
    [self open];
    [self recoverPrefetch];
    [self recoverConfirmations];
    [self recoverExchanges];
    [self recoverExchangeBindings]; 
    [self recoverQueuesAndTheirBindings];
    [self recoverConsumers];
}
 
- (void)blockingWaitOn:(Class)method {
    [self.dispatcher blockingWaitOn:method];
}
 
- (void)confirmSelect {
    [self.confirmations enable];
    [self.dispatcher sendSyncMethod:[RMQConfirmSelect new]];
}
 
- (void)afterConfirmed:(NSNumber *)timeout
               handler:(void (^)(NSSet<NSNumber *> * _Nonnull, NSSet<NSNumber *> * _Nonnull))handler {
    [self.confirmations addCallbackWithTimeout:timeout
                                      callback:handler];
}
 
- (void)afterConfirmed:(RMQConfirmationCallback)handler {
    [self afterConfirmed:@30
                 handler:handler];
}
 
- (RMQQueue *)queue:(NSString *)originalQueueName
            options:(RMQQueueDeclareOptions)options
          arguments:(nonnull NSDictionary<NSString *,RMQValue<RMQFieldValue> *> *)arguments {
    RMQQueue *found = self.queues[originalQueueName];
    if (found) {
        return found;
    } else {
        return [self memoizedQueueDeclare:originalQueueName options:options arguments:[[RMQTable alloc] init:arguments]];
    }
}
 
- (RMQQueue *)queue:(NSString *)originalQueueName
            options:(RMQQueueDeclareOptions)options {
    return [self queue:originalQueueName options:options arguments:@{}];
}
 
- (RMQQueue *)queue:(NSString *)queueName {
    return [self queue:queueName options:RMQQueueDeclareNoOptions];
}
 
- (void)queueDelete:(NSString *)queueName
            options:(RMQQueueDeleteOptions)options {
    [self.queues removeObjectForKey:queueName];
    [self.dispatcher sendSyncMethod:[[RMQQueueDelete alloc] initWithQueue:queueName
                                                                  options:options]];
}
 
- (void)queueBind:(NSString *)queueName
         exchange:(NSString *)exchangeName
       routingKey:(nonnull NSString *)routingKey {
    [self.dispatcher sendSyncMethod:[[RMQQueueBind alloc] initWithQueue:queueName
                                                               exchange:exchangeName
                                                             routingKey:routingKey]];
    [self.queueBindings[queueName] addObject:@{@"exchange": exchangeName,
                                               @"routing-key": routingKey}];
}
 
- (void)queueUnbind:(NSString *)queueName
           exchange:(NSString *)exchangeName
         routingKey:(NSString *)routingKey {
    [self.dispatcher sendSyncMethod:[[RMQQueueUnbind alloc] initWithQueue:queueName
                                                                 exchange:exchangeName
                                                               routingKey:routingKey]];
    [self.queueBindings[queueName] removeObject:@{@"exchange": exchangeName,
                                                  @"routing-key": routingKey}];
}
 
- (RMQConsumer *)basicConsume:(NSString *)queueName
                      options:(RMQBasicConsumeOptions)options
                      handler:(RMQConsumerDeliveryHandler)handler {
    RMQConsumer *consumer = [[RMQConsumer alloc] initWithChannel:self
                                                       queueName:queueName
                                                         options:options];
    [consumer onDelivery:handler];
    [self basicConsume:consumer];
    return consumer;
}
 
- (void)basicConsume:(RMQConsumer *)consumer {
    [self.dispatcher sendSyncMethod:[[RMQBasicConsume alloc] initWithQueue:consumer.queueName
                                                               consumerTag:consumer.tag
                                                                   options:consumer.options]
                  completionHandler:^(RMQFrameset *frameset) {
                      self.consumers[consumer.tag] = consumer;
                  }];
}
 
- (NSString *)generateConsumerTag {
    return [self.nameGenerator generateWithPrefix:@"rmq-objc-client.gen-"];
}
 
- (void)basicCancel:(NSString *)consumerTag {
    [self.dispatcher sendSyncMethod:[[RMQBasicCancel alloc] initWithConsumerTag:[[RMQShortstr alloc] init:consumerTag]
                                                                        options:RMQBasicCancelNoOptions]
                  completionHandler:^(RMQFrameset *frameset) {
                      [self.consumers removeObjectForKey:consumerTag];
                  }];
}
 
- (NSNumber *)basicPublish:(NSData *)body
                routingKey:(NSString *)routingKey
                  exchange:(NSString *)exchange
                properties:(NSArray<RMQValue *> *)properties
                   options:(RMQBasicPublishOptions)options {
    RMQBasicPublish *publish = [[RMQBasicPublish alloc] initWithReserved1:[[RMQShort alloc] init:0]
                                                                 exchange:[[RMQShortstr alloc] init:exchange]
                                                               routingKey:[[RMQShortstr alloc] init:routingKey]
                                                                  options:options];
    RMQContentBody *contentBody = [[RMQContentBody alloc] initWithData:body];
 
    NSData *bodyData = contentBody.amqEncoded;
 
    NSArray *mergedProperties = [RMQBasicProperties mergeProperties:properties
                                                       withDefaults:RMQBasicProperties.defaultProperties];
    RMQContentHeader *contentHeader = [[RMQContentHeader alloc] initWithClassID:publish.classID
                                                                       bodySize:@(bodyData.length)
                                                                     properties:mergedProperties];
 
    NSArray *contentBodies = [self contentBodiesFromData:bodyData
                                              inChunksOf:self.contentBodySize.integerValue];
    RMQFrameset *frameset = [[RMQFrameset alloc] initWithChannelNumber:self.channelNumber
                                                                method:publish
                                                         contentHeader:contentHeader
                                                         contentBodies:contentBodies];
    [self.dispatcher sendAsyncFrameset:frameset];
    return [self.confirmations addPublication];
}
 
-  (void)basicGet:(NSString *)queue
          options:(RMQBasicGetOptions)options
completionHandler:(RMQConsumerDeliveryHandler)userCompletionHandler {
    [self.dispatcher sendSyncMethod:[[RMQBasicGet alloc] initWithReserved1:[[RMQShort alloc] init:0]
                                                                     queue:[[RMQShortstr alloc] init:queue]
                                                                   options:options]
                  completionHandler:^(RMQFrameset *frameset) {
                      RMQBasicGetOk *getOk = (RMQBasicGetOk *)frameset.method;
                      RMQMessage *message = [[RMQMessage alloc] initWithBody:frameset.contentData
                                                                 consumerTag:@""
                                                                 deliveryTag:@(getOk.deliveryTag.integerValue)
                                                                 redelivered:getOk.options & RMQBasicGetOkRedelivered
                                                                exchangeName:getOk.exchange.stringValue
                                                                  routingKey:getOk.routingKey.stringValue
                                                                  properties:frameset.contentHeader.properties];
                      userCompletionHandler(message);
                  }];
}
 
- (void)basicQos:(NSNumber *)count
          global:(BOOL)isGlobal {
    [self.dispatcher sendSyncMethod:[[RMQBasicQos alloc] initWithPrefetchCount:count
                                                                        global:isGlobal]
                  completionHandler:^(RMQFrameset *frameset) {
                      if (isGlobal) {
                          self.prefetchCountPerChannel = count;
                      } else {
                          self.prefetchCountPerConsumer = count;
                      }
                  }];
}
 
- (void)ack:(NSNumber *)deliveryTag
    options:(RMQBasicAckOptions)options {
    [self.dispatcher sendAsyncMethod:[[RMQBasicAck alloc] initWithDeliveryTag:[[RMQLonglong alloc] init:deliveryTag.integerValue]
                                                                      options:options]];
}
 
- (void)ack:(NSNumber *)deliveryTag {
    [self ack:deliveryTag options:RMQBasicAckNoOptions];
}
 
- (void)reject:(NSNumber *)deliveryTag
       options:(RMQBasicRejectOptions)options {
    [self.dispatcher sendAsyncMethod:[[RMQBasicReject alloc] initWithDeliveryTag:[[RMQLonglong alloc] init:deliveryTag.integerValue]
                                                                         options:options]];
}
 
- (void)reject:(NSNumber *)deliveryTag {
    [self reject:deliveryTag options:RMQBasicRejectNoOptions];
}
 
- (void)nack:(NSNumber *)deliveryTag
     options:(RMQBasicNackOptions)options {
    [self.dispatcher sendAsyncMethod:[[RMQBasicNack alloc] initWithDeliveryTag:[[RMQLonglong alloc] init:deliveryTag.integerValue]
                                                                       options:options]];
}
 
- (void)nack:(NSNumber *)deliveryTag {
    [self nack:deliveryTag options:RMQBasicNackNoOptions];
}
 
- (void)exchangeDeclare:(NSString *)name
                   type:(NSString *)type
                options:(RMQExchangeDeclareOptions)options {
    [self.dispatcher sendSyncMethod:[[RMQExchangeDeclare alloc] initWithExchange:name
                                                                            type:type
                                                                         options:options]];
}
 
- (void)exchangeBind:(NSString *)sourceName
         destination:(NSString *)destinationName
          routingKey:(NSString *)routingKey {
    [self.dispatcher sendSyncMethod:[[RMQExchangeBind alloc] initWithDestination:destinationName
                                                                          source:sourceName
                                                                      routingKey:routingKey]];
    [self.exchangeBindings[sourceName] addObject:@{@"destination": destinationName,
                                                   @"routing-key": routingKey}];
}
 
- (void)exchangeUnbind:(NSString *)sourceName
           destination:(NSString *)destinationName
            routingKey:(NSString *)routingKey {
    [self.dispatcher sendSyncMethod:[[RMQExchangeUnbind alloc] initWithDestination:destinationName
                                                                            source:sourceName
                                                                        routingKey:routingKey]];
    [self.exchangeBindings[sourceName] removeObject:@{@"destination": destinationName,
                                                      @"routing-key": routingKey}];
}
 
- (RMQExchange *)fanout:(NSString *)name options:(RMQExchangeDeclareOptions)options {
    return [self memoizedExchangeDeclare:name type:@"fanout" options:options];
}
 
- (RMQExchange *)fanout:(NSString *)name {
    return [self fanout:name options:RMQExchangeDeclareNoOptions];
}
 
- (RMQExchange *)direct:(NSString *)name options:(RMQExchangeDeclareOptions)options {
    return [self memoizedExchangeDeclare:name type:@"direct" options:options];
}
 
- (RMQExchange *)direct:(NSString *)name {
    return [self direct:name options:RMQExchangeDeclareNoOptions];
}
 
- (RMQExchange *)topic:(NSString *)name options:(RMQExchangeDeclareOptions)options {
    return [self memoizedExchangeDeclare:name type:@"topic" options:options];
}
 
- (RMQExchange *)topic:(NSString *)name {
    return [self topic:name options:RMQExchangeDeclareNoOptions];
}
 
- (RMQExchange *)headers:(NSString *)name options:(RMQExchangeDeclareOptions)options {
    return [self memoizedExchangeDeclare:name type:@"headers" options:options];
}
 
- (RMQExchange *)headers:(NSString *)name {
    return [self headers:name options:RMQExchangeDeclareNoOptions];
}
 
- (void)exchangeDelete:(NSString *)name
               options:(RMQExchangeDeleteOptions)options {
    [self.exchanges removeObjectForKey:name];
    [self.dispatcher sendSyncMethod:[[RMQExchangeDelete alloc] initWithReserved1:[[RMQShort alloc] init:0]
                                                                        exchange:[[RMQShortstr alloc] init:name]
                                                                         options:options]];
}
 
# pragma mark - RMQFrameHandler
 
- (void)handleFrameset:(RMQFrameset *)frameset {
    if ([frameset.method isKindOfClass:[RMQBasicDeliver class]]) {
        [self.dispatcher enqueue:^{
            [self handleBasicDeliver:frameset];
        }];
    } else if ([frameset.method isKindOfClass:[RMQBasicCancel class]]) {
        [self.dispatcher enqueue:^{
            [self handleBasicCancel:frameset];
        }];
    } else if ([frameset.method isKindOfClass:[RMQBasicAck class]]) {
        [self.dispatcher enqueue:^{
            [self.confirmations ack:(RMQBasicAck *)frameset.method];
        }];
    } else if ([frameset.method isKindOfClass:[RMQBasicNack class]]) {
        [self.dispatcher enqueue:^{
            [self.confirmations nack:(RMQBasicNack *)frameset.method];
        }];
    } else {
        [self.dispatcher handleFrameset:frameset];
    }
}
 
# pragma mark - Private
 
- (void)handleBasicDeliver:(RMQFrameset *)frameset {
    RMQBasicDeliver *deliver = (RMQBasicDeliver *)frameset.method;
    RMQConsumer *consumer = self.consumers[deliver.consumerTag.stringValue];
    if (consumer) {
        [consumer consume: [[RMQMessage alloc] initWithBody:frameset.contentData
                                                consumerTag:deliver.consumerTag.stringValue
                                                deliveryTag:@(deliver.deliveryTag.integerValue)
                                                redelivered:deliver.options & RMQBasicDeliverRedelivered
                                               exchangeName:deliver.exchange.stringValue
                                                 routingKey:deliver.routingKey.stringValue
                                                 properties:frameset.contentHeader.properties]];
    }
}
 
- (void)handleBasicCancel:(RMQFrameset *)frameset {
    RMQBasicCancel *cancel = (RMQBasicCancel *)frameset.method;
    NSString *consumerTag = cancel.consumerTag.stringValue;
    RMQConsumer *consumer = self.consumers[consumerTag];
    [consumer handleCancellation];
    [self.consumers removeObjectForKey:consumerTag];
}
 
- (RMQExchange *)memoizedExchangeDeclare:(NSString *)name
                                    type:(NSString *)type
                                 options:(RMQExchangeDeclareOptions)options {
    RMQExchange *exchange;
    exchange = self.exchanges[name];
    if (!exchange) {
        [self exchangeDeclare:name type:type options:options];
        exchange = [[RMQExchange alloc] initWithName:name
                                                type:type
                                             options:options
                                             channel:self];
        self.exchanges[name] = exchange;
        self.exchangeBindings[name] = [NSMutableSet new];
    }
    return exchange;
}
 
- (RMQQueue *)memoizedQueueDeclare:(NSString *)originalQueueName
                           options:(RMQQueueDeclareOptions)options
                         arguments:(RMQTable *)arguments {
    NSString *declaredQueueName = [originalQueueName isEqualToString:@""]
    ? [self.nameGenerator generateWithPrefix:@"rmq-objc-client.gen-"]
    : originalQueueName;
 
    if (self.queues[declaredQueueName]) {
        NSError *error = [NSError errorWithDomain:RMQErrorDomain
                                             code:RMQErrorChannelQueueNameCollision
                                         userInfo:@{NSLocalizedDescriptionKey: @"Name collision when generating unique name."}];
        [self.delegate channel:self error:error];
        return nil;
    } else {
        RMQQueue *q = [[RMQQueue alloc] initWithName:declaredQueueName
                                             options:options
                                           arguments:arguments
                                             channel:(id<RMQChannel>)self];
 
        RMQQueueDeclare *method = [[RMQQueueDeclare alloc] initWithQueue:declaredQueueName
                                                                 options:options
                                                               arguments:arguments];
        [self.dispatcher sendSyncMethod:method];
 
        self.queues[q.name] = q;
        self.queueBindings[q.name] = [NSMutableSet new];
        return q;
    }
}
 
- (NSArray *)contentBodiesFromData:(NSData *)data inChunksOf:(NSUInteger)chunkSize {
    NSMutableArray *bodies = [NSMutableArray new];
    NSUInteger chunkCount = data.length / chunkSize;
    for (int i = 0; i < chunkCount; i++) {
        NSUInteger offset = i * chunkSize;
        NSData *subData = [data subdataWithRange:NSMakeRange(offset, chunkSize)];
        RMQContentBody *body = [[RMQContentBody alloc] initWithData:subData];
        [bodies addObject:body];
    }
    NSUInteger lastChunkSize = data.length % chunkSize;
    if (lastChunkSize > 0) {
        NSData *lastData = [data subdataWithRange:NSMakeRange(data.length - lastChunkSize, lastChunkSize)];
        [bodies addObject:[[RMQContentBody alloc] initWithData:lastData]];
    }
    return bodies;
}
 
- (void)recoverPrefetch {
    if (self.prefetchCountPerConsumer) {
        [self basicQos:self.prefetchCountPerConsumer global:NO];
    }
    if (self.prefetchCountPerChannel) {
        [self basicQos:self.prefetchCountPerChannel global:YES];
    }
}
 
- (void)recoverConfirmations {
    if (self.confirmations.isEnabled) {
        [self.confirmations recover];
        [self.dispatcher sendSyncMethod:[RMQConfirmSelect new]];
    }
}
 
- (void)recoverExchanges {
    for (RMQExchange *exchange in self.exchanges.allValues) {
        [self exchangeDeclare:exchange.name type:exchange.type options:exchange.options];
    }
}
 
- (void)recoverExchangeBindings {
    for (RMQExchange *exchange in self.exchanges.allValues) {
        for (NSDictionary *binding in [self.exchangeBindings[exchange.name] copy]) {
            [self exchangeBind:exchange.name destination:binding[@"destination"] routingKey:binding[@"routing-key"]];
        }
    }
}
 
- (void)recoverQueuesAndTheirBindings {
    for (RMQQueue *queue in self.queues.allValues) {
        [self.dispatcher sendSyncMethod:[[RMQQueueDeclare alloc] initWithQueue:queue.name
                                                                       options:queue.options
                                                                     arguments:queue.arguments]];
        for (NSDictionary *binding in [self.queueBindings[queue.name] copy]) {
            [self queueBind:queue.name exchange:binding[@"exchange"] routingKey:binding[@"routing-key"]];
        }
    }
}
 
- (void)recoverConsumers {
    for (RMQConsumer *consumer in self.consumers.allValues) {
        [self.dispatcher sendSyncMethod:[[RMQBasicConsume alloc] initWithQueue:consumer.queueName
                                                                   consumerTag:consumer.tag
                                                                       options:consumer.options]];
    }
}
 
@end