zoukankan      html  css  js  c++  java
  • vertx异步编程测试

    vertx是异步编程的框架,性能较高,开发简单。异步编程就是当一个请求来了,vertx将其交由一个事件进行处理,然后继续向下执行,等处理完成,返回结果,通知客户端。这是一个由服务端反向调用客户端的过程。

    大多数app只是一个vertx对象。

    继承abstractVerticle就会获得一个组件,里面包含vertx对象。

    下面创建一个vertx工程,查看其具体执行流程,使用maven 项目构建工具

    1:向pom.xml中添加依赖

       

            <!-- vertx -->
            <dependency>
                <groupId>io.vertx</groupId>
                <artifactId>vertx-core</artifactId>
                <version>3.2.1</version>
            </dependency>
            <dependency>
                <groupId>io.vertx</groupId>
                <artifactId>vertx-web</artifactId>
                <version>3.2.1</version>
            </dependency>
            

    2:创建Server.java,主要进行路由功能,将请求转发给服务。

      

    package studyNote01;
    
    import io.netty.handler.codec.http.HttpContentEncoder.Result;
    import io.vertx.core.AbstractVerticle;
    import io.vertx.core.Handler;
    import io.vertx.core.buffer.Buffer;
    import io.vertx.core.http.HttpServerOptions;
    import io.vertx.ext.web.Router;
    
    public class Server01 extends AbstractVerticle{
    
        public void start(){  //服务一发布就会执行start
            Router router=Router.router(vertx);
            System.out.println("enter");
            
            vertx.setPeriodic(1000, id->{
                System.out.println("timer");  //每1000毫秒,打印一次,也就是说1000毫秒的逻辑执行完成,后面的就会被触发
            }
            );
            
            Handler handler;
            router.routeWithRegex("/sp1").handler(  //对url请求进行拦截并处理
                    //ctx应用上下文
                    ctx -> vertx.eventBus().<String> send(  //vertx.eventBus()事件总线,可以与参数里面的地址进行通信,能够在所有vertx之间进行通信
                                                            //<String> 向服务发送的Message里面包含的类型。可以是对象等
                            Service01.URL01,                //请求转发的地址,交由相应的服务对事件进行处理
                            "Hello Vertx1",                 //Message中的内容。有上面的String决定<String>
                            result -> {                     //服务将事件处理完成,返回的结果
                                if(result.succeeded())   //处理成功后执行的逻辑
                                    System.out.println(result.result().body());
                                //ctx.reroute("");可以进行二级路由
                                System.out.println("testi1");
                                ctx.response()  //响应,和面可以跟响应内容,如setstatus()等
                                .end();
                            }
                        )
                    );
            System.out.println("test1");  
            
            router.routeWithRegex("/sp2").handler(
                    ctx -> vertx.eventBus().<String> send(
                            Service01.URL02,
                            "Hello Vertx2",
                            result -> {   //这是一个Handler,当结果准备好时,它就会被调用   
                                if(result.succeeded())   //如果成功,后台将会通知来执行这个。
                                    System.out.println(result.result().body());
                                System.out.println("testi2");
                                ctx.response()
                                .end();
                            }
                            )
                    );
            System.out.println("test2");
            
            router.routeWithRegex("/sp3").handler(
                    ctx -> vertx.eventBus().<String> send(
                            Service03.URL03,
                            "Hello Vertx3",
                            result -> {
                                if(result.succeeded())   //如果成功,后台将会通知来执行这个。
                                    System.out.println(result.result().body());
                                System.out.println("testi3");
                                ctx.response()
                                .end();
                            }
                            )
                    );
            System.out.println("test3");
            
            vertx.createHttpServer().requestHandler(router::accept).listen(8080);
        }
    }

    3:创建服务,处理相应的事件,也要继承AbstractVerticle

    Service01.java   

    package studyNote01;
    
    import io.vertx.core.AbstractVerticle;
    import io.vertx.core.cli.Option;
    
    public class Service01 extends AbstractVerticle{
    
        public static final String URL01="VERTX_HELLO_SERVER01";
        public static final String URL02="VERTX_HELLO_SERVER02";
        
        public void start(){
            vertx.eventBus().consumer(URL01, 
                    msg -> {
                        System.out.println(msg.body()); //处理请求消息
                        System.out.print("url01"); //相当于其它逻辑 
                        msg.reply("success01");  //返回给result
                      }
                    );
            vertx.eventBus().consumer(URL02, 
                    msg -> {
                        System.out.println(msg.body());
                        System.out.print("url02"); //相当于其它逻辑 
                        msg.reply("success02");  //返回给result
                    }
                    );
        }
    }

    Service03.java

    package studyNote01;
    
    import io.vertx.core.AbstractVerticle;
    
    public class Service03 extends AbstractVerticle{
    
        public static final String URL03="VERTX_HELLO_SERVER03";
        
        public void start(){
            vertx.eventBus().consumer(URL03,   //处理总线发送过来的相应的事件
                    msg -> {                   //msg消息发送过来的消息
                        System.out.println(msg.body()); //处理请求消息
                        System.out.print("url03"); //相当于其它逻辑 
                        msg.reply("success03");  //对事件进行返回结果,相当于事件中的result
                      }
                    );
        }
    }

    4:创建主函数对象进行发布

    package studyNote01;
    
    import io.vertx.core.Vertx;
    
    public class Main01 {
    
        public static void main(String[] args) {
            Vertx vertx=Vertx.vertx();
            vertx.deployVerticle(new Service01());
            vertx.deployVerticle(new Server01());
            vertx.deployVerticle(new Service03());
        }
    }

    5:访问
         浏览器地址栏依次输入

     http://localhost:8080/sp1

    http://localhost:8080/sp2

    http://localhost:8080/sp3

     后台打印数据:
     

    enter
    test1
    test2
    test3
    Hello Vertx1
    url01success01
    testi1
    timer
    timer
    timer
    Hello Vertx2
    url02success02
    testi2
    timer
    timer
    Hello Vertx3
    url03success03
    testi3
    timer
    timer
    timer
    timer

      

      

  • 相关阅读:
    缓存地图 ArcGIS ——Local compact and exploded tile cache layer for WPF API
    ArcGIS Runtime数据制作教程
    ArcGis 的测距
    图片展示上移效果
    积累
    html/css 图片展示效果
    javascript 回到顶部效果的实现
    javascript实现下拉菜单的显示与隐藏
    html/css 实现下拉菜单效果
    信息排列效果
  • 原文地址:https://www.cnblogs.com/liyafei/p/8370763.html
Copyright © 2011-2022 走看看