大佬教程收集整理的这篇文章主要介绍了ios – Objective-C RabbitMQ客户端不将消息发布到队列,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
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; }
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客户端不将消息发布到队列所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。