Java源码示例:org.jboss.netty.handler.codec.string.StringEncoder
示例1
public void start(int listenPort, final ExecutorService threadPool, final long timeout) throws Exception {
if (!startFlag.compareAndSet(false, true)) {
return;
}
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline pipeline = new DefaultChannelPipeline();
pipeline.addLast("decoder", new StringDecoder());
pipeline.addLast("encoder", new StringEncoder());
pipeline.addLast("handler", new NettyServerHandler(threadPool, timeout));
return pipeline;
}
});
bootstrap.bind(new InetSocketAddress(listenPort));
log.warn("Server started,listen at: " + listenPort);
}
示例2
@Override
public ChannelPipeline getPipeline() throws Exception
{
ChannelPipeline pipeline = Channels.pipeline();
// Add the text line codec combination first,
FrameDecoder frameDecoder = new LineBasedFrameDecoder(
m_maxSize, true, true);
pipeline.addLast("framer", frameDecoder);
pipeline.addLast("decoder", new WordSplitter());
pipeline.addLast("encoder", new StringEncoder());
// and then business logic.
pipeline.addLast("handler", this);
return pipeline;
}
示例3
public void connect(){
bootstrap = new ClientBootstrap( new NioClientSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()));
bootstrap.setOption("tcpNoDelay", false);
bootstrap.setOption("keepAlive", true);
final NettyClientHandler handler = new NettyClientHandler(this, timer);
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline pipeline = Channels.pipeline();
pipeline.addLast("handler", handler);
pipeline.addLast("encoder", new StringEncoder());
return pipeline;
}
});
bootstrap.setOption("remoteAddress", new InetSocketAddress(host, port));
try {
ChannelFuture future = bootstrap.connect().sync();
channel = future.getChannel();
} catch (Exception e) {
logger.error(ExceptionUtil.getErrorMessage(e));
bootstrap.releaseExternalResources();
System.exit(-1);//第一次连接出现异常直接退出,不走重连
}
}
示例4
@Override
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline pipeline = new DefaultChannelPipeline();
pipeline.addLast("decoder", new StringDecoder());
pipeline.addLast("encoder", new StringEncoder());
pipeline.addLast("handler", handler);
return pipeline;
}
示例5
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline pipeline = pipeline();
pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
pipeline.addLast("decoder", new StringDecoder());
pipeline.addLast("encoder", new StringEncoder());
pipeline.addLast("handler", new NettyTCPWriterHandler(this.bootstrap, this.channel, this.timer));
return pipeline;
}
示例6
public ChannelPipeline getPipeline() throws Exception {
// this is pretty verbose from the netty examples
ChannelPipeline pipeline = Channels.pipeline();
pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
pipeline.addLast("decoder", new StringDecoder());
pipeline.addLast("encoder", new StringEncoder());
pipeline.addLast("handler", new FakeCarbonHandler(appConfig));
return pipeline;
}
示例7
@Override
protected ChannelPipelineFactory createPipelineFactory(final ChannelGroup group) {
return new ChannelPipelineFactory() {
private final ChannelGroupHandler groupHandler = new ChannelGroupHandler(group);
@Override
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline pipeline = pipeline();
Encryption secure = getEncryption();
if (secure != null && !secure.isStartTLS()) {
// We need to set clientMode to false.
// See https://issues.apache.org/jira/browse/JAMES-1025
SSLEngine engine = secure.getContext().createSSLEngine();
engine.setUseClientMode(false);
pipeline.addFirst(SSL_HANDLER, new SslHandler(engine));
}
pipeline.addLast(GROUP_HANDLER, groupHandler);
pipeline.addLast(CONNECTION_LIMIT_HANDLER, new ConnectionLimitUpstreamHandler(ManageSieveServer.this.connectionLimit));
pipeline.addLast(CONNECTION_LIMIT_PER_IP_HANDLER, new ConnectionPerIpLimitUpstreamHandler(ManageSieveServer.this.connPerIP));
// Add the text line decoder which limit the max line length,
// don't strip the delimiter and use CRLF as delimiter
// Use a SwitchableDelimiterBasedFrameDecoder, see JAMES-1436
pipeline.addLast(FRAMER, getFrameHandlerFactory().create(pipeline));
pipeline.addLast(CONNECTION_COUNT_HANDLER, getConnectionCountHandler());
pipeline.addLast(CHUNK_WRITE_HANDLER, new ChunkedWriteHandler());
ExecutionHandler ehandler = getExecutionHandler();
if (ehandler != null) {
pipeline.addLast(EXECUTION_HANDLER, ehandler);
}
pipeline.addLast("stringDecoder", new StringDecoder(CharsetUtil.UTF_8));
pipeline.addLast(CORE_HANDLER, createCoreHandler());
pipeline.addLast("stringEncoder", new StringEncoder(CharsetUtil.UTF_8));
return pipeline;
}
};
}