0
我有Netty Web服務器應該使用Hibernate檢索數據並將響應返回給客戶端。EventLoopGroup和BIO操作。
所以我只是想知道我的實現是正確的
ChannelPipeline pipeline = ch.pipeline();
EventExecutorGroup ex = new DefaultEventExecutorGroup(64);
pipeline.addLast("decoder", new HttpRequestDecoder());
pipeline.addLast("aggregator", new HttpObjectAggregator(65536));
pipeline.addLast("encoder", new HttpResponseEncoder());
pipeline.addLast("chunkedWriter", new ChunkedWriteHandler());
pipeline.addLast(ex,"handler", new HttpWebServerHandler());
,然後裏面messageReceived
方法:
@Override
public void messageReceived(ChannelHandlerContext ctx, FullHttpRequest request)
throws Exception {
if (!request.getDecoderResult().isSuccess()) {
sendError(ctx, BAD_REQUEST);
return;
}
ctx.executor().execute(new MyRunnable(ctx));
}
和裏面的Runnable:
@Override
public void run() {
SomeObejct so = HibernateTemplate.getSomeObject() ;
String serializedSo = serialize(so);
FullHttpResponse res = new DefaultFullHttpResponse(
HTTP_1_1, OK, Unpooled.copiedBuffer(serializedSo, CharsetUtil.UTF_8));
res.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8"); ctx.write(res).addListener(ChannelFutureListener.CLOSE);
}
我這樣做是正確的?