10.
}
};
BootstrapTemplate.newTcpClientBootstrap(host, port, initializer
);
return this;
}
public static void main(String[] args) throws Exception {
int time = (int) (System.currentTimeMillis() / 1000L);
PurchaseData data = new PurchaseData(1001, 499.99f, "Trieu",
"Amazon", time, false );
new PurchaseClient("127.0.0.1",8007).send(data, rs > {
System.out.println(rs);
});
}
}
Recipe 2.4 Listening multiple sockets in one Netty
instance
Problem:
In networking development, sometimes you want your server listen multiple sockets
in one instance. Example , you could implement a HTTP server , which would listen in
2 sockets. The first one for public, that serves all traffic requests from Internet. The
second one for private usage, which only internal networking management or
networking monitoring.
How to do it
We use 2 independent objects of ServerBootstrap to solve this problem. Each of them
bind to different socket, so the request would be processed in 2 independent
instances of ChannelHandler. Loot at this code:
try {
//public service processor
24.
server.close();
}
}
In code of ClientAvroRPC
public class ClientAvroRPC {
public static void main(String[] args) throws IOException {
if(args.length < 3){
args = new String[]
{"someone@example.com","myself@example.com","Hello !"};
}
NettyTransceiver client = new NettyTransceiver(new
InetSocketAddress(10000));
Mail proxy = (Mail) SpecificRequestor.getClient(Mail.class, client);
LogUtil.println("ClientAvroRPC built OK, got proxy, ready to send data
...");
Message message = new Message();
message.setTo(new Utf8(args[0]));
message.setFrom(new Utf8(args[1]));
message.setBody(new Utf8(args[2]));
LogUtil.println("Calling proxy.send with message: " +
message.toString());
LogUtil.println("Result from server: " + proxy.send(message));
client.close();
}
}
How it works
When you start ProxyServerAvroRPC, the socket at port 10000 is listened.
The client, ClientAvroRPC would send a mail message “Hello !”, which is serialized and
is sent to ServerAvroRPC via the method send of the instance of Mail class. The output
when you run the code of ClientAvroRPC is:
Client built, got proxy
Calling proxy.send with message: {"to": "someone@example.com", "from":
"myself@example.com", "body": "Hello !"}