Objective-C RabbitMQ client not publishing messages to queue

匿名 (未验证) 提交于 2019-12-03 01:54:01

问题:

I am trying to make a messaging application using, RabbitMQ for iOS. and i am using this wrapper classes for objective c, with RabbitMQ-C client libraries.

https://github.com/profmaad/librabbitmq-objc

Exchange, Queue & Queue Binding all are ok but my code is not publishing message to RabbitMQ server. Please help me , what is the problem?

this is my code:

    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;     }

回答1:

I searched allot as I am too implementing this type of app, at many places I found library that have many types of errors while implementing with iPhone application. I know you have solved your problem but this answer is for those who are still suffering. I combined libs of rabbitmq-c and obejective-c wrapper , erase issues and store it at my own place. You can have rabbitmq-lib for Objective-C development given link.

https://dl.dropboxusercontent.com/u/75870052/AMQPLib.zip

Include this library, import files as given below:-

#import "AMQPExchange.h" #import "AMQPConsumer.h" #import "AMQPConnection.h" #import "AMQPConsumerThread.h" #import "AMQPChannel.h" #import "AMQPQueue.h" #import "AMQPMessage.h"

Define your credentials, I have used default.

#define host @"localhost" #define routingQueue @"CreateQueue" #define port 5672 #define user @"guest" #define pass @"guest"

For publishing message on server use following method.

- (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         
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!