Some changes to the examples to show the usage of @Sharable

This commit is contained in:
Norman Maurer 2012-06-02 20:13:58 +02:00
parent 19dcb81727
commit f34fc73e89
4 changed files with 18 additions and 7 deletions

View File

@ -17,6 +17,7 @@ package io.netty.example.telnet;
import io.netty.channel.ChannelInboundHandlerContext;
import io.netty.channel.ChannelInboundMessageHandlerAdapter;
import io.netty.channel.ChannelHandler.Sharable;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -24,6 +25,7 @@ import java.util.logging.Logger;
/**
* Handles a client-side channel.
*/
@Sharable
public class TelnetClientHandler extends ChannelInboundMessageHandlerAdapter<String> {
private static final Logger logger = Logger.getLogger(

View File

@ -27,7 +27,9 @@ import io.netty.handler.codec.string.StringEncoder;
* Creates a newly configured {@link ChannelPipeline} for a new channel.
*/
public class TelnetClientInitializer extends ChannelInitializer<SocketChannel> {
private static final StringDecoder DECODER = new StringDecoder();
private static final StringEncoder ENCODER = new StringEncoder();
private static final TelnetClientHandler CLIENTHANDLER = new TelnetClientHandler();
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
@ -35,10 +37,10 @@ public class TelnetClientInitializer extends ChannelInitializer<SocketChannel> {
// Add the text line codec combination first,
pipeline.addLast("framer", new DelimiterBasedFrameDecoder(
8192, Delimiters.lineDelimiter()));
pipeline.addLast("decoder", new StringDecoder());
pipeline.addLast("encoder", new StringEncoder());
pipeline.addLast("decoder", DECODER);
pipeline.addLast("encoder", ENCODER);
// and then business logic.
pipeline.addLast("handler", new TelnetClientHandler());
pipeline.addLast("handler", CLIENTHANDLER);
}
}

View File

@ -17,6 +17,7 @@ package io.netty.example.telnet;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelInboundHandlerContext;
import io.netty.channel.ChannelInboundMessageHandlerAdapter;
@ -28,6 +29,7 @@ import java.util.logging.Logger;
/**
* Handles a server-side channel.
*/
@Sharable
public class TelnetServerHandler extends ChannelInboundMessageHandlerAdapter<String> {
private static final Logger logger = Logger.getLogger(

View File

@ -27,6 +27,10 @@ import io.netty.handler.codec.string.StringEncoder;
* Creates a newly configured {@link ChannelPipeline} for a new channel.
*/
public class TelnetServerPipelineFactory extends ChannelInitializer<SocketChannel> {
private static final StringDecoder DECODER = new StringDecoder();
private static final StringEncoder ENCODER = new StringEncoder();
private static final TelnetServerHandler SERVERHANDLER = new TelnetServerHandler();
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
@ -34,10 +38,11 @@ public class TelnetServerPipelineFactory extends ChannelInitializer<SocketChanne
// Add the text line codec combination first,
pipeline.addLast("framer", new DelimiterBasedFrameDecoder(
8192, Delimiters.lineDelimiter()));
pipeline.addLast("decoder", new StringDecoder());
pipeline.addLast("encoder", new StringEncoder());
// the encoder and decoder are static as these are sharable
pipeline.addLast("decoder", DECODER);
pipeline.addLast("encoder", ENCODER);
// and then business logic.
pipeline.addLast("handler", new TelnetServerHandler());
pipeline.addLast("handler", SERVERHANDLER);
}
}