netty的客户端怎么在jFinal中启动 是手动运行main函数还是放在afterJFianlStart启动,我放在afterJFinalStart中启动没有反应
客户端启动代码 public class NettySSLClient extends Thread{ private String host; private int port; private boolean auth; public NettySSLClient(String host, int port, boolean auth) { super(); this.host = host; this.port = port; this.auth = auth; } @Override public void run() { try { start(host,port,auth); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void start(String host,int port,boolean auth) throws Exception{ EventLoopGroup group = new NioEventLoopGroup(); try{ Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class) .handler(new ChannelInitializer<SocketChannel>(){ @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); String cChatPath = System.getProperty("user.dir")+"/src/main/resources/netty/conf/cChat.jks"; SSLEngine engine = NettySSLContextFactory.getClientContext(cChatPath,cChatPath,auth).createSSLEngine(); engine.setUseClientMode(true);//客户方模式 pipeline.addLast("ssl", new SslHandler(engine)); pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter())); pipeline.addLast("decoder", new StringDecoder()); pipeline.addLast("encoder", new StringEncoder()); pipeline.addLast("handler", new NettySSLClientHandler()); } }); Channel ch = b.connect(host, port).sync().channel(); ChannelFuture lastWriteFuture = null; BufferedReader in = new BufferedReader(new InputStreamReader( System.in)); for (;;) { String line = in.readLine(); if (line == null) { break; } lastWriteFuture = ch.writeAndFlush(line + "\r\n"); if ("bye".equals(line.toLowerCase())) { ch.closeFuture().sync(); break; } } if (lastWriteFuture != null) { lastWriteFuture.sync(); } }finally{ group.shutdownGracefully(); } } public static void main(String[] args) throws Exception { //new NettySSLClient().start("localhost", 9000,true); } }
jFinal启动代码 @Override public void afterJFinalStart() { NettySSLClient nettyClient = new NettySSLClient("localhost", 9000, true); nettyClient.setDaemon(true); nettyClient.start(); }