4

I have a code like this in RabbitMQ :

byte[] rawBytes = serialize(trxEntities);    
byte[] zipped = rawBytes;  
if (shouldBeCompress) {  
zipped = compressor.compress(rawBytes);  
}  
BasicProperties persistentBasic = MessageProperties.PERSISTENT_BASIC;  
channel.basicPublish("", queueName, persistentBasic, zipped);  

As you see some of my messages should be compress along witch some others shouldn't.
Is there any way I could set any properties to tell the consumer that "hey! this is a zipped message" ?

PS. does "com.rabbitmq.client.AMQP.BasicProperties.BasicProperties(..., Map headers, ...)" help me? I mean could I set any parameter in BasicProperties.header ?

Arash Zareh
  • 51
  • 1
  • 1
  • 3

4 Answers4

5

I think you may add anything you like to the header. However, there is a field called "contentEncoding", which I think is better for this situation. You may just put "gzip", "deflate", or the compression algorithm in this field, take a look at this page for those encoding defined for HTTP: http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.5

Raymond Tau
  • 3,429
  • 26
  • 28
2

Yes you can!

We use the type field for specifying what message was sent (eg. USER_INFO, HEARTBEAT, etc.) and the contentEncoding field for specifying compression (if any):

AMQP.BasicProperties.Builder propsBuilder = new AMQP.BasicProperties.Builder();
propsBuilder.type(typeName);
if (compress)
{
  propsBuilder.contentEncoding("zip");
}
BasicProperties props = propsBuilder.build();
channel.basicPublish(targetExchange, "", true, props, data);

// and receiving works like this:
Delivery delivery = consumer.nextDelivery();
byte[] data = delivery.getBody();
BasicProperties props = delivery.getProperties();
String typeName = props.getType();
String replyToServerId = props.getReplyTo();
String contentEncoding = props.getContentEncoding();
Frederic Leitenberger
  • 1,949
  • 24
  • 32
1

you could use a wrapper for it as:

    public class wrapper(){
    public boolean isZipped;
    public String serializedMessage;
}

and then serialize this message with Java Serializable Object to Byte Array

or you can use this code:

persistentBasic = persistentBasic.builder().headers(filter).build();

and put your appropriate filter in header.

Community
  • 1
  • 1
mohsen Lzd
  • 313
  • 1
  • 4
  • 13
0

For the project that I'm working which uses RabbitMQ we use a header field to identify the content-type and content-encoding

For example, a plain-text message in a queue would read

{'type' : 'plaintext', 'encoding' : 'utf-8'}

Compressed data streams are processed through Base64 and then sent

{'type' : 'gzip', 'encoding' : 'base64'}

There is not a standardized mechanism with RabbitMQ to identify the content-type and encoding, you may adopt your own or select a commonly used standard.

lukecampbell
  • 14,728
  • 4
  • 34
  • 32
  • Shouldn't type & encoding be similar to how it's used more broadly? i.e. http://stackoverflow.com/a/23600787/98069 – wbyoung Mar 18 '17 at 17:05