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

政务网站源码东坑镇仿做网站

政务网站源码,东坑镇仿做网站,会宁网站建设公司,当下最火的购物平台highlight: arduino-light ServerBootstrap Bootstrap 意思是引导#xff0c;一个 Netty 应用通常由一个 Bootstrap 开始#xff0c;主要作用是配置整个 Netty 程序#xff0c;串联各个组件#xff0c;Netty 中ServerBootstrap 是服务端启动引导类。 java //泛型 AbstractB… highlight: arduino-light ServerBootstrap Bootstrap 意思是引导一个 Netty 应用通常由一个 Bootstrap 开始主要作用是配置整个 Netty 程序串联各个组件Netty 中ServerBootstrap 是服务端启动引导类。 java //泛型 AbstractBootstrapB extends AbstractBootstrapB, C, C extends Channel ​ ServerBootstrap extends AbstractBootstrapServerBootstrap, ServerChannel ​ //B指的是 ServerBootstrap,C指的是ServerChannel ​ //该方法用于服务器端用来设置两个 EventLoop,分别是bossGroup和workerGroup public ServerBootstrap group(EventLoopGroup parentGroup, EventLoopGroup childGroup)     //该方法用来设置一个服务器端的通道实现即指定服务器端的ServerSocketchannel的类型 public ServerBootstrap channel(Class? extends ServerChannel channelClass) ​ //用来给ServerSocketchannel添加配置 public T ServerBootstrap option(ChannelOptionT option, T value) ​ //用来给接收到的客户端Socketchannel添加配置 public T ServerBootstrap childOption(ChannelOptionT childOption, T value) ​ //该方法用来设置业务处理类。即自定义用来处理客户端Socketchannel的handler public ServerBootstrap childHandler(ChannelHandler childHandler) ​ //该方法用于服务器端用来设置占用的端口号 public ChannelFuture bind(int inetPort) Bootstrap Bootstrap 意思是引导一个 Netty 应用通常由一个 Bootstrap 开始主要作用是配置整个 Netty 程序串联各个组件Netty 中 Bootstrap 类是客户端程序的启动引导类。 java //泛型 AbstractBootstrapBootstrap, Channel ​ Bootstrap extends AbstractBootstrapBootstrap, Channel B指的是 ,C指的是Channel ​ //该方法用于客户端用来设置一个 EventLoop public Bootstrap  group(EventLoopGroup group) //该方法用来设置一个客户端的端的通道实现即指定客户端的Socketchannel的类型 Bootstrap channel(Class? extends Channel channelClass)     //该方法用来设置业务处理类。即自定义用来处理客户端Socketchannel的handler Bootstrap handler(ChannelHandler handler)     //该方法用于客户端用来连接服务器端 public ChannelFuture connect(String inetHost, int inetPort) Future和ChannelFuture Future 说明 表示异步的执行结果, 可以通过它提供的方法来检测执行是否完成比如检索计算等等。 java /****     * 对账业务生成数据     */    Override    public void pullData(String time) {        if (StringUtils.isEmpty(time)) {            time DateTimeUtils.getDateString(-1);       }        String lockKey time;        Mutex mutex null;        try {            mutex iLockService.lock(MutexLockEnum.PULL_DATA.getKey(), lockKey,                    MuteLockTimeConstant.RECORD_ACCOUNT_FLOW_WAIT_TIME,                    MuteLockTimeConstant.RECORD_ACCOUNT_FLOW_LEASE_TIME, TimeUnit.SECONDS);       } catch (Exception e) {            ReconciliationException.throwException(ExceptionEnum.GET_LOCK_FAIL);       } ​        try {            //step1. 查询所有sql            ListReconciliationSqlEo sqlList reconciliationSqlDas.selectReconciliationListData();            ArrayListFutureClearResult list new ArrayList();            if (CollectionUtils.isNotEmpty(sqlList)) {                for (ReconciliationSqlEo reconciliationSqlEo : sqlList) {                    ReconciliationPullControlEo history getHistory(time, reconciliationSqlEo);                    if (Objects.nonNull(history)) {                        continue;                   }                    final String finalTime time;                    FutureClearResult future ThreadPoolUtils.submit(() - {                                String totalRecordSql reconciliationSqlEo.getTotalRecordSql();                                Integer belongCenterCode reconciliationSqlEo.getBelongCenterCode();                                String node reconciliationSqlEo.getNode();                                RestResponseListReconciliationDataDto recordResponse null;                                if (BelongCenterCode.PAYMENT.getCode().equals(belongCenterCode)) {                                    recordResponse paymentReconciliationApi.queryList(totalRecordSql);                               } else if (BelongCenterCode.SETTLEMENT.getCode().equals(belongCenterCode)) {                                    recordResponse iSettlementQueryApi.queryReconciliationData(totalRecordSql);                               } else if (BelongCenterCode.ACCOUNT.getCode().equals(belongCenterCode)) {                                    recordResponse accountReconciliationApi.queryReconciliationData(totalRecordSql);                               } else if (BelongCenterCode.U9_THROW.getCode().equals(belongCenterCode)                                         BusinessNode.THROW_ACCOUNT_COLLECTION.getCode().equals(node)) {                                    recordResponse u9ViewService.selectCollectionThrow(finalTime);                               } else if (BelongCenterCode.U9_THROW.getCode().equals(belongCenterCode)                                         BusinessNode.THROW_ACCOUNT_PAY.getCode().equals(node)) {                                    recordResponse u9ViewService.selectPaymentThrow(finalTime);                               } else if (BelongCenterCode.OFFLINE_SETTLE.getCode().equals(belongCenterCode)                                         BusinessNode.OFFLINE_SETT.getCode().equals(node)) {                                    recordResponse u9ViewService.selectSettle(finalTime);                               }                                log.info(拉取内部数据sql:{},响应:{}, totalRecordSql, recordResponse);                                ClearResult clearResult new ClearResult(Boolean.FALSE, belongCenterCode, node);                                try {                                    addData(reconciliationSqlEo, recordResponse, clearResult, finalTime);                               } catch (Exception e) {                                    log.info(-----------内部数据清洗异常:配置:{},{}, reconciliationSqlEo, e.getMessage());                               }                                return clearResult;                           }                   );                    list.add(future);               }                if (CollectionUtils.isNotEmpty(list)) {                    for (FutureClearResult future : list) {                        FutureTaskClearResult futureTask (FutureTask) future;                        try {                            ClearResult clearResult futureTask.get();                            log.info(内部数据清洗结果:{}, clearResult.toString());                       } catch (Exception e) {                            log.info(内部数据清洗结果获取失败:{}, e.getMessage());                       }                   }               }           }       } finally {            if (Objects.nonNull(mutex)) {                iLockService.unlock(mutex);           }       }   } ChannelFuture 是1个接口。public interface ChannelFuture extends Future 在使用 Netty 进行编程时拦截操作和转换出入站数据只需要您提供 callback 或利用future 即可。 这使得链式操作简单、高效, 并有利于编写可重用的、通用的代码。 Netty 框架的目标就是让你的业务逻辑从网络基础应用编码中分离出来、解脱出来 Netty 中所有的 IO 操作都是异步的不能立刻得知消息是否被正确处理。 但是可以过一会等它执行完成或者直接注册一个监听具体的实现就是通过 Future 和 ChannelFutures他们可以注册一个监听当操作执行成功或失败时监听会自动触发注册的监听事件。 Future-Listener 机制 当 Future 对象刚刚创建时处于非完成状态调用者可以通过返回的 ChannelFuture 来获取操作执行的状态注册监听函数来执行完成后的操作。 常见有如下操作: 通过 isDone 方法来判断当前操作是否完成 通过 isSuccess 方法来判断已完成的当前操作是否成功 通过 getCause 方法来获取已完成的当前操作失败的原因 通过 isCancelled 方法来判断已完成的当前操作是否被取消 通过 addListener 方法来注册监听器当操作已完成(isDone 方法返回完成)将会通知指定的监听器如果 Future 对象已完成则通知指定的监听器 java //返回当前正在进行 IO 操作的通道 Channel channel() //等待异步操作执行完毕 ChannelFuture sync() ​            //示例代码            ChannelFuture cf bootstrap.bind(6668).sync();            Channel channel cf.channel();            //给cf 注册监听器监控我们关心的事件            cf.addListener(new ChannelFutureListener() {                Override             public void operationComplete(ChannelFuture future) throws Exception {                    if (cf.isSuccess()) {                        System.out.println(服务器监听端口 6668 成功);                   } else {                        System.out.println(服务器监听端口 6668 失败);                   }               }           });            //对关闭通道进行监听            ChannelFuture sync cf.channel().closeFuture().sync();       } java  public static void main(String[] args) {        EventExecutorGroup group new DefaultEventExecutorGroup(1);        Callable callable new Callable() { ​            Override            public Integer call() throws Exception {                Thread.sleep(10000);                System.out.println(Sleep......);                return new Integer(99);           }       };        io.netty.util.concurrent.Future f (io.netty.util.concurrent.Future) group.submit(callable); ​ ​        GenericFutureListener listener new GenericFutureListener() {            Override           public void operationComplete(io.netty.util.concurrent.Future future) throws Exception {                System.out.println(Thread.currentThread().getName() result: future.get());               //不调用程序会一直存活               //实际应用 可不调用 让它一直存在                group.shutdown();           }       };        GenericFutureListener[] listeners {listener};        f.addListeners(listeners);        System.out.println( f.isSuccess()); ​   } Channel Netty 网络通信的组件能够用于执行网络 I/O 操作。 通过Channel 可获得当前网络连接的通道的状态 通过Channel 可获得 网络连接的配置参数 (例如接收缓冲区大小) Channel 提供异步的网络 I/O 操作(如建立连接读写绑定端口)异步调用意味着任何 I/O 调用都将立即返回并且不保证在调用结束时所请求的 I/O 操作已完成。 调用立即返回一个 ChannelFuture 实例通过注册监听器到 ChannelFuture 上可以 I/O 操作成功、失败或取 消时回调通知调用方。 支持关联 I/O 操作与对应的处理程序 不同协议、不同的阻塞类型的连接都有不同的 Channel 类型与之对应常用的 Channel 类型: java NioSocketChannel异步的客户端 TCP Socket 连接。 NioServerSocketChannel异步的服务器端 TCP Socket 连接。 NioDatagramChannel异步的 UDP 连接。 NioSctpChannel异步的客户端 Sctp 连接。 NioSctpServerChannel异步的 Sctp 服务器端连接这些通道涵盖了 UDP 和 TCP 网络 IO 以及文件 IO。 Selector 一个eventLoop会绑定1个selector。 Netty 基于 Selector 对象实现 I/O 多路复用通过 Selector 一个线程可以监听多个 [Channel] 的不同事件。 当向一个 Selector 中注册 Channel 后Selector 内部的机制就可以自动不断地查询这些注册的 Channel 是否有已就绪的 I/O 事件(例如可读可写网络连接完成等)这样程序就可以很简单地使用一个线程高效地管理多个 Channel 。 ChannelHandler及其实现类 1.ChannelHandler 是一个接口处理 I/O 事件或拦截 I/O 操作并将其转发到其 ChannelPipeline(业务处理链)中的下一个处理程序。 2.ChannelHandler 本身并没有提供很多方法因为这个接口有许多的方法需要实现方便使用期间可以继承它的子类。 3.我们经常需要自定义一个 Handler 类去继承 ChannelInboundHandlerAdapter然后通过重写相应方法实现业务逻辑我们接下来看看一般都需要重写哪些方法。 java public class ChannelInboundHandlerAdapter extends ChannelHandlerAdapter implements ChannelInboundHandler { ​ public ChannelInboundHandlerAdapter() { } ​ public void channelRegistered(ChannelHandlerContext ctx) throws Exception {     ctx.fireChannelRegistered(); } ​ public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {       ctx.fireChannelUnregistered(); } ​ //通道就绪事件 public void channelActive(ChannelHandlerContext ctx) throws Exception {  ctx.fireChannelActive(); } ​ public void channelInactive(ChannelHandlerContext ctx) throws Exception {      ctx.fireChannelInactive(); }     ​ //通道读取数据事件 public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {      ctx.fireChannelRead(msg); }   ​ //数据读取完毕事件   public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {        ctx.fireChannelReadComplete();     }     ​ public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {        ctx.fireUserEventTriggered(evt); }   ​ public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception { ctx.fireChannelWritabilityChanged(); } ​ //通道发生异常事件 public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception{ ctx.fireExceptionCaught(cause); } } ​ Pipeline 和 ChannelPipeline ChannelPipeline 是一个 Handler 的集合它负责处理和拦截 inbound 或者 outbound 的事件和操作相当于一个贯穿 Netty 的链。 也可以这样理解:ChannelPipeline是保存ChannelHandler的List用于处理或拦截Channel的入站事件和出站操作。 ChannelPipeline 实现了一种高级形式的拦截过滤器模式。 使用户可以完全控制事件的处理方式以及 Channel 中各个的 ChannelHandler 如何相互交互。 在 Netty 中每个 Channel 都有且仅有一个 ChannelPipeline 与之对应它们的组成关系如下 一个 Channel 包含了一个ChannelPipeline而 ChannelPipeline 中又维护了一个由 ChannelHandlerContext 组成的双向链表并且每个 ChannelHandlerContext 中又关联着一个 ChannelHandler。 入站事件和出站事件在一个双向链表中入站事件会从链表 head 往后传递到最后一个入站的 handler出站事件会从链表 tail 往前传递到最前一个出站的 handler两种类型的 handler 互不干扰。 //把一个业务处理类handler添加到链中的第一个位置 ChannelPipeline addFirst(ChannelHandler... handlers) //把一个业务处理类handler添加到链中的最后一个位置 ChannelPipeline addLast(ChannelHandler... handlers) ChannelHandlerContext 保存 Channel 相关的所有上下文信息1个ChannelHandlerContext关联一个 ChannelHandler对象。 ChannelHandlerContext 中包含一个具体的事件处理器ChannelHandler ,ChannelHandlerContext 中也绑定了对应的 pipeline和Channel 的信息方便对 ChannelHandler进行调用。 //关闭通道 ChannelFuture close() ​ //刷新 ChannelOutboundInvoker flush()     //将 数 据 写 到 ChannelPipeline中当前ChannelHandler的下一个ChannelHandler开始处理(出站) ChannelFuture writeAndFlush(Object msg) ChannelOption Netty 在创建 Channel 实例后,一般都需要设置 ChannelOption 参数。 ChannelOption 参数如下: //ChannelOption.SO_BACKLOG 对应 TCP/IP 协议 listen 函数中的 backlog 参数用来初始化服务器端可连接队列大小。服 务端处理客户端连接请求是顺序处理的所以同一时间只能处理一个客户端连接。多个客户 端来的时候服务端将不能处理的客户端连接请求放在队列中等待处理backlog参数指定 了队列的大小。 ​ //ChannelOption.SO_KEEPALIVE 一直保持连接活动状态 EventLoopGroup 实现类是NioEventLoopGroup EventLoopGroup 是一组 EventLoop 的抽象Netty 为了更好的利用多核 CPU 资源一般会有多个 EventLoop 同时工作每个 EventLoop 维护着一个 Selector 实例。 EventLoopGroup 提供 next 接口可以从组里面按照一定规则获取其中一个 EventLoop来处理任务。在 Netty 服务器端编程中我们一般都需要提供两个EventLoopGroup即BossEventLoopGroup 和 WorkerEventLoopGroup。 通常一个服务端口即一个 ServerSocketChannel对应一个Selector 和一个BossEventLoopGroup 。BossEventLoop 负责接收客户端的连接并将 SocketChannel 交给 WorkerEventLoopGroup 来进行 IO 处理如下图所示 BossEventLoopGroup 通常是一个单线程的 EventLoop。 z换个EventLoop关联1个Selector 实例。 BossEventLoop 不断轮询 Selector 将连接事件 OP_ACCEPT 分离出来。 然后将接收到的SocketChannel 交给 WorkerEventLoopGroup, WorkerEventLoopGroup 会由 next 选择其中一个 EventLoop来将这个 SocketChannel 注册到其维护的Selector并对其后续的 IO 事件进行处理。 //构造方法 public NioEventLoopGroup() //断开连接关闭线程 public Future? shutdownGracefully() Unpooled 类 Netty提供一个专门用来操作缓冲区(即Netty的数据容器)的工具类。 java package unpooledByte; ​ import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; ​ public class NettyByteBuf01 {    public static void main(String[] args) { ​        /*        说明:        1)读取数据geteByte(i)需要指定i        2)读取数据readByte() , 不需要指定i 内部的readIndex 会移动        3)通过Debug 看一下readIndex writeIndex 和 capacity        4)大家看到和NIO的Buffer比较,不再需要flip 进行读写切换了,内部通过readIndex和writeIndex来控制读和写操作        */        ByteBuf buffer Unpooled.buffer(10); ​        for (int i 0; i 10; i) {            buffer.writeByte(i);       } ​        for (int i 0; i buffer.capacity(); i) {            System.out.println(buffer.getByte(i));       } ​       for(int i 0; i buffer.capacity(); i) {            System.out.println(buffer.readByte());       }   } } ​ java package unpooledByte; ​ import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; ​ import java.nio.charset.Charset; ​ public class NettyByteBuf01 {    public static void main(String[] args) {        ByteBuf byteBuf Unpooled.copiedBuffer(hello,world!, Charset.forName(utf-8)); ​        if (byteBuf.hasArray()) { ​            byte[] content byteBuf.array();            //hello,world!            System.out.println(new String(content, Charset.forName(utf-8)));            //UnpooledByteBufAllocator$InstrumentedUnpooledUnsafeHeapByteBuf(ridx: 0, widx: 12, cap: 36)            System.out.println(byteBuf);            //0            System.out.println(byteBuf.arrayOffset());            //0            System.out.println(byteBuf.readerIndex());            //12            System.out.println(byteBuf.writerIndex());            //36            System.out.println(byteBuf.capacity()); ​            int length byteBuf.readableBytes();            //12            System.out.println(length); ​            for (int i 0; i byteBuf.readableBytes(); i) {                //hello,world!                System.out.print((char) byteBuf.getByte(i));           }            System.out.println();            //hell            System.out.println(byteBuf.getCharSequence(0, 4, Charset.forName(utf-8)));            //o,worl            System.out.println(byteBuf.getCharSequence(4, 7, Charset.forName(utf-8)));       }   } } java //通过给定的数据和字符编码返回一个ByteBuf 对象(类似于 NIO 中的 ByteBuffer 但有区别) public static ByteBuf copiedBuffer(CharSequence string, Charset charset)
http://www.sadfv.cn/news/33671/

