Say so much nonsense, only to mention the transmission of objects, do not know whether you are impatient. It is not realistic to pass the message inside a system without object passing. Let's say, how to pass the object.
If you have seen the previous introduction, if you are good at focusing on the essence, diligent thinking. You should also think, as we have said, that Netty's message delivery is flow-based, passing through Channelbuffer, then naturally, object also needs to be converted to Channelbuffer to pass. Fortunately, Netty itself has written such a conversion tool for us. Objectencoder and Objectdecoder.
How does the tool work? Once again the so-called essence, as we have said before, Netty gives us the space to deal with our business is on the flexible definable handler, that is to say, if we do this transformation work, then we should also do in the handler. And Netty, provided to our objectencoder and decoder is precisely a group of handler. Then, modify the startup code for the server and client:
View Sourceprint?
01.
// 设置一个处理客户端消息和各种消息事件的类(Handler)
02.
bootstrap.setPipelineFactory(
new
ChannelPipelineFactory() {
03.
@Override
04.
public
ChannelPipeline getPipeline()
throws
Exception {
05.
return
Channels.pipeline(
06.
new
ObjectDecoder(ClassResolvers.cacheDisabled(
this
07.
.getClass().getClassLoader())),
08.
new
ObjectServerHandler());
09.
}
10.
});
// 设置一个处理服务端消息和各种消息事件的类(Handler)
11.
bootstrap.setPipelineFactory(
new
ChannelPipelineFactory() {
12.
@Override
13.
public
ChannelPipeline getPipeline()
throws
Exception {
14.
return
Channels.pipeline(
new
ObjectEncoder(),
15.
new
ObjectClientHandler());
16.
}
17.
});
To pass an object, it is natural to have a passing model, a simple pojo, and of course, implementing a serialized interface is a must.
View Sourceprint?
01.
/**
02.
* @author lihzh
03.
* @alia OneCoder
04.
* @blog http://www.it165.net
05.
*/
06.
public
class
Command
implements
Serializable {
07.
08.
private
static
final
long
serialVersionUID = 7590999461767050471L;
09.
10.
private
String actionName;
11.
12.
public
String getActionName() {
13.
return
actionName;
14.
}
15.
16.
public
void
setActionName(String actionName) {
17.
this
.actionName = actionName;
18.
}
19.
}
Service side and client, our custom handler implementation is as follows:
View Sourceprint?
01.
/**
02.
* 对象传递服务端代码
03.
*
04.
* @author lihzh
05.
* @alia OneCoder
06.
* @blog http://www.it165.net
07.
*/
08.
public
class
ObjectServerHandler
extends
SimpleChannelHandler {
09.
10.
/**
11.
* 当接受到消息的时候触发www.it165.net
12.
*/
13.
@Override
14.
public
void
messageReceived(ChannelHandlerContext ctx, MessageEvent e)
15.
throws
Exception {
16.
Command command = (Command) e.getMessage();
17.
// 打印看看是不是我们刚才传过来的那个
18.
System.out.println(command.getActionName());
19.
}
20.
}
/**
21.
* 对象传递,客户端代码
22.
*
23.
* @author lihzh
24.
* @alia OneCoder
25.
* @blog http://www.it165.net
26.
*/
27.
public
class
ObjectClientHandler
extends
SimpleChannelHandler {
28.
29.
/**
30.
* 当绑定到服务端的时候触发,给服务端发消息。
31.
*
32.
* @author lihzh
33.
* @alia OneCoder
34.
*/
35.
@Override
36.
public
void
channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) {
37.
// 向服务端发送Object信息
38.
sendObject(e.getChannel());
39.
}
40.
41.
/**
42.
* 发送Object
43.
*
44.
* @param channel
45.
* @author lihzh
46.
* @alia OneCoder
47.
*/
48.
private
void
sendObject(Channel channel) {
49.
Command command =
new
Command();
50.
command.setActionName(
"Hello action."
);
51.
channel.write(command);
52.
}
53.
54.
}
After startup, the service side normally prints the result: Hello action.
Simply comb your thoughts:
- Pass through the Netty, all need to flow based, in the form of Channelbuffer. So, Object---Channelbuffer.
- Netty provides a conversion tool that requires us to configure to handler.
- Sample from the client-side server, one-way message, so the client configured the encoding, the service-side decoding. If you are sending and receiving two-way, you need to configure encoder and decoder.
It is important to note that the handler registered to the server are sequential, if you reverse the registration order:
View Sourceprint?
01.
bootstrap.setPipelineFactory(
new
ChannelPipelineFactory() {
02.
@Override
03.
public
ChannelPipeline getPipeline()
throws
Exception {
04.
return
Channels.pipeline(
new
ObjectServerHandler(),
05.
new
ObjectDecoder(ClassResolvers.cacheDisabled(
this
06.
.getClass().getClassLoader()))
07.
);
08.
}
09.
});
As a result, we will first enter our own business and then decode it. This is not natural, will be strong turn failure. At this point, you should pass the object with Netty.
Java NIO Framework Netty Tutorial (eight) object objects passing