How to create messages in java client-server model

I have installed a basic client and a basic server using java sockets. it can send lines in between successfully.

Now I want to create some basic posts. could you please give me any guidelines on how to post them? Should I use java serialzation to dispatch classes? or should I just encode the information I need in a custom string and decode on the other side?

how to recognize the type of messages? is there any convention for this? how the first 4 characters of each message are the message id?

thank!

+2


source to share


3 answers


I would recommend that you don't reinvent the wheel. If Java serialization is fine for you, just use it.

Also consider that there are good serialization schemes around:

thrift , from facebook, and protocol buffers from Google.



Thrift is also an RPC engine, so you can use it instead of opening / reading raw sockets, but that of course depends on your problem domain.

Edit: and answering the question about message formatting. Definitely if you want to implement your own protocol, and if you have multiple message types, you must create a yes header. But I warn you that the protocol implementation is tough and very error-prone. Just create an object containing the different internal objects + methods you need if you want to add it to the version field and make it implement the java.io.Serializable interface.

+4


source


Maybe JMS can help you, it's hard to tell without knowing the details. But JMS is standard, thoughtful, and generic, and there are an impressive number of implementations available, open source and advertised. We are using Sun OpenMQ and we are very happy with it. It is fast enough for our needs, very mature and reliable. Keep in mind that JMS is not easy by any standard, so it may well be overkill for your needs.



+3


source


If you are going to deploy this to a production environment, I would suggest you take a look at RMI or XML Web Services . (Google's Protocol Buffers are interesting too, but they don't include a standard protocol for transporting messages, although third-party implementations do exist.)

If you do it for the fun of learning, there are many ways to do it. In general, a message in a general messaging system will have some sort of "envelope format" that contains not only the body of the message, but also the metadata about the message. The bare minimum for a header is what the intended recipient identifies - either an integer identifier, a string representing a method name, or a file, or something like that.

A simple example is HTTP , a plain text format in which an envelope is made up of all lines until the first line is empty. The first line identifies the protocol version and the intended destination (≈ the requested file), the next lines are the metadata about the request, and the message body follows the first blank line.

In general, XML is a common format for distributed services (mainly because of its good schema capabilities and cross-platform support), although some schemas use other formats for simplicity and / or performance. For example, RMI uses standard Java object serialization.

What you decide to use is ultimately based on your needs. If you want to make it easier to interact with your system from a large number of platforms, use XML (or REST ) web services . Use RMI to communicate between distributed Java subsystems. If your system is extremely transaction-intensive, the proprietary binary format is perhaps best suited for faster processing and smaller messages, but before doing this "optimization", remember that it takes a lot more work to run and most applications don't. there will be a lot of benefit from it.

+1


source







All Articles