Reputation: 61
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 ?
Upvotes: 5
Views: 13523
Reputation: 2019
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();
Upvotes: 2
Reputation: 15256
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.
Upvotes: 0
Reputation: 3469
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
Upvotes: 5
Reputation: 313
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.
Upvotes: 1