8000 Tutorial 5 controller for Objective-C · rtoth89/rabbitmq-tutorials@45de28d · GitHub
[go: up one dir, main page]

Skip to content

Commit 45de28d

Browse files
committed
Tutorial 5 controller for Objective-C
[#118375823]
1 parent 632c6d2 commit 45de28d

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

objective-c/tutorial5/tutorial5/ViewController.m

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,45 @@ @implementation ViewController
99

1010
- (void)viewDidLoad {
1111
[super viewDidLoad];
12+
13+
[self receiveLogsTopic:@[@"kern.*", @"*.critical"]];
14+
sleep(1);
15+
[self emitLogTopic:@"Hello World!" routingKey:@"kern.info"];
16+
[self emitLogTopic:@"A critical kernel error" routingKey:@"kern.critical"];
17+
[self emitLogTopic:@"Critical module error" routingKey:@"somemod.critical"];
18+
[self emitLogTopic:@"Just some module info. You won't get this." routingKey:@"somemod.info"];
19+
}
20+
21+
- (void)receiveLogsTopic:(NSArray *)routingKeys {
22+
RMQConnection *conn = [[RMQConnection alloc] initWithDelegate:[RMQConnectionDelegateLogger new]];
23+
[conn start];
24+
25+
id<RMQChannel> ch = [conn createChannel];
26+
RMQExchange *x = [ch topic:@"topic_logs"];
27+
RMQQueue *q = [ch queue:@"" options:RMQQueueDeclareExclusive];
28+
29+
for (NSString *routingKey in routingKeys) {
30+
[q bind:x routingKey:routingKey];
31+
}
32+
33+
NSLog(@"Waiting for logs.");
34+
35+
[q subscribe:^(RMQDeliveryInfo * _Nonnull deliveryInfo, RMQMessage * _Nonnull message) {
36+
NSLog(@"%@:%@", deliveryInfo.routingKey, message.content);
37+
}];
38+
}
39+
40+
- (void)emitLogTopic:(NSString *)msg routingKey:(NSString *)routingKey {
41+
RMQConnection *conn = [[RMQConnection alloc] initWithDelegate:[RMQConnectionDelegateLogger new]];
42+
[conn start];
43+
44+
id<RMQChannel> ch = [conn createChannel];
45+
RMQExchange *x = [ch topic:@"topic_logs"];
46+
47+
[x publish:msg routingKey:routingKey];
48+
NSLog(@"Sent '%@'", msg);
49+
50+
[conn close];
1251
}
1352

1453
@end

0 commit comments

Comments
 (0)
0