Netty 4.0&小程序

时间:2012-10-27 09:39:38

标签: java applet netty

当我使用 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无法运行。

0 个答案:

没有答案