Objective-C - Using RMQBasicHeaders while sending message

142 views
Skip to first unread message

G.Y.

unread,
Sep 20, 2016, 3:55:36 AM9/20/16
to rabbitmq-users
I have completed sending simple message as told in tutorials of RabbitMQ Client app. Now i want to send message without using a specific exchange, still using only queue. However, i need to send some information by NSDictionary or something like it (as header). I received message from server, the informations are in "properties" property of message. In other words:

2016-09-09 10:23:03.815 application[563:410885] message properties: (
   
"<RMQBasicContentType: 0x12569e410\n stringValue = @\"text/plain\">",
   
"<RMQBasicHeaders: 0x12566e000\n dictionaryValue =
 @{@\"senderUid\": <RMQLongstr: 0x125685960\n stringValue = @\"anything\">,\n                    
 @\"receiverUid\": <RMQLongstr: 0x1256aa150\n stringValue = @\"userId\">,\n                    
 @\"receiverType\": <RMQSignedLong: 0x1256648f0\n integerValue = 0>,\n                    
 @\"senderCode\": <RMQLongstr: 0x1256df400\n stringValue = @\"anycode\">,\n                    
 @\"messageType\": <RMQSignedLong: 0x1256df7d0\n integerValue = 106>}>"
,
   
"<RMQBasicDeliveryMode: 0x125662a30\n        octet = 2\n integerValue = 2>",
   
"<RMQBasicPriority: 0x125691be0\n        octet = 0\n integerValue = 0>"
)

This is the construct of message. I need to send this kind of message, the text or file in message.body, some informations in message.header. The problem is, since there are quite less documentation of Objective-C, i could not find how to send/publish the header. 

I implemented a subclass of RMQValue, as RMQMessageHeaders, set the informations as property. Then i created an array with this class and tried:

RMQMessageHeaders *messageProp = [[RMQMessageHeaders alloc] initWithDictionary:headers];

NSArray *properties = [NSArray arrayWithObjects:messageProp, nil];

RMQQueue *q = [ch queue:@"simpleQueue" options:RMQQueueDeclareDurable];

[ch.defaultExchange publish:[@"hello world!" dataUsingEncoding:NSUTF8StringEncoding] routingKey:q.name properties:properties options:RMQBasicPublishNoOptions];


This code gets no warning or error. In the runtime, i get an error of:


Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<RMQMessageHeaders 0x12f6b7080> valueForUndefinedKey:]: this class is not key value coding-compliant for the key flagBit.'

XCode shows that there is a sorting in RMQValues.m file, uses "flagBit" which is a value in RMQBasicvalues class. Then there it's getting complicated for me. I also tried adding one more property just like "properties" value of RMQMessage class and done something about it but could not handle problem. 


...


To make it more clear, how should i send these headers to a queue? If someone can help me, i'll appreciate very much. 

Thank you. 

Michael Klishin

unread,
Sep 20, 2016, 5:32:42 AM9/20/16
to rabbitm...@googlegroups.com
This can be a client library limitation this week. Its current maintainer will be back online in a week.

--
You received this message because you are subscribed to the Google Groups "rabbitmq-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rabbitmq-users+unsubscribe@googlegroups.com.
To post to this group, send email to rabbitmq-users@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
MK

Staff Software Engineer, Pivotal/RabbitMQ

G.Y.

unread,
Oct 6, 2016, 3:10:39 AM10/6/16
to rabbitmq-users
After a while, is there anyone to answer my question?

Thanks in advance. 

Andrew Bruce

unread,
Oct 6, 2016, 3:56:50 AM10/6/16
to rabbitmq-users
Hi G.Y.!

Sorry you're having trouble. This area is a bit clunky at present, and lacks documentation.

You can instantiate a header directly, and include it in the properties array. The header classes available are in https://github.com/rabbitmq/rabbitmq-objc-client/blob/master/RMQClient/RMQBasicProperties.h

For example:

```

NSArray *properties = @[[[RMQBasicAppId alloc] init:@"my-app-id"], [[RMQBasicContentType alloc] init:@"text/plain"]];

[ch.defaultExchange publish:[@"Hello World!" dataUsingEncoding:NSUTF8StringEncoding] routingKey:q.name properties:properties options:RMQBasicPublishNoOptions];

```

