[rabbitmq-discuss] Rabbitmq message?
Jerry Kuch
jerryk at vmware.com
Sun Nov 6 00:02:24 GMT 2011
Hi, Nguyen...
> Hi Jerry, thanks millions for you reply.I will give you more details
> so may be you can help me to solve my problem. What I actually do that
> I want to send a java object to the queue. Two channels will be
> registered and the java object will be delivered to them. In this
> case,the message type is not a array of bytes.Take a look at my
> example code, may be you will be more clear:
The AMQP protocol, and thus the RabbitMQ Java client, view the message
payload as a structure-less blob of bytes. You can see this in the
Javadoc for the basicPublish's method signature:
void basicPublish(java.lang.String exchange,
java.lang.String routingKey,
AMQP.BasicProperties props,
byte[] body) <-- UNSTRUCTURED BLOB OF BYTES!
throws java.io.IOException
If we return to your pseudocode example...
> For the sender:
>
> Book message = new Book("A","Hoan");
> channel.basicPublish("", QUEUE_NAME, null,message);
We see that the parameter in which you want to pass your Book object
is declared to be of type byte[]. Thus, what you write will never
compile. You'd have to choose some way to convert your Book object into
an array of bytes and, correspondingly, to convert the transmitted array of
bytes at the receiving end back into a Book Java object.
Out of curiosity, have you worked with Java Serialization before?
Java Serialization may help you with this, but be warned: although it
is very powerful, it has many subtleties and will impose some serious
inflexibilities on the system you're building. Both sides of the
conversation are going to need to need suitably versioned, compatible
definitions of the Book class on their CLASSPATHs. Also, things can
get complex when the object you're serializing isn't simple, since you
need to send over the entire object graph implied by what the sender
has. If you look at chapter 11 of Josh Bloch's "Effective Java,"
second edition, you will get a sense for just how hairy Serialization,
which starts off seeming simple, can get in practice.
Another problem you'll have is that if the other side of your
communications channel isn't a Java program (or as above just isn't a
Java program which a perfectly suitable version of your Book class)
you won't be able to communicate at all, and you'll discover this with
ugly exceptions being thrown on the receiving end. There are also
potential security pitfalls to be wary of. This is counter to the
spirit of AMQP, which aims to provide easy interoperability between
programs potentially written in different languages, running on
different operating systems, etc.
If you're only concerned about some set of basic, structured data that
describes a book (e.g. each book has a title, author, and publication
date, or something like that), rather than the full semantics of a
Java object with all of its *callable methods* and *code*, as well as
the raw data it's carrying, you might consider serializing into XML,
or JSON, or the like, or using one of the other less exotic then Java
Serialization frameworks that have come into being, like Google's
ProtocolBuffers, or Facebook's Thrift.
Does that make sense?
Best regards,
Jerry
More information about the rabbitmq-discuss
mailing list