相关文章:

  • 网站宽度设置网页设计相关书籍
  • 注册网站不用手机短信验证的网站怎么把做的网站上传到网络
  • 创建论坛网站d84 wordpress
  • 建设网站的基本步骤网页设计师月薪
  • 免费域名网站推荐做平台好还是做网站好
  • 沧州市网站建设价格高端网站定制公司
  • 怎么做网站链接危险网站提示
  • 网站上面的体验卡怎么做大连网站建设报价
  • 做网站价格廊坊市固安县建设局网站
  • 如何做seo和网站ui设计公司官网
  • 门户网站建设流程亿网行网站建设
  • 手机登录网站后台沈阳网站建设黑酷科技
  • 东阳市住房和城乡建设局网站wordpress 侵权
  • 如何在行业门户网站上做推广wordpress一直有人登录
  • 顶尖的郑州网站建设怎么做关注网站
  • 许昌网站制作公司怎么做网站的需求
  • 网站建设相关知识深圳苏州旅游网站建设服务
  • 天津网站制作福州别墅外观设计网站推荐
  • 电子商务网站建设规划范文上海网络推广服务
  • 怎么做企业网站推广建网站前途
  • 海外网站加速免费用jsp做的网站源代码下载
  • 郑州专门做网站的公司有哪些传奇公益服
  • 一个网站的设计思路详情页面设计模板
  • 网站定做肇庆自助建站模板
  • 浙江嘉兴建设局网站营销策划与运营方案怎么写
  • 如何对网站进行改版签证中心网站建设
  • 学生网站建设可行性分析wordpress记录主题使用情况
  • 怎样建立一个公司网站中国住房和城乡建设部网站公文
  • 免费制造网站合肥住房城乡建设部的网站
  • 哪里做网站比较好杭州品牌策划