【问题标题】:Objective-C RabbitMQ client not publishing messages to queueObjective-C RabbitMQ 客户端未将消息发布到队列
【发布时间】:2014-07-17 04:58:42
【问题描述】:

我正在尝试使用 RabbitMQ for iOS 制作消息传递应用程序。 我正在将此包装类用于目标 c,并带有 RabbitMQ-C 客户端库。

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

Exchange、队列和队列绑定一切正常,但我的代码没有将消息发布到 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;
    }

【问题讨论】:

  • 您是否按预期声明了交换?您是否按预期声明了队列?绑定队列是否按预期进行交换?您是否将消息发布到适当的交易所?您的消息可以路由到预期的队列吗?您是否有一些消费者窃取了您的信息?应用程序是否抛出任何异常? RabbitMQ 日志中是否有任何可疑输出? - 这些问题通常会节省我的时间和神经元来寻找某些魔法发生的原因。
  • 感谢 zaq178miami,我正在使用 EXCHANGE NAME = "fanout" ,BINDING KEY = "hello",ROUTING KEY = "hello" 和 QUEUE = "11111" 我也尝试过这个项目:github.com/leisurehuang/RabbitMQ-IOS-lib同样的问题。它在 RabbitMQ 服务器上制作所有内容,但不向队列发布任何消息。而且我没有运行消费者线程,xcode 也不例外。我很困惑这是服务器问题还是我遗漏了什么?
  • 这些问题是为了帮助您调试原因。无论如何,尝试使用路由键等于队列名称发布到默认交换(具有空名称),这是将消息放入您想要的确切队列的快速而肮脏的方式。
  • zaq178miami ,我现在使用的方式与您建议的方式相同。并在 Xcode 中遇到一些异常。请建议我现在应该做什么?我是 RabbitMQ 的新手。异常:AMQPException:无法将队列绑定到交换:ACCESS_REFUSED - 在默认交换上不允许操作 AMQPException:无法发布消息:ACCESS_REFUSED - 在默认交换上不允许操作错误 declareExchange:错误域 = AMQPExchange 代码 = -10“失败发布消息:" UserInfo=0x8f41250 {NSLocalizedDescription=无法发布消息:}
  • 我已经解决了。实际上amqp_basic_properties_t props; 有问题然后我尝试使用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; 它现在可以工作了。 @zaq178miami 和@old_sound 谢谢你的帮助。

标签: ios objective-c rabbitmq amqp


【解决方案1】:

我搜索了分配,因为我也在实现这种类型的应用程序,在很多地方我发现库在使用 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"
#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);
}

【讨论】:

  • 这个端口号是特定的吗??
  • 如何在 Swift 中添加这个库??我尝试使用 Bridging-Header.h 文件添加它。它给出了很多错误。 ex '/Users/Adonta/moojic_workspace/moojicapp2/moojicapp2/amqp.h:243:49: 错误:未知类型名称 'size_t' extern void *amqp_pool_alloc(amqp_pool_t *pool, size_t amount);'
  • 我也对上述问题感到震惊。我有所有库文件,我仍然收到,,, 文件未找到@jeevs
  • @McDonal_11 不,我找不到解决方案。我切换到另一个库。
  • stackoverflow.com/questions/38259528/…@jeevs 。请指导我。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-21
  • 2019-03-01
  • 2019-12-02
  • 2011-04-27
  • 2012-01-21
  • 1970-01-01
  • 2015-12-19
相关资源
最近更新 更多