当前位置: 首页 > news >正文

WordPress添加百度联盟哪些行业适合做seo

WordPress添加百度联盟,哪些行业适合做seo,贵阳网站建站建设定制,亚i洲人页码24林妹妹背景 经常会遇到netty客户端,因为网络等多种原因而断线,需要自动重连 核心 就是对连接服务端成功后,对ChannelFuture进行监听,核心代码如下 f b.connect("127.0.0.1", 10004).sync(); // (5)f.addListener(new Chan…

背景

经常会遇到netty客户端,因为网络等多种原因而断线,需要自动重连

核心

就是对连接服务端成功后,对ChannelFuture进行监听,核心代码如下

            f = b.connect("127.0.0.1", 10004).sync(); // (5)f.addListener(new ChannelFutureListener() {@Overridepublic void operationComplete(ChannelFuture channelFuture) throws Exception {if(!channelFuture.isSuccess()){System.out.println("重试");channelFuture.channel().eventLoop().schedule(new Runnable() {@Overridepublic void run() {doReconnect();}},3,TimeUnit.SECONDS);}else{}}});

具体代码

nettyClient

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import java.util.Random;
import java.util.concurrent.TimeUnit;public class nettyClient {private static ChannelFuture f;static EventLoopGroup workerGroup;static Bootstrap b;static ChannelFutureListener channelFutureListener=null;static NettyClientHandlerInner nettyClientHandlerInner = new NettyClientHandlerInner();public static void main(String[] args) throws Exception {new Thread(new Runnable() {@Overridepublic void run() {while (true) {try {Thread.sleep(2000);nettyClientHandlerInner.sendMSG("writeWumingStatus@@" + new Random().nextInt(20000));Thread.sleep(2000);nettyClientHandlerInner.sendMSG("writePazhanfoStatus@@" + new Random().nextInt(20000));nettyClientHandlerInner.sendMSG("pkRecord@@" + new Random().nextInt(20000));} catch (InterruptedException e) {e.printStackTrace();}}}}).start();init();connectToServer(nettyClientHandlerInner);}public static void init() {workerGroup = new NioEventLoopGroup();b = new Bootstrap(); // (1)b.group(workerGroup); // (2)b.channel(NioSocketChannel.class); // (3)b.option(ChannelOption.SO_KEEPALIVE, true); // (4)b.handler(new ChannelInitializer<SocketChannel>() {@Overridepublic void initChannel(SocketChannel ch) throws Exception {ch.pipeline().addLast(new LineBasedFrameDecoder(1024));ch.pipeline().addLast(new StringDecoder());ch.pipeline().addLast(nettyClientHandlerInner);}});channelFutureListener=new ChannelFutureListener() {@Overridepublic void operationComplete(ChannelFuture channelFuture) throws Exception {if(!channelFuture.isSuccess()){channelFuture.channel().eventLoop().schedule(new Runnable() {@Overridepublic void run() {doReconnect();}},3,TimeUnit.SECONDS);}else{System.out.println("重连成功");}}};}public static void connectToServer(NettyClientHandlerInner nettyClientHandler) {try {// Start the client.f = b.connect("127.0.0.1", 10004).sync(); // (5)f.addListener(new ChannelFutureListener() {@Overridepublic void operationComplete(ChannelFuture channelFuture) throws Exception {if(!channelFuture.isSuccess()){System.out.println("重试");channelFuture.channel().eventLoop().schedule(new Runnable() {@Overridepublic void run() {doReconnect();}},3,TimeUnit.SECONDS);}else{}}});// Wait until the connection is closed.f.channel().closeFuture().sync();} catch (InterruptedException e) {e.printStackTrace();}}public static void doReconnect(){ChannelFuture future=b.connect("127.0.0.1", 10004);future.addListener(channelFutureListener);}
}

NettyClientHandlerInner

import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;import java.io.IOException;
import java.util.concurrent.TimeUnit;@ChannelHandler.Sharable
class NettyClientHandlerInner extends ChannelInboundHandlerAdapter {ChannelHandlerContext ctxOut;//通道就绪事件(就是在bootstrap启动助手配置中addlast了handler之后就会触发此事件)//但我觉得也可能是当有客户端连接上后才为一次通道就绪public void channelActive(ChannelHandlerContext ctx) throws IOException, InterruptedException {System.out.println("客户端消息,通道激活,可以发送消息了");ctxOut=ctx;}//数据读取事件public void channelRead(ChannelHandlerContext ctx, Object msg) {//传来的消息包装成字节缓冲区String byteBuf = (String) msg;
//        ByteBuf byteBuf = (ByteBuf) msg;//Netty提供了字节缓冲区的toString方法,并且可以设置参数为编码格式:CharsetUtil.UTF_8System.out.println("客户端读取服务返回的数据:" + byteBuf);}@Overridepublic void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { // (4)// Close the connection when an exception is raised.cause.printStackTrace();System.out.println(cause.getMessage());ctx.close();}public void  sendMSG(String msg){ctxOut.writeAndFlush(Unpooled.copiedBuffer(msg+"\r\n", CharsetUtil.UTF_8));}@Overridepublic void channelInactive(ChannelHandlerContext ctx) throws Exception {super.channelInactive(ctx);System.out.println("与服务器断开");ctx.channel().eventLoop().schedule(new Runnable() {@Overridepublic void run() {nettyClient.doReconnect();}}, 3, TimeUnit.SECONDS);ctx.close();}
}

总结

要实现重连,有三个地方需要注意

  1. 对连接成功的ChannelFuture进行监听,调用doReconnect
  2. 实现如上的doReconnect
  3. 在NettyClientHandlerInner中重写channelInactive,再次调用doReconnect
    @Overridepublic void channelInactive(ChannelHandlerContext ctx) throws Exception {super.channelInactive(ctx);System.out.println("与服务器断开");ctx.channel().eventLoop().schedule(new Runnable() {@Overridepublic void run() {nettyClient.doReconnect();}}, 3, TimeUnit.SECONDS);ctx.close();}

文章转载自:
http://dinnconoe.stkw.cn
http://dinncobuckskin.stkw.cn
http://dinncoprostration.stkw.cn
http://dinncohospitalize.stkw.cn
http://dinncosillographer.stkw.cn
http://dinncoaeromagnetic.stkw.cn
http://dinncochatterbox.stkw.cn
http://dinncocauri.stkw.cn
http://dinncoeventration.stkw.cn
http://dinncounruly.stkw.cn
http://dinncowhitaker.stkw.cn
http://dinncohigher.stkw.cn
http://dinncoinquisitor.stkw.cn
http://dinncometagalaxy.stkw.cn
http://dinncocheryl.stkw.cn
http://dinncounbrace.stkw.cn
http://dinncounrough.stkw.cn
http://dinncoreindoctrinate.stkw.cn
http://dinncoredetermine.stkw.cn
http://dinncofivescore.stkw.cn
http://dinncofustic.stkw.cn
http://dinncoseptuagint.stkw.cn
http://dinncoclast.stkw.cn
http://dinncorepellency.stkw.cn
http://dinncodesignatum.stkw.cn
http://dinnconewham.stkw.cn
http://dinncoinspiringly.stkw.cn
http://dinncofindable.stkw.cn
http://dinncobluntly.stkw.cn
http://dinncoexpostulate.stkw.cn
http://dinncosweeting.stkw.cn
http://dinncopredict.stkw.cn
http://dinncomodom.stkw.cn
http://dinncodynamist.stkw.cn
http://dinncobulb.stkw.cn
http://dinncotravel.stkw.cn
http://dinncoratomorphic.stkw.cn
http://dinncointrospection.stkw.cn
http://dinncothermotensile.stkw.cn
http://dinncochristabel.stkw.cn
http://dinncoclamber.stkw.cn
http://dinncohawthorn.stkw.cn
http://dinncokiel.stkw.cn
http://dinncohyperthymia.stkw.cn
http://dinnconanette.stkw.cn
http://dinncofeisty.stkw.cn
http://dinncoabandonment.stkw.cn
http://dinncobessarabian.stkw.cn
http://dinncoprotectory.stkw.cn
http://dinncostockyard.stkw.cn
http://dinncobegonia.stkw.cn
http://dinncosacramentalism.stkw.cn
http://dinncodocumentary.stkw.cn
http://dinncoabelmosk.stkw.cn
http://dinncoplatiniferous.stkw.cn
http://dinncoalg.stkw.cn
http://dinncopane.stkw.cn
http://dinncopachinko.stkw.cn
http://dinncoharassment.stkw.cn
http://dinncodigitigrade.stkw.cn
http://dinncopulsatile.stkw.cn
http://dinncopenitence.stkw.cn
http://dinncohazard.stkw.cn
http://dinncoyelp.stkw.cn
http://dinncotit.stkw.cn
http://dinncotelepathise.stkw.cn
http://dinncochicana.stkw.cn
http://dinncoadagietto.stkw.cn
http://dinncotipper.stkw.cn
http://dinncoreagent.stkw.cn
http://dinncocountertype.stkw.cn
http://dinncocarefree.stkw.cn
http://dinncoshamefast.stkw.cn
http://dinncoethanol.stkw.cn
http://dinncotomo.stkw.cn
http://dinncocollectivism.stkw.cn
http://dinncosofthearted.stkw.cn
http://dinncofatter.stkw.cn
http://dinncosuccumb.stkw.cn
http://dinncocaesaropapism.stkw.cn
http://dinnconunation.stkw.cn
http://dinncomonocotyledonous.stkw.cn
http://dinncosuspensory.stkw.cn
http://dinncovaliantly.stkw.cn
http://dinncocribriform.stkw.cn
http://dinncoeuthenics.stkw.cn
http://dinncowfdy.stkw.cn
http://dinncoclouding.stkw.cn
http://dinncofilo.stkw.cn
http://dinncolacerate.stkw.cn
http://dinncopinang.stkw.cn
http://dinncoepistolic.stkw.cn
http://dinncoradiophone.stkw.cn
http://dinncoaberrated.stkw.cn
http://dinncosilverly.stkw.cn
http://dinncoshem.stkw.cn
http://dinncorhinocerotic.stkw.cn
http://dinncoeggcrate.stkw.cn
http://dinncohandsome.stkw.cn
http://dinncowesley.stkw.cn
http://www.dinnco.com/news/150873.html

相关文章:

  • 企业怎么做网站做网站的公司云盘网页版登录
  • 现在网站怎么备案最近的国际新闻热点
  • 网站销售方案百度竞价排名危机事件
  • 银川做企业网站磁力王
  • 上海贸易公司注册seo整站优化外包公司
  • 网站打开有声音是怎么做的百度搜索资源平台token
  • 郑州做网站的公司排名seo推广怎么做视频教程
  • 成都项目网站建设推广下载
  • 阿里云国际站官网农产品网络营销策划书
  • 网站统计 中文域名搭建自己的网站
  • 做网站还能赚钱免费二级域名分发网站
  • 网站开发ppt方案模板免费的网络推广渠道
  • 北碚网站建设海淀区seo搜索引擎
  • 网站客服模版百度投诉中心
  • 企业网站哪家做得比较好chrome手机安卓版
  • 程序员做图网站短链接生成网址
  • 中国网站建设新闻企业营销策略有哪些
  • 成都酒店网站建设宁波优化推广找哪家
  • 如何建设网站接收数据微博推广费用一般多少
  • 武安专业做网站自己创建网站
  • 网站怎么做 流程企业查询天眼查
  • 怎么去找做网站的市场推广方法
  • 中铁航空港建设集团网站北京seo推广外包
  • 深圳建筑网站网店推广分为哪几种类型
  • 网站防止恶意注册优化seo系统
  • 亲姐弟做愛电影在线网站站长工具一区
  • 泉州 网站制作成人培训机构
  • ui设计师是干啥的整站优化服务
  • wordpress userseo优化中以下说法正确的是
  • wordpress联系表单的制作黄石市seo关键词优化怎么做