You should not need to subclass anything in the Objective-C client except for RMQConsumer.

I've file a story in our backlog about improving the documentation around headers.

Thanks for your feedback!

Andrew

On Thu, Oct 6, 2016 at 8:10 AM G.Y. <ylmz...@gmail.com> wrote:
After a while, is there anyone to answer my question?

Thanks in advance. 

--
You received this message because you are subscribed to the Google Groups "rabbitmq-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rabbitmq-user...@googlegroups.com.
To post to this group, send email to rabbitm...@googlegroups.com.

Andrew Bruce

unread,
Oct 6, 2016, 4:02:50 AM10/6/16
to rabbitmq-users
Further to this, the example I just gave is for *properties*, not headers. Headers are themselves a property:

```
RMQBasicHeaders *header = [[RMQBasicHeaders alloc] init:@{@"X-Foo-Bar": [[RMQLongstr alloc] init:@"baz"],
                                                          @"Some-Bool": [[RMQBoolean alloc] init:YES]}];
```
`header` could then be included in the properties array. Note that the values of the dictionary passed to the RMQBasicHeaders constructor must be RMQValues: https://github.com/rabbitmq/rabbitmq-objc-client/blob/master/RMQClient/RMQValues.h

G.Y.

unread,
Oct 7, 2016, 2:07:22 AM10/7/16
to rabbitmq-users
Thank you!

G.Y.

unread,
Oct 7, 2016, 3:33:49 AM10/7/16
to rabbitmq-users
But i get the error. It says:

-[RMQBasicHeaders allKeys]: unrecognized selector sent to instance 0x15e244220

2016-10-07 10:22:50.002 MDMAgent[3799:2936619] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[RMQBasicHeaders allKeys]: unrecognized selector sent to instance 0x15e244220'

*** First throw call stack:


I get also warning of init function for NSArray. It wants not RMQBasicHeaders but NSDictionary. I can send it a dictionary but i think this causes a problem. What do you suggest? 

G.Y.

unread,
Oct 7, 2016, 4:33:38 AM10/7/16
to rabbitmq-users
Yes i changed the code, worked fine. Thank you again!

For anyone needing help for this, what i did is:

NSDictionary *header = @{ @"numberedThing1" : [[RMQSignedLong alloc] init:int],
                           
@"numberedThing2" : [[RMQSignedLong alloc] init:int],
                           
@"stringThing1" : [[RMQLongstr alloc] init:@"anyString"],
                           
@"stringThing2" : [[RMQLongstr alloc] init:@"anyString"]};
NSArray *properties = @[[[RMQBasicHeaders alloc] init:header]];

RMQQueue *q = [ch queue:q.name options:RMQQueueDeclareDurable];

[ch.defaultExchange publish:[(NSString *)messageContent dataUsingEncoding:NSUTF8StringEncoding] routingKey:self.queueName properties:properties options:RMQBasicPublishNoOptions];


Thanks again.

Andrew Bruce

unread,
Oct 7, 2016, 4:37:36 AM10/7/16
to rabbitm...@googlegroups.com
Can I see the code that's producing this error, please?

--

Andrew Bruce

unread,
Oct 7, 2016, 4:38:42 AM10/7/16
to rabbitm...@googlegroups.com
Ah, glad you got it sorted. Thanks for posting your code.

bhargav dudi

unread,
Jul 18, 2017, 2:50:35 AM7/18/17
to rabbitmq-users, abr...@pivotal.io
Hi ,

I need to add headers. But I am unable to add headers.

I am using above code also. But i can't.

Can you please help me on this.

Michael Klishin

unread,
Jul 18, 2017, 6:51:14 AM7/18/17
to rabbitm...@googlegroups.com
Consider starting new threads for new questions.

There's a test that sets a lot of message properties, including headers:

To unsubscribe from this group and stop receiving emails from it, send an email to rabbitmq-users+unsubscribe@googlegroups.com.
To post to this group, send email to rabbitmq-users@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages