0

New to RabbitMQ and message passing APIs. I want to send a triplet of float values every few milliseconds. To initialize my connection/channel, I have:

connection = factory.newConnection();
channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);

Then when I want to send a message, the following does strings:

private void sendMessage(String message) throws IOException {
    channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
}

How can I change sendMessage to send float x1, float x2, float x3 ?

And on the server-side, how would I receive/parse this message into 3 floats?

JDS
  • 16,388
  • 47
  • 161
  • 224

1 Answers1

2

You can, for instance, use a ByteBuffer:

final ByteBuffer buf = ByteBuffer.allocate(12)  // 3 floats
    .putFloat(f1).putFloat(f2).putFloat(f3);    // put them; .put*() return this
channel.basicPublish(buf.array());              // send

This will write the floats in big endian (default network order and what Java uses as well).

On the receiving side, you would do:

// delivery is a QueuingConsumer.Delivery
final ByteBuffer buf = ByteBuffer.wrap(delivery.getBody());
final float f1 = buf.getFloat();
final float f2 = buf.getFloat();
final float f3 = buf.getFloat();
fge
  • 119,121
  • 33
  • 254
  • 329
  • Hey that's awesome. What's QueuingConsumer.Delivery? – JDS Jul 11 '13 at 18:26
  • The receiving side of a RabbitMQ client -- I have gone and read the tutorial :p (ie, [here](http://www.rabbitmq.com/tutorials/tutorial-one-java.html)) – fge Jul 11 '13 at 18:27
  • Cool. One more issue - in my code for channel.basicPublish I get the error: `The method basicPublish(String, String, AMQP.BasicProperties, byte[]) in the type Channel is not applicable for the arguments (byte[])` EDIT - nvm, I seem to have fixed (compiled at least) with `channel.basicPublish("", QUEUE_NAME, null, buf.array()); ` – JDS Jul 11 '13 at 18:28