当我使用Netty 4.0时,Applet 冻结。如果我使用Netty 3,Applet 会正确启动。我的代码很简单,看不懂哪里出错了。
public class Applet extends JApplet
{
@Override
public void init()
{
try {
new DiscardServer(1020).run();
} catch (Exception ex) {
Logger.getLogger(Applet.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public class DiscardServer {
private final int port;
public DiscardServer(int port) {
this.port = port;
}
public void run() throws Exception {
ServerBootstrap b = new ServerBootstrap();
try {
b.group(new NioEventLoopGroup(), new NioEventLoopGroup()).channel(NioServerSocketChannel.class).localAddress(port)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new DiscardServerHandler());
}
});
ChannelFuture f = b.bind().sync();
f.channel().closeFuture().sync();
} finally {
b.shutdown();
}
}
}
public class DiscardServerHandler extends ChannelInboundByteHandlerAdapter {
private static final Logger logger = Logger.getLogger(DiscardServerHandler.class.getName());
@Override
public void inboundBufferUpdated(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
System.out.println("Message " + new String(in.array()));
in.clear();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
logger.log(Level.WARNING,"Unexpected exception from downstream.",cause);
ctx.close();
}
}
控制台中的最后一个帖子:
network: Cache entry not found [url: file:/D:/java/Applet/dist/lib/netty-4.0.0.Alpha6-20121021.180934-2.jar, version: null]
basic: Plugin2ClassLoader.getPermissions CeilingPolicy allPerms
如果我删除该行:
ChannelFuture f = b.bind().sync();
,小程序启动,但 DiscardServer 不工作。