//
|
// ViewController.m
|
// RabbitMQ
|
//
|
// Created by WindShan on 2016/12/9.
|
// Copyright © 2016年 WindShan. All rights reserved.
|
//
|
|
#import "ViewController.h"
|
#import "RMQClient.h"
|
|
@interface ViewController ()
|
|
@end
|
|
@implementation ViewController
|
|
- (void)viewDidLoad
|
{
|
[super viewDidLoad];
|
|
[self workerNamed:@"Jack"];
|
[self workerNamed:@"Jill"];
|
sleep(1);
|
[self newTask:@"Hello World..."];
|
[self newTask:@"Just one this time."];
|
[self newTask:@"Five....."];
|
[self newTask:@"None"];
|
[self newTask:@"Two..dots"];
|
// Do any additional setup after loading the view, typically from a nib.
|
}
|
|
|
- (void)didReceiveMemoryWarning {
|
[super didReceiveMemoryWarning];
|
// Dispose of any resources that can be recreated.
|
}
|
|
- (void)newTask:(NSString *)msg
|
{
|
NSLog(@"Attempting to connect to local RabbitMQ broker");
|
|
RMQConnection *conn = [[RMQConnection alloc] initWithUri:@"amqp://myrabbitserver.com:1234"
|
delegate:[RMQConnectionDelegateLogger new]];
|
//RMQConnection *conn = [[RMQConnection alloc] initWithDelegate:[RMQConnectionDelegateLogger new]];
|
[conn start];
|
|
id<RMQChannel> ch = [conn createChannel];
|
|
RMQQueue *q = [ch queue:@"task_queue" options:RMQQueueDeclareDurable];
|
|
NSData *msgData = [msg dataUsingEncoding:NSUTF8StringEncoding];
|
[ch.defaultExchange publish:msgData routingKey:q.name persistent:YES];
|
NSLog(@"Sent %@", msg);
|
|
[conn close];
|
}
|
|
- (void)workerNamed:(NSString *)name
|
{
|
RMQConnection *conn = [[RMQConnection alloc] initWithUri:@"amqp://myrabbitserver.com:1234"
|
delegate:[RMQConnectionDelegateLogger new]];
|
|
//RMQConnection *conn = [[RMQConnection alloc] initWithDelegate:[RMQConnectionDelegateLogger new]];
|
[conn start];
|
|
id<RMQChannel> ch = [conn createChannel];
|
|
RMQQueue *q = [ch queue:@"task_queue" options:RMQQueueDeclareDurable];
|
|
[ch basicQos:@1 global:NO];
|
NSLog(@"%@: Waiting for messages", name);
|
|
RMQBasicConsumeOptions manualAck = RMQBasicConsumeNoOptions;
|
[q subscribe:manualAck handler:^(RMQMessage * _Nonnull message)
|
{
|
NSString *messageText = [[NSString alloc] initWithData:message.body encoding:NSUTF8StringEncoding];
|
NSLog(@"%@: Received %@", name, messageText);
|
// imitate some work
|
unsigned int sleepTime = (unsigned int)[messageText componentsSeparatedByString:@"."].count - 1;
|
NSLog(@"%@: Sleeping for %u seconds", name, sleepTime);
|
sleep(sleepTime);
|
|
[ch ack:message.deliveryTag];
|
}];
|
}
|
|
@end
|