patternjavaMinor
High-volume object-passing from client to server
Viewed 0 times
passinghighobjectclientserverfromvolume
Problem
I have 1 client and 1 server. Client passes Java objects (throughput 10000/sec). I am using Netty. I am short on CPU and this code consumes a lot.
I have to make Server1 (Netty Client) talk to Server2 (Netty Server). I do not expect any response from Netty Server (Any optimization on this point? e.g closing some events might make it faster?). I am creating a Channel object and reusing it for connection.
Code to send message. In real code I initiate
My clientHandler:
```
public class ClientHandler extends ChannelInboundHandlerAdapter {
public Channel channel;
/**
* Creates a client-side handler.
*/
public void init()
{
String host = "MY HOST";
int port = 8114;
EventLoopGroup group = new NioEventLoopGroup();
Bootstrap b = new Bootstrap();
try {
b.group(group)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
p.addLast(
new ObjectEncoder(),
new ObjectDecoder(ClassResolvers.cacheDisabled(getClass().getClassLoader())),
new ClientHandler());
}
});
// Start the connection attempt.
ChannelFuture f= b.connect(host, port);
channel=f.awaitUninterruptibly().channel();
TestObj obj= new TestObj();
I have to make Server1 (Netty Client) talk to Server2 (Netty Server). I do not expect any response from Netty Server (Any optimization on this point? e.g closing some events might make it faster?). I am creating a Channel object and reusing it for connection.
Code to send message. In real code I initiate
CLientHandler only once and reuse the same instance.public static void main(String[] args) throws Exception {
final ClientHandler client= new ClientHandler();
client.init();
TestObj obj= new TestObj();
client.send(obj);
}My clientHandler:
```
public class ClientHandler extends ChannelInboundHandlerAdapter {
public Channel channel;
/**
* Creates a client-side handler.
*/
public void init()
{
String host = "MY HOST";
int port = 8114;
EventLoopGroup group = new NioEventLoopGroup();
Bootstrap b = new Bootstrap();
try {
b.group(group)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
p.addLast(
new ObjectEncoder(),
new ObjectDecoder(ClassResolvers.cacheDisabled(getClass().getClassLoader())),
new ClientHandler());
}
});
// Start the connection attempt.
ChannelFuture f= b.connect(host, port);
channel=f.awaitUninterruptibly().channel();
TestObj obj= new TestObj();
Solution
This does not address the optimization question, I just want to point out something.
As codesparkle said in a lighter way, your indentation is pretty bad.
Here is how your indentation should be:
-
Everything starts with 0 indenations.
-
All code in code blocks gets 1 more indentation.
-
Stay consistent with indentation. For example, if you are going to use 1 tab for indentation, stick with one tab throughout your entire code.
With this, your code will become much, much more easy to read.
If you have trouble keeping track of indentation, get a text editor. Most text editors will automatically indent for you, making it so that you don't have to worry about it yourself.
Here is one example for where your indentation could be fixed:
Gets turned into
-
The signature of main and the closing
-
Everything inside the only code block (between
This is what the rest of your code should look like; no mixed spaces/lost tabs/etc.
This may just be me, but I find that it is extremely difficult to review poorly indented code (maybe that is why it has taken a while for you get any review).
I recommend that you fix the indentation and then re-post here as a follow up question so you get better reviews.
I will look at your code again and edit my answer to see if I can make anything of it.
As codesparkle said in a lighter way, your indentation is pretty bad.
Here is how your indentation should be:
-
Everything starts with 0 indenations.
-
All code in code blocks gets 1 more indentation.
-
Stay consistent with indentation. For example, if you are going to use 1 tab for indentation, stick with one tab throughout your entire code.
With this, your code will become much, much more easy to read.
If you have trouble keeping track of indentation, get a text editor. Most text editors will automatically indent for you, making it so that you don't have to worry about it yourself.
Here is one example for where your indentation could be fixed:
public static void main(String[] args) throws Exception {
final ClientHandler client= new ClientHandler();
client.init();
TestObj obj= new TestObj();
client.send(obj);
}Gets turned into
public static void main(String[] args) throws Exception {
final ClientHandler client= new ClientHandler();
client.init();
TestObj obj= new TestObj();
client.send(obj);
}-
The signature of main and the closing
} are not indented-
Everything inside the only code block (between
{ and }) is indented a single tab.This is what the rest of your code should look like; no mixed spaces/lost tabs/etc.
This may just be me, but I find that it is extremely difficult to review poorly indented code (maybe that is why it has taken a while for you get any review).
I recommend that you fix the indentation and then re-post here as a follow up question so you get better reviews.
I will look at your code again and edit my answer to see if I can make anything of it.
Code Snippets
public static void main(String[] args) throws Exception {
final ClientHandler client= new ClientHandler();
client.init();
TestObj obj= new TestObj();
client.send(obj);
}public static void main(String[] args) throws Exception {
final ClientHandler client= new ClientHandler();
client.init();
TestObj obj= new TestObj();
client.send(obj);
}Context
StackExchange Code Review Q#92293, answer score: 2
Revisions (0)
No revisions yet.