Objective-C Rabbitmq Client Not Publishing Messages to Queue

Objective-C RabbitMQ client not publishing messages to queue

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 Client use headers while publishing messages

I've just answered a question about this over on the mailing list, but we will be adding instructions to the documentation soon. Apologies - this area isn't well documented.

https://groups.google.com/d/msg/rabbitmq-users/Bw0Jz6zOgxM/w7BO8NORBAAJ

How should I use the library as librabbitmq-objc?

For using in iOS

Delete:

#import <Cocoa/Cocoa.h>

Use:

#import <Foundation/Foundation.h>

And if file not found using this:

# import <amqp.h> 

Use this:

# import "amqp.h"

This will almost solve your problem. You may get some error on uint64 fix it as xcode suggests you. And it will work fine.

I am also using Objective-C wrapper for librabbitmq-c library. I am able to connect, create exchange, create queue, bind queue but i am not able to publish any message using this code:

amqp_basic_properties_t props;
props._flags= AMQP_BASIC_CLASS;
props.type = amqp_cstring_bytes([@"typeOfMessage" UTF8String]);
props.priority = 1;
[exchange publishMessage:@"Hello Friends" usingRoutingKey:@"hello" propertiesMessage:props mandatory:NO immediate:NO error:&error];

No error, No exception, i don't know why this is not working !!! I am still looking for the exact solution.

See this, it may help you
Objective-C RabbitMQ client not publishing messages to queue

Rabbitmq message is not appearing in the queue from c#

It appears as though you are declaring an 'exclusive' queue. Exclusive queues are exclusive to the connection and as such cannot be operated upon (including being listed) from another connection.

Try

Model.QueueDeclare(queName, true, **false**, false, null)

If you want to be able to list it or do anything else with if from another connection.

Not able to build rabbitmq-c for iOS application

I finally found it, here it is for further reference

1/ get the latest rabbitmq-c and rabbitmq-objc libraries.

2/ make sure Cmake is updated (2.6 or better)

3/ install iOS-cmake

4/ in rabbitmq-c directory (change OSX_ARCHITECTURES and IOS_PLATFORM flags for simulator or device)

mkdir build.ios && cd build.ios
cmake -DCMAKE_TOOLCHAIN_FILE=/path/to/iOS.cmake
-DIOS_PLATFORM=SIMULATOR (resp. OS)
-DCMAKE_IOS_DEVELOPER_ROOT=/Applications/Xcode.app/Contents/Developer
-DCMAKE_IOS_SDK_ROOT=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.2.sdk
-DCMAKE_OSX_ARCHITECTURES=x86_64 (resp. i386)
-DBUILD_STATIC_LIBS=True
-DENABLE_SSL_SUPPORT=False
-GXcode ..

5/ in build.os/librabbitmq, rename directory Debug to Debug-iphonesimulator. Open in XCode the librabbitmq-c.xcodeproj, select target rabbitmq-static, build, and close this project.

6/ import in project that will be using the library librabbitmq.a. In build settings, make sure that Always Search User Paths is set to Yes and that User Header Search Path contains both paths to rabbitmq-c/librabbitmq and to rabbitmq-c/build.ios/rabbitmq-c.

7/ copy the rabbitmq-objc classes in the project, and set -fno-objc-arc flag for those classes.

8/ Replace occurences of uint8 by u_int8_t, replace occurences of #import < Cocoa/Cocoa.h> by #import < Foundation/Foundation.h>, replace occurences of #import < amqp.h> and #import < amqp_framing.h> by #import "amqp.h" and #import "amqp_framing.h".

9/ In AMQPConsumer.m, add missing parameter AMQP_EMPTY_TABLE in function amqp_basic_consume, and in AMQPExchange.m, add missing

#define AMQP_EXCHANGE_TYPE_DIRECT @"direct" 
#define AMQP_EXCHANGE_TYPE_FANOUT @"fanout"
#define AMQP_EXCHANGE_TYPE_TOPIC @"topic"

10/ Build project.



Related Topics



Leave a reply



Submit