Objective-C RabbitMQ 客户端不向队列发布消息 [英] Objective-C RabbitMQ client not publishing messages to queue

查看:23
本文介绍了Objective-C RabbitMQ 客户端不向队列发布消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 RabbitMQ for iOS 制作消息传递应用程序.我将这个包装类用于目标 c,以及 RabbitMQ-C 客户端库.

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

交换、队列和队列绑定一切正常,但我的代码没有将消息发布到 RabbitMQ 服务器.请帮帮我,有什么问题吗?

这是我的代码:

 NSError *error= nil;AMQPConnection *connection = [[AMQPConnection alloc] init];[连接connectToHost:@"SERVER_NAME" onPort:PORT 错误:&error];如果(错误!= nil){NSLog(@"错误连接:%@", error);返回;}[连接 loginAsUser:@"USER_NAME" withPasswort:@"PASSWORD" onVHost:@"/" 错误:&error];如果(错误!= nil){NSLog(@"登录错误:%@", error);返回;}AMQPChannel *channel = [连接openChannel];AMQPExchange *exchange = [[AMQPExchange alloc] initFanoutExchangeWithName:@"EXCHANGE_NAME" onChannel:channel isPassive:NO isDurable:NO getsAutoDeleted:NO er​​ror:&error];如果(错误!= nil){NSLog(@"Error declareExchange: %@", error);返回;}//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]];如果(错误!= nil){NSLog(@"错误声明队列:%@", error);返回;}NSError *错误;[队列 bindToExchange:exchange withKey:@"KEY" 错误:&error];amqp_basic_properties_t 道具;props._flags= AMQP_BASIC_CLASS;props.type = amqp_cstring_bytes([@"typeOfMessage" UTF8String]);props.priority = 1;[交换publishMessage:@"Test message" usingRoutingKey:@"ROUTING_KEY" propertiesMessage:props 强制:NO 立即:NO 错误:&error];如果(错误!= nil){NSLog(@"Error declareExchange: %@", error);返回;}

解决方案

我搜索了 allot 因为我也在实现这种类型的应用程序,在很多地方我发现使用 iPhone 应用程序实现时有多种类型错误的库.我知道你已经解决了你的问题,但这个答案是给那些仍在受苦的人的.我结合了 rabbitmq-c 和 obejective-c wrapper 的库,删除了问题并将其存储在我自己的地方.给定链接,您可以使用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"#定义端口5672#define 用户@"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];[连接connectToHost:主机onPort:端口错误:&error];如果(错误!= nil){NSLog(@"错误连接:%@", error);返回;}[连接 loginAsUser:user withPasswort:pass onVHost:@"/" error:&error];如果(错误!= nil){NSLog(@"登录错误:%@", error);返回;}AMQPChannel *channel = [连接 openChannelError:&error2];AMQPExchange *exchange = [[AMQPExchange alloc] initDirectExchangeWithName:@"AMQP" onChannel:channel isPassive:NO isDurable:NO getsAutoDeleted:NO er​​ror:&error];如果(错误!= nil){NSLog(@"Error declareExchange: %@", error);返回;}AMQPQueue *queue = [[AMQPQueue alloc] initWithName:routingQueue onChannel:channel error:&error3];如果(错误!= nil){NSLog(@"错误声明队列:%@", error);返回;}BOOL 成功 = [队列 bindToExchange:exchange withKey:routingQueue 错误:&error4];如果(成功){amqp_basic_properties_t 道具;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;//这里放你要发布的消息...[交换publishMessage:@"YOUR MESSAGE" usingRoutingKey:routingQueue propertiesMessage:props required:NOimmediate:NO er​​ror:&error];如果(错误!= nil){NSLog(@"Error declareExchange: %@", error);返回;}}}

接收消息需要委托.

-(IBAction)receiveMessage:(id)sender{NSError *error= nil;NSError *error2 = nil;NSError *error3 = nil;NSError *error4 = nil;AMQPConnection *connection = [[AMQPConnection alloc] init];[连接connectToHost:主机onPort:端口错误:&error];[连接 loginAsUser:user withPasswort:pass onVHost:@"/" error:&error];AMQPChannel *channel = [连接 openChannelError:&error2];AMQPQueue *queue = [[AMQPQueue alloc] initWithName:routingQueue onChannel:channel isPassive:NO isExclusive:NO isDurable:NO getsAutoDeleted:NO er​​ror:&error3];AMQPConsumer *consumer = [[AMQPConsumer alloc] initForQueue:queue onChannel:&channel useAcknowledgements:YES isExclusive:NO receiveLocalMessages:NO er​​ror:&error4 deepLo​​op:1];AMQPConsumerThread *consumerThread = [[AMQPConsumerThread alloc] initWithConsumer:consumer delegate:self nameThread:@"myThread"persistenListen:NO];consumerThread.delegate=self;[消费者线程启动];}-(void)amqpConsumerThreadReceivedNewMessage:(AMQPMessage *)theMessage{NSLog(@"message = %@", theMessage.body);}

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

解决方案

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

For receiving message, you need delegate.

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

这篇关于Objective-C RabbitMQ 客户端不向队列发布消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