In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
How to use JID for high-performance serialization of Java objects, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.
JID is used to deserialize, update, and deserialize Map objects, and it takes only 4 milliseconds to complete 10000 entries on the machine of the i5 2.53GHz processor. Serialization and deserialization time depends largely on the size and complexity of table entries, and incremental serialization and deserialization are used to achieve high performance.
Today I experimented with the serialization process of Java objects using JID, and now I would like to share this process with you, as for the scenario in which you use it.
First of all, JID depends on JActor and SLF4J, and the jar file of both is included in the JID package you download.
We do not need to care about the purpose of some of the classes involved in the code, let's start with the examples!
Let's assume that we want to serialize a User object, and the User object is as follows:
/ * * serialize object * @ author Winter Lau * / public static class User extends AppJid {private StringJid getNameJid () throws Exception {return (StringJid) _ iGet (0);} private IntegerJid getAgeJid () throws Exception {return (IntegerJid) _ iGet (1);} public String getName () throws Exception {return getNameJid (). GetValue () } public void setName (String name) throws Exception {getNameJid () .setValue (name);} public int getAge () throws Exception {return getAgeJid () .getValue ();} public void setAge (int age) throws Exception {getAgeJid () .setValue (age);}}
Then each object to be serialized needs to have a corresponding factory class, here is UserFactory:
Public static class UserFactory extends AppJidFactory {final public static UserFactory fac = new UserFactory (); public UserFactory () {super ("User", JidFactories.STRING_JID_TYPE, JidFactories.INTEGER_JID_TYPE);} protected User instantiateActor () throws Exception {return new User ();}}
Pay special attention to the parameter "User" in the UserFactory constructor, which you need to use below.
Then there is the test program:
Public static void main (String [] args) throws Exception {JAFactory factory = new JAFactory () {{(new JidFactories ()) .initialize (this);}}; factory.registerActorFactory (UserFactory.fac); RootJid rootJid = (RootJid) factory.newActor (JidFactories.ROOT_JID_TYPE); long ct = System.currentTimeMillis (); rootJid.setValue ("User"); User user = (User) rootJid.getValue (); user.setName ("Winter Lau") User.setAge (98); int slen = rootJid.getSerializedLength (); byte [] sdatas = new byte [slen]; rootJid.save (sdatas, 0); rootJid.load (sdatas, 0, slen); User user1 = (User) rootJid.getValue (); System.out.printf ("% dms- >% SV% d\ n", (System.currentTimeMillis ()-ct), user1.getName (), user1.getAge ()) } Program explanation:
1. First you need to construct JAFactory, which is a necessary step to use JID (line 3)
two。 Then register the class we need to serialize (line 4)
3. Construct the RootJid, which is also a necessary step (line 6)
4. RootJid.setValue ("User") this is the type of object to be serialized. This "User" is the string we defined in UserFactory.
5. Set the value of the User object instance
6. To serialize using the rootJid.save method is to rename the object as an byte array
7. * * one step is to demonstrate loading an object from byte data and printing its properties
This is a complete process of serializing and deserializing custom objects using JID. If you are using some data types native to Java, you do not need to create your own class objects.
What needs to be paid attention to here is the parameter order of the constructor in UserFactory. * the parameters are type names, and each of the following parameters corresponds to the type of each attribute, which must strictly correspond to the value of xx in _ iGet (xx) in the User class.
It is said that the serialization performance of JID is extremely high, which I haven't tested yet. It is important to note that the result of this serialization is incompatible with the serialization of Java itself.
For the complete code and serialization of the list, see:
Https://github.com/oschina/cache-framework/tree/master/src/net/oschina/demo
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.