hello! Hello everyone, my name is Xiaoqi. Xiaoqi, a programmer who loves to share, intends to share some technologies in a light and humorous dialogue. If you think you have learned something through Xiaoqi's article, then give Xiaoqi a like. Continue to update, you can search for [Xiaoqi JAVA Interview] on WeChat to read it for the first time, reply to [Information] and there are more benefits I have prepared for you! Reply to [Project] There are some project source codes that I have prepared for you. Reply [Resume Template] There is a resume template that I have prepared for you.
I. Introduction
I picked up the last book. Yesterday it rained and I didn’t walk on the street very much. Today is a sunny day. I’m going to go to the street to watch the old man play chess. After all, my chess skills are ranked high in our talent village. I'm going to give them pointers. .
When I walked to the street, I didn't see anyone playing chess, so I wondered, how could there be no uncle coming out to play chess on such a good day? Could it be that the aunt just came over with a broom to sweep it?
At this time, I saw a few of them playing glass balls on the roadside. They were usually little spies in the village. What happened in the village was definitely clear.
Me: Dog Dan, did you guys see Uncle playing chess today?
Goudan: I see, now the uncle is probably playing fiercely.
Me: where is it? Why did I not see it.
Dog Dan: Let's go, I'll go home and ride my mount to take you there.
Me: Brother Goudan, let's wait a while, you can't open the eyes of this dog, let's just walk.
Dog egg: go.
Goudan led me down seven or eight streets, turning a dozen or so corners, almost to the town, and finally saw the uncles playing chess in our village.
Me: Uncle, how can we run so far in a chess game, and then go abroad after two more steps. . .
Uncle: No way, your aunt just bought a big broom, it hurts to beat someone, you have to run farther.
Me: Yes, then you go first, whoever loses will replace me.
Just when I gave the uncle a trick to smash the other's elephant eyes with the car, my cell phone rang.
Me: "Hello".
Opposite: "Hello, is this Xiao Qi?"
Me: "It's me, are you?".
Opposite: "I'm from XXX company. I saw that HR pushed me your resume. I feel pretty good. When is it convenient for you to come to the site for an interview."
Me: "It's inconvenient for on-site interviews now."
Opposite: "Okay, is it convenient for you now? Let's have an online interview now."
Me: "OK".
2. Interview
Interviewer: I see that you are proficient in Netty on your resume. Can you simply say what is Netty codec?
Me: When we use Netty to send or receive a message, an encoding or decoding occurs. For example, when we receive a message from the outside, we will decode the message. If we send a message, we will convert the message (such as a java object) It is encoded in binary and sent out, because the message is transmitted in binary format during the actual transmission process.
Interviewer: What codecs does Netty have?
Me: Netty provides many codecs, such as StringEncoder for string encoding, StringDecoder for string decoding, ObjectEncoder for object encoding and ObjectDecoder for object decoding.
Interviewer: I feel that these codecs are a bit inefficient. Are there any more efficient codecs?
Me: We can use protostuff, protostuff is a serialization method based on protobuf, which can be serialized with very low performance loss.
Interviewer: How exactly is protostuff used?
Me: First introduce dependencies.
<dependency>
<groupId>com.dyuproject.protostuff</groupId>
<artifactId>protostuff‐api</artifactId>
<version>1.0.10</version>
</dependency>
<dependency>
<groupId>com.dyuproject.protostuff</groupId>
<artifactId>protostuff‐core</artifactId>
<version>1.0.10</version>
</dependency>
<dependency>
<groupId>com.dyuproject.protostuff</groupId>
<artifactId>protostuff‐runtime</artifactId>
<version>1.0.10</version>
</dependency>
Me: Then create a new serializer class.
public class ProtostuffUtil {
private static Map<Class<?>, Schema<?>> cachedSchema = new ConcurrentHashMap<Class<?>, Schema<?>>();
private static <T> Schema<T> getSchema(Class<T> clazz) {
@SuppressWarnings("unchecked")
Schema<T> schema = (Schema<T>) cachedSchema.get(clazz);
if (schema == null) {
schema = RuntimeSchema.getSchema(clazz);
if (schema != null) {
cachedSchema.put(clazz, schema);
}
}
return schema;
}
me: write serialize deserialize method.
/**
* 序列化
*
* @param obj
* @return
*/
public static <T> byte[] serializer(T obj) {
@SuppressWarnings("unchecked")
Class<T> clazz = (Class<T>) obj.getClass();
LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);
try {
Schema<T> schema = getSchema(clazz);
return ProtostuffIOUtil.toByteArray(obj, schema, buffer);
} catch (Exception e) {
throw new IllegalStateException(e.getMessage(), e);
} finally {
buffer.clear();
}
}
/**
* 反序列化
*
* @param data
* @param clazz
* @return
*/
public static <T> T deserializer(byte[] data, Class<T> clazz) {
try {
T obj = clazz.newInstance();
Schema<T> schema = getSchema(clazz);
ProtostuffIOUtil.mergeFrom(data, obj, schema);
return obj;
} catch (Exception e) {
throw new IllegalStateException(e.getMessage(), e);
}
}
Interviewer: "You are not bad, when will you be able to return to Beijing for a job?"
Me: "Um... Wait, there are still many companies waiting to negotiate salary, I have to pick a suitable one."
Interviewer: "I'll give you as much as you want, come to me"
Me: "Um...then the monthly salary is 100 W."
Interviewer: "Hey, I can't hear what you said, the signal is not good..."
Me: "Hey hey hey" (beep beep beep beep beep...).
3. Summary
The relevant content here has not been sorted out, and the article will continue to be updated later, and it is recommended to collect it.
The commands involved in the article must be knocked several times like me. Only in the process of knocking can you find out whether you really master the commands.
If you think my article is not bad, please like it. In addition, you can search for [Xiaoqi JAVA Interview] on WeChat to read it for the first time, and reply to [Information] and there are more benefits I have prepared for you! Reply to [Project] There are some project source codes that I have prepared for you. Reply [Resume Template] There is a resume template that I have prepared for you.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。