Search in sources :

Example 46 with Message

use of io.vertx.core.eventbus.Message in project vertx-tcp-eventbus-bridge by vert-x3.

the class TcpEventBusBridgeTest method testUnRegister.

@Test
public void testUnRegister(TestContext context) {
    // Send a request and get a response
    NetClient client = vertx.createNetClient();
    final Async async = context.async();
    final String address = "test";
    client.connect(7000, "localhost", context.asyncAssertSuccess(socket -> {
        // 2 replies will arrive:
        // 1). message published to test
        // 2). err of NO_HANDLERS because of consumer for 'test' is unregistered.
        final AtomicBoolean unregistered = new AtomicBoolean(false);
        final FrameParser parser = new FrameParser(parse -> {
            context.assertTrue(parse.succeeded());
            JsonObject frame = parse.result();
            if (unregistered.get()) {
                // consumer on 'test' has been unregistered, send message will fail.
                context.assertEquals("err", frame.getString("type"));
                context.assertEquals("#backtrack", frame.getString("address"));
                context.assertEquals("NO_HANDLERS", frame.getString("failureType"));
                context.assertEquals("No handlers for address test", frame.getString("message"));
                client.close();
                async.complete();
            } else {
                // got message, then unregister the handler
                context.assertNotEquals("err", frame.getString("type"));
                context.assertEquals(false, frame.getBoolean("send"));
                context.assertEquals("Vert.x", frame.getJsonObject("body").getString("value"));
                unregistered.compareAndSet(false, true);
                FrameHelper.sendFrame("unregister", address, null, socket);
                FrameHelper.sendFrame("send", address, "#backtrack", new JsonObject().put("value", "This will fail anyway!"), socket);
            }
        });
        socket.handler(parser);
        FrameHelper.sendFrame("register", address, null, socket);
        FrameHelper.sendFrame("publish", address, new JsonObject().put("value", "Vert.x"), socket);
    }));
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) RunWith(org.junit.runner.RunWith) Vertx(io.vertx.core.Vertx) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Message(io.vertx.core.eventbus.Message) Test(org.junit.Test) BridgeOptions(io.vertx.ext.bridge.BridgeOptions) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) NetServerOptions(io.vertx.core.net.NetServerOptions) FrameParser(io.vertx.ext.eventbus.bridge.tcp.impl.protocol.FrameParser) FrameHelper(io.vertx.ext.eventbus.bridge.tcp.impl.protocol.FrameHelper) After(org.junit.After) BridgeEventType(io.vertx.ext.bridge.BridgeEventType) JsonObject(io.vertx.core.json.JsonObject) Handler(io.vertx.core.Handler) NetClient(io.vertx.core.net.NetClient) PermittedOptions(io.vertx.ext.bridge.PermittedOptions) Before(org.junit.Before) NetSocket(io.vertx.core.net.NetSocket) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) NetClient(io.vertx.core.net.NetClient) Async(io.vertx.ext.unit.Async) JsonObject(io.vertx.core.json.JsonObject) FrameParser(io.vertx.ext.eventbus.bridge.tcp.impl.protocol.FrameParser) Test(org.junit.Test)

Example 47 with Message

use of io.vertx.core.eventbus.Message in project vertx-tcp-eventbus-bridge by vert-x3.

the class TcpEventBusBridgeTest method testReplyFromClient.

@Test
public void testReplyFromClient(TestContext context) {
    // Send a request from java and get a response from the client
    NetClient client = vertx.createNetClient();
    final Async async = context.async();
    final String address = "test";
    client.connect(7000, "localhost", context.asyncAssertSuccess(socket -> {
        final FrameParser parser = new FrameParser(parse -> {
            context.assertTrue(parse.succeeded());
            JsonObject frame = parse.result();
            if ("message".equals(frame.getString("type"))) {
                context.assertEquals(true, frame.getBoolean("send"));
                context.assertEquals("Vert.x", frame.getJsonObject("body").getString("value"));
                FrameHelper.sendFrame("send", frame.getString("replyAddress"), new JsonObject().put("value", "You got it"), socket);
            }
        });
        socket.handler(parser);
        FrameHelper.sendFrame("register", address, null, socket);
        // There is now way to know that the register actually happened, wait a bit before sending.
        vertx.setTimer(500L, timerId -> {
            vertx.eventBus().<JsonObject>request(address, new JsonObject().put("value", "Vert.x"), respMessage -> {
                context.assertTrue(respMessage.succeeded());
                context.assertEquals("You got it", respMessage.result().body().getString("value"));
                client.close();
                async.complete();
            });
        });
    }));
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) RunWith(org.junit.runner.RunWith) Vertx(io.vertx.core.Vertx) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Message(io.vertx.core.eventbus.Message) Test(org.junit.Test) BridgeOptions(io.vertx.ext.bridge.BridgeOptions) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) NetServerOptions(io.vertx.core.net.NetServerOptions) FrameParser(io.vertx.ext.eventbus.bridge.tcp.impl.protocol.FrameParser) FrameHelper(io.vertx.ext.eventbus.bridge.tcp.impl.protocol.FrameHelper) After(org.junit.After) BridgeEventType(io.vertx.ext.bridge.BridgeEventType) JsonObject(io.vertx.core.json.JsonObject) Handler(io.vertx.core.Handler) NetClient(io.vertx.core.net.NetClient) PermittedOptions(io.vertx.ext.bridge.PermittedOptions) Before(org.junit.Before) NetSocket(io.vertx.core.net.NetSocket) NetClient(io.vertx.core.net.NetClient) Async(io.vertx.ext.unit.Async) JsonObject(io.vertx.core.json.JsonObject) FrameParser(io.vertx.ext.eventbus.bridge.tcp.impl.protocol.FrameParser) Test(org.junit.Test)

Example 48 with Message

use of io.vertx.core.eventbus.Message in project vertx-tcp-eventbus-bridge by vert-x3.

the class TcpEventBusBridgeEventTest method testSendVoidMessageTrustAll.

@Test
public void testSendVoidMessageTrustAll(TestContext context) {
    NetClient client = vertx.createNetClient(new NetClientOptions().setSsl(true).setTrustAll(true).setKeyStoreOptions(sslKeyPairCerts.getClientKeyStore()));
    final Async async = context.async();
    vertx.eventBus().consumer("test", (Message<JsonObject> msg) -> {
        client.close();
        async.complete();
    });
    client.connect(7000, "localhost", conn -> {
        context.assertFalse(conn.failed());
        NetSocket socket = conn.result();
        FrameHelper.sendFrame("send", "test", new JsonObject().put("value", "vert.x"), socket);
    });
}
Also used : Message(io.vertx.core.eventbus.Message) Async(io.vertx.ext.unit.Async) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 49 with Message

use of io.vertx.core.eventbus.Message in project vertx-swagger by bobxwang.

the class Application method demo.

/**
 * a demo method about how to using vertx
 *
 * @param applicationContext
 */
private void demo(ApplicationContext applicationContext) {
    final Vertx vertx = applicationContext.getBean(Vertx.class);
    vertx.setPeriodic(1000, id -> {
        // This handler will get called every second
        System.out.println("timer fired!" + new Date().toString());
    });
    vertx.setTimer(1000, id -> {
        // this handler will called after one second, just once
        System.out.println("And one second later this is printed");
    });
    // 拦截所有消息
    vertx.eventBus().addInterceptor(event -> {
        Message message = event.message();
        logger.info(message);
        event.next();
    });
    FileResolver fileResolver = new FileResolver(vertx);
    File file = fileResolver.resolveFile("webjars/bycdao-ui/cdao/DUI.js");
    if (file != null) {
        System.out.println(file.getAbsoluteFile());
    } else {
        System.out.println("null");
    }
}
Also used : Message(io.vertx.core.eventbus.Message) Vertx(io.vertx.core.Vertx) FileResolver(io.vertx.core.impl.FileResolver) File(java.io.File) Date(java.util.Date)

Example 50 with Message

use of io.vertx.core.eventbus.Message in project VX-API-Gateway by EliMirren.

the class VxApiApplication method addRoute.

/**
 * 添加一个路由
 *
 * @param msg
 */
public void addRoute(Message<JsonObject> msg) {
    JsonObject body = msg.body().getJsonObject("api");
    VxApisDTO dto = VxApisDTO.fromJson(body);
    if (dto != null) {
        VxApis api = new VxApis(dto);
        // 是否代理启动API到当前应用
        boolean otherRouteAdd = msg.body().getBoolean("elseRouteToThis", false);
        if (otherRouteAdd) {
            // 服务器的类型1=http,2=https,3=webSocket
            int type = msg.body().getInteger("serverType", 0);
            if (type == 1) {
                addHttpRouter(api, res -> {
                    if (res.succeeded()) {
                        msg.reply(1);
                    } else {
                        msg.fail(500, res.cause().getMessage());
                    }
                });
            } else if (type == 2) {
                addHttpsRouter(api, res -> {
                    if (res.succeeded()) {
                        msg.reply(1);
                    } else {
                        msg.fail(500, res.cause().getMessage());
                        res.cause().printStackTrace();
                    }
                });
            } else {
                msg.fail(500, "不存在的服务");
            }
        } else {
            // 本应用添加API,既属于自己的网关应用添加API
            if (httpRouter != null && httpsRouter != null) {
                Future<Boolean> httpFuture = Future.future(http -> addHttpRouter(api, http));
                Future<Boolean> httpsFuture = Future.<Boolean>future(https -> addHttpsRouter(api, https));
                CompositeFuture.all(httpFuture, httpsFuture).setHandler(res -> {
                    if (res.succeeded()) {
                        msg.reply(1);
                    } else {
                        msg.fail(500, res.cause().getMessage());
                    }
                });
            } else if (httpRouter != null) {
                addHttpRouter(api, res -> {
                    if (res.succeeded()) {
                        msg.reply(1);
                    } else {
                        msg.fail(500, res.cause().getMessage());
                    }
                });
            } else if (httpsRouter != null) {
                addHttpsRouter(api, res -> {
                    if (res.succeeded()) {
                        msg.reply(1);
                    } else {
                        msg.fail(500, res.cause().getMessage());
                    }
                });
            } else {
                msg.fail(404, "找不到的服务器可以加载API");
            }
        }
    } else {
        msg.fail(1400, "API参数不能为null,请检查APIDTO需要实例化的JSON编写是否正确");
    }
}
Also used : VxApis(com.szmirren.vxApi.core.entity.VxApis) VxApiServerOptions(com.szmirren.vxApi.core.options.VxApiServerOptions) VxApiBeforeHandler(com.szmirren.vxApi.spi.handler.VxApiBeforeHandler) HttpMethodEnum(com.szmirren.vxApi.core.enums.HttpMethodEnum) Router(io.vertx.ext.web.Router) RoutingContext(io.vertx.ext.web.RoutingContext) BodyHandler(io.vertx.ext.web.handler.BodyHandler) VxApiEventBusAddressConstant(com.szmirren.vxApi.core.common.VxApiEventBusAddressConstant) ApiServerTypeEnum(com.szmirren.vxApi.core.enums.ApiServerTypeEnum) VxApiAuthFactory(com.szmirren.vxApi.spi.auth.VxApiAuthFactory) PemKeyCertOptions(io.vertx.core.net.PemKeyCertOptions) Map(java.util.Map) VxApiAfterHandler(com.szmirren.vxApi.spi.handler.VxApiAfterHandler) VxApiAfterHandlerFactory(com.szmirren.vxApi.spi.handler.VxApiAfterHandlerFactory) JsonObject(io.vertx.core.json.JsonObject) CookieHandler(io.vertx.ext.web.handler.CookieHandler) VxApiApplicationOptions(com.szmirren.vxApi.core.options.VxApiApplicationOptions) ClusteredSessionStore(io.vertx.ext.web.sstore.ClusteredSessionStore) VxApiCustomHandler(com.szmirren.vxApi.spi.customHandler.VxApiCustomHandler) Set(java.util.Set) Message(io.vertx.core.eventbus.Message) VxApiTrackInfos(com.szmirren.vxApi.core.entity.VxApiTrackInfos) Future(io.vertx.core.Future) VxApisDTO(com.szmirren.vxApi.core.options.VxApisDTO) VxApis(com.szmirren.vxApi.core.entity.VxApis) List(java.util.List) VxApiGatewayAttribute(com.szmirren.vxApi.core.common.VxApiGatewayAttribute) VxApiAuth(com.szmirren.vxApi.spi.auth.VxApiAuth) Logger(org.apache.logging.log4j.Logger) VxApiAfterHandlerOptions(com.szmirren.vxApi.spi.handler.VxApiAfterHandlerOptions) StrUtil(com.szmirren.vxApi.core.common.StrUtil) SessionStore(io.vertx.ext.web.sstore.SessionStore) HttpServerResponse(io.vertx.core.http.HttpServerResponse) AbstractVerticle(io.vertx.core.AbstractVerticle) VxApiApplicationDTO(com.szmirren.vxApi.core.options.VxApiApplicationDTO) VxApiCustomHandlerOptions(com.szmirren.vxApi.spi.customHandler.VxApiCustomHandlerOptions) HttpClient(io.vertx.core.http.HttpClient) VxApiCustomHandlerFactory(com.szmirren.vxApi.spi.customHandler.VxApiCustomHandlerFactory) VxApiRouteHandlerApiLimit(com.szmirren.vxApi.core.handler.route.VxApiRouteHandlerApiLimit) LocalSessionStore(io.vertx.ext.web.sstore.LocalSessionStore) VxApiRouteHandlerRedirectType(com.szmirren.vxApi.core.handler.route.VxApiRouteHandlerRedirectType) MessageFormat(java.text.MessageFormat) ArrayList(java.util.ArrayList) PfxOptions(io.vertx.core.net.PfxOptions) LinkedHashMap(java.util.LinkedHashMap) CompositeFuture(io.vertx.core.CompositeFuture) VxApiBeforeHandlerOptions(com.szmirren.vxApi.spi.handler.VxApiBeforeHandlerOptions) SessionHandler(io.vertx.ext.web.handler.SessionHandler) VxApiRouteHandlerParamCheck(com.szmirren.vxApi.core.handler.route.VxApiRouteHandlerParamCheck) VxApiCertOptions(com.szmirren.vxApi.core.options.VxApiCertOptions) AsyncResult(io.vertx.core.AsyncResult) LinkedHashSet(java.util.LinkedHashSet) Route(io.vertx.ext.web.Route) VxApiCorsOptions(com.szmirren.vxApi.core.options.VxApiCorsOptions) MalformedURLException(java.net.MalformedURLException) VxApiAuthOptions(com.szmirren.vxApi.spi.auth.VxApiAuthOptions) JsonArray(io.vertx.core.json.JsonArray) VxApiBeforeHandlerFactory(com.szmirren.vxApi.spi.handler.VxApiBeforeHandlerFactory) HttpMethod(io.vertx.core.http.HttpMethod) VxApiRouteHandlerHttpService(com.szmirren.vxApi.core.handler.route.VxApiRouteHandlerHttpService) Handler(io.vertx.core.Handler) LogManager(org.apache.logging.log4j.LogManager) CorsHandler(io.vertx.ext.web.handler.CorsHandler) JsonObject(io.vertx.core.json.JsonObject) VxApisDTO(com.szmirren.vxApi.core.options.VxApisDTO)

Aggregations

Message (io.vertx.core.eventbus.Message)53 JsonObject (io.vertx.core.json.JsonObject)37 Test (org.junit.Test)30 Async (io.vertx.ext.unit.Async)19 Vertx (io.vertx.core.Vertx)18 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)17 PermittedOptions (io.vertx.ext.bridge.PermittedOptions)16 Handler (io.vertx.core.Handler)15 BridgeOptions (io.vertx.ext.bridge.BridgeOptions)15 TestContext (io.vertx.ext.unit.TestContext)15 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)14 Before (org.junit.Before)14 RunWith (org.junit.runner.RunWith)14 NetServerOptions (io.vertx.core.net.NetServerOptions)12 BridgeEventType (io.vertx.ext.bridge.BridgeEventType)12 JsonArray (io.vertx.core.json.JsonArray)11 NetClient (io.vertx.core.net.NetClient)11 NetSocket (io.vertx.core.net.NetSocket)11 FrameHelper (io.vertx.ext.eventbus.bridge.tcp.impl.protocol.FrameHelper)11 FrameParser (io.vertx.ext.eventbus.bridge.tcp.impl.protocol.FrameParser)11