我正在使用RabbitMQ-C客户端库的目标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; }解决方法 我搜索了分配,因为我也实现了这种类型的应用程序,在许多地方我发现库在使用iPhone应用程序时有许多类型的错误.我知道你已经解决了你的问题,但这个答案适用于那些仍在受苦的人.我将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);}总结
以上是内存溢出为你收集整理的ios – Objective-C RabbitMQ客户端不将消息发布到队列全部内容,希望文章能够帮你解决ios – Objective-C RabbitMQ客户端不将消息发布到队列所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)