Objective-C RabbitMQ客户端不会将消息发布到队列中
我正在尝试使用iOS的RabbitMQ制作消息应用程序。 我用RabbitMQ-C客户端库来实现这个包装类。
https://github.com/profmaad/librabbitmq-objc
交换,队列和队列绑定一切正常,但我的代码不发布消息到RabbitMQ服务器。 请帮帮我,是什么问题?
这是我的代码:
NSError *error= nil; AMQPConnection *connection = [[AMQPConnection alloc] init]; [connection connectToHost:@"SERVER_NAME" onPort:PORT error:&error]; if (error != nil){ NSLog(@"Error connection: %@", error); return; } [connection loginAsUser:@"USER_NAME" withPasswort:@"PASSWORD" onVHost:@"/" error:&error]; if (error != nil){ NSLog(@"Error logined: %@", error); return; } AMQPChannel *channel = [connection openChannel]; AMQPExchange *exchange = [[AMQPExchange alloc] initFanoutExchangeWithName:@"EXCHANGE_NAME" onChannel:channel isPassive:NO isDurable:NO getsAutoDeleted:NO error:&error]; if (error != nil){ NSLog(@"Error declareExchange: %@", error); return; } //AMQPQueue *queue = [[AMQPQueue alloc] initWithName:@"NAME" onChannel:channel isPassive:NO isExclusive:NO isDurable:YES getsAutoDeleted:YES error:&error]; AMQPQueue *queue = [[AMQPQueue alloc] initWithName:@"NAME" onChannel:[connection openChannel]]; if (error != nil){ NSLog(@"Error declare Queue: %@", error); return; } NSError *error ; [queue bindToExchange:exchange withKey:@"KEY" error:&error]; amqp_basic_properties_t props; props._flags= AMQP_BASIC_CLASS; props.type = amqp_cstring_bytes([@"typeOfMessage" UTF8String]); props.priority = 1; [exchange publishMessage:@"Test message" usingRoutingKey:@"ROUTING_KEY" propertiesMessage:props mandatory:NO immediate:NO error:&error]; if (error != nil){ NSLog(@"Error declareExchange: %@", error); return; }
我search分配,因为我太执行这种types的应用程序,在许多地方,我发现有许多types的错误,同时用iPhone应用程序执行的库。 我知道你已经解决了你的问题,但是这个答案是针对那些还在受苦的人。 我把libs的rabbitmq-c和obejective-c包装器结合起来,擦除问题并存储在我自己的地方。 你可以为rabbitmq-lib给Objective-C开发指定链接。
https://dl.dropboxusercontent.com/u/75870052/AMQPLib.zip
包括这个库,导入文件如下:
#import "AMQPExchange.h" #import "AMQPConsumer.h" #import "AMQPConnection.h" #import "AMQPConsumerThread.h" #import "AMQPChannel.h" #import "AMQPQueue.h" #import "AMQPMessage.h"
定义您的凭据,我已经使用默认。
#define host @"localhost" #define routingQueue @"CreateQueue" #define port 5672 #define user @"guest" #define pass @"guest"
用于在服务器上发布消息使用以下方法。
- (IBAction)send:(id)sender { NSError *error= nil; NSError *error2 = nil; NSError *error3 = nil; NSError *error4 = nil; AMQPConnection *connection = [[AMQPConnection alloc] init]; [connection connectToHost:host onPort:port error:&error]; if (error != nil){ NSLog(@"Error connection: %@", error); return; } [connection loginAsUser:user withPasswort:pass onVHost:@"/" error:&error]; if (error != nil){ NSLog(@"Error logined: %@", error); return; } AMQPChannel *channel = [connection openChannelError:&error2]; AMQPExchange *exchange = [[AMQPExchange alloc] initDirectExchangeWithName:@"AMQP" onChannel:channel isPassive:NO isDurable:NO getsAutoDeleted:NO error:&error]; if (error != nil){ NSLog(@"Error declareExchange: %@", error); return; } AMQPQueue *queue = [[AMQPQueue alloc] initWithName:routingQueue onChannel:channel error:&error3]; if (error != nil){ NSLog(@"Error declare Queue: %@", error); return; } BOOL success = [queue bindToExchange:exchange withKey:routingQueue error:&error4]; if (success) { amqp_basic_properties_t props; props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG | AMQP_BASIC_DELIVERY_MODE_FLAG; props.content_type = amqp_cstring_bytes("text/plain"); props.delivery_mode = 2; props.priority = 1; //Here put your message to publish... [exchange publishMessage:@"YOUR MESSAGE" usingRoutingKey:routingQueue propertiesMessage:props mandatory:NO immediate:NO error:&error]; if (error != nil){ NSLog(@"Error declareExchange: %@", error); return; } } }
为了接收消息,你需要委托。
-(IBAction)receiveMessage:(id)sender { NSError *error= nil; NSError *error2 = nil; NSError *error3 = nil; NSError *error4 = nil; AMQPConnection *connection = [[AMQPConnection alloc] init]; [connection connectToHost:host onPort:port error:&error]; [connection loginAsUser:user withPasswort:pass onVHost:@"/" error:&error]; AMQPChannel *channel = [connection openChannelError:&error2]; AMQPQueue *queue = [[AMQPQueue alloc] initWithName:routingQueue onChannel:channel isPassive:NO isExclusive:NO isDurable:NO getsAutoDeleted:NO error:&error3]; AMQPConsumer *consumer = [[AMQPConsumer alloc] initForQueue:queue onChannel:&channel useAcknowledgements:YES isExclusive:NO receiveLocalMessages:NO error:&error4 deepLoop:1]; AMQPConsumerThread *consumerThread = [[AMQPConsumerThread alloc] initWithConsumer:consumer delegate:self nameThread:@"myThread" persistentListen:NO]; consumerThread.delegate=self; [consumerThread start]; } -(void)amqpConsumerThreadReceivedNewMessage:(AMQPMessage *)theMessage { NSLog(@"message = %@", theMessage.body); }