1.3 Distributed-distributed communication (serialization)

Source: Internet
Author: User

In a network, when you are transferring content with large amounts of data, you need to convert the object to a byte stream in the form of a stream in a serialized way. Here is a brief introduction to the serialization of several ways and basic knowledge points. Java serialization mechanism serialize interface problems with the serialization mechanism of Java itself

1. The results of serialized data are relatively large and the transmission efficiency is relatively low.

2. No cross-language docking

That's why, for a long time, the XML format encoding-based object serialization mechanism has become the mainstream, which solves multiple language compatibility problems on the one hand and is easier to understand than the binary serialization approach. The XML-based SOAP protocol and the corresponding WebService framework have become the necessary technologies for the various mainstream development languages for a long time.

Later, the HTTP rest interface, which is based on the simple text format encoding of JSON, basically replaces the complex Web service interface and becomes the primary choice of remote communication in the distributed architecture. But JSON serialization storage takes up a lot of space and performance issues, while mobile client applications require more efficient transfer of data to enhance the user experience. In this case, the language-independent and funny binary coding protocol has become one of the hot technologies that we are pursuing. First the emergence of an open source binary serialization framework-messagepack. It's even earlier than Google's protocol buffers appeared.

The proper serialization protocol can not only improve the universality, robustness, security and optimization performance of the system. It also makes the system easier to debug and extend

Concepts of serialization and deserialization

The process of converting an object to a sequence of bytes is called the serialization of an object.

Conversely, called deserialization

How to implement a serialization operation

1. Implement the Serializable interface

2. ObjectInputStream: Indicates that reading the specified byte data is converted to an object

The function of knowledge serialversionuid of popular science

Class in the file stream and the class in the Classpath, which is the modified class, are incompatible, in security considerations, the program throws an error, and refuses to load. From the error results, if the specified class is not configured for Serialversionuid, then the Java compiler will automatically give the Class A digest algorithm, similar to the fingerprint algorithm, as long as the file has any changes, the resulting UID will be very different, This number is guaranteed to be unique in so many classes. Therefore, because there is no explicit serialversionuid, the compiler generated a UID for us, of course, and the previous saved in the file is not the same, so there are 2 serialized version number inconsistent error. Therefore, as long as we specify the Serialversionuid, we can after serialization, to add a field, or method, without affecting the later restore, the restored object can still be used, but also more methods or properties can be used.

Serialization of static variables

Serialization does not save the state of a static variable

Transient keywords

The Transient keyword indicates that the specified attribute does not participate in serialization

Parent-Child class issues

If the parent class does not implement serialization, the subclass implements column serialization. Then members of the parent class are not able to do serialization operations

Serialization of storage rules

Multiple writes are made to the same object, the first stored result is printed, and the second stored result, with only a 5-byte reference relationship.

does not cause the file to accumulate

Serialization for deep cloning

Shallow copy (shallow copy, Shallow clone): All variables of the copied object contain the same value as the original object, and all references to other objects still point to the original object.

In other words, a shallow copy simply copies the copied object without copying the object it references.

Deep copy (deep copy, Deep clone): All the variables of the copied object contain the same values as the original object, removing the variables that refer to other objects.

Variables that refer to other objects will point to new objects that have been copied, not those that are already referenced.

In other words, a deep copy copies the objects referenced by the object being copied over

Summarize

1. In Java, as long as a class implements the Java.io.Serializable interface, it can be serialized

2. Sequence and deserialization of objects via ObjectOutputStream and ObjectInputStream

3. Whether the object is allowed to be deserialized, not only depends on the object's code consistency, but also has an important factor (UID)

4. Serialization does not save static variables

5. In order for the parent class to participate in the serialization operation, the parent class must also implement the Serializable interface

6. Transient keyword, mainly control whether the variable can be serialized. If a member variable that is not serialized is deserialized, it is set to the initial value, such as String, NULL

7. Deep cloning through serialization operations

What are the mainstream serialization technologies

Json/hessian (2)/xml/protobuf/kryo/msgpack/fst/thrift/protostuff/avro

1.3 Distributed-distributed communication (serialization)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.