Reputation: 1
Im new to Netty and I have a question about storing the data received by clients in the server, for a later consult by whatever clients.
If I define the pipeline as follow:
ch.pipeline().addLast(new ServerHandler());
Can i define in my ServerHandler a class variable as follow ?
public class ServerHandler extends ChannelInboundHandlerAdapter {
private static final ArrayList<String> llamadas = new ArrayList<String>();
....
And after use it whitin all clients like this ?
synchronized(llamadas) {
llamadas.add(mensaje);
}
Also I want to ask if there is any difference if I create only one instance of ServerHandler
ch.pipeline().addLast(serverHandler);
and I annotate the class with @Sharable
make any difference in the access of the variable?
Many thanks!!
regards.
Upvotes: 0
Views: 712
Reputation: 3661
This is more of a Java question than Netty-specific. Since llamadas
is a static class member, you will need to synchronize regardless of whether you make your ChannelHandler @Sharable
or not i.e whether you have multiple instances of the channel handler or only one, you have a single ArrayList
you are appending to.
Generally, @Sharable
is used with handlers that are "stateless" w.r.t channels i.e that do not maintain any channel-specific state, and so you can get away with using a single instance of the handler across pipelines.
See this tutorial for a good explanation of this.
Upvotes: 1