Search in sources :

Example 26 with Message

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

the class TcpEventBusBridgeTest method testReplyStringMessageFromClient.

@Test
public void testReplyStringMessageFromClient(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"), "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());
                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 27 with Message

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

the class TcpEventBusBridgeTest method testFailFromClient.

@Test
public void testFailFromClient(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.writeFrame(new JsonObject().put("type", "send").put("address", frame.getString("replyAddress")).put("failureCode", 1234).put("message", "ooops!"), 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().request(address, new JsonObject().put("value", "Vert.x"), respMessage -> {
                context.assertTrue(respMessage.failed());
                context.assertEquals("ooops!", respMessage.cause().getMessage());
                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 28 with Message

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

the class TcpEventBusBridgeTest method testNoAddress.

@Test
public void testNoAddress(TestContext context) {
    NetClient client = vertx.createNetClient();
    final Async async = context.async();
    final AtomicBoolean errorOnce = new AtomicBoolean(false);
    final FrameParser parser = new FrameParser(parse -> {
        context.assertTrue(parse.succeeded());
        JsonObject frame = parse.result();
        if (!errorOnce.compareAndSet(false, true)) {
            context.fail("Client gets error message twice!");
        } else {
            context.assertEquals("err", frame.getString("type"));
            context.assertEquals("missing_address", frame.getString("message"));
            vertx.setTimer(200, l -> {
                client.close();
                async.complete();
            });
        }
    });
    client.connect(7000, "localhost", context.asyncAssertSuccess(socket -> {
        socket.handler(parser);
        FrameHelper.sendFrame("send", 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 29 with Message

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

the class TcpEventBusBridgeTest method testErrorReply.

@Test
public void testErrorReply(TestContext context) {
    // Send a request and get a response
    NetClient client = vertx.createNetClient();
    final Async async = context.async();
    vertx.eventBus().consumer("test", (Message<JsonObject> msg) -> {
        msg.fail(0, "oops!");
    });
    client.connect(7000, "localhost", context.asyncAssertSuccess(socket -> {
        final FrameParser parser = new FrameParser(parse -> {
            context.assertTrue(parse.succeeded());
            JsonObject frame = parse.result();
            context.assertEquals("err", frame.getString("type"));
            context.assertEquals("#backtrack", frame.getString("address"));
            client.close();
            async.complete();
        });
        socket.handler(parser);
        FrameHelper.sendFrame("send", "test", "#backtrack", 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) NetClient(io.vertx.core.net.NetClient) Message(io.vertx.core.eventbus.Message) 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 30 with Message

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

the class TcpEventBusBridgeEchoServer method main.

public static void main(String[] args) {
    Vertx vertx = Vertx.vertx();
    EventBus eb = vertx.eventBus();
    eb.consumer("hello", (Message<JsonObject> msg) -> {
        msg.reply(new JsonObject().put("value", "Hello " + msg.body().getString("value")));
    });
    eb.consumer("echo", (Message<JsonObject> msg) -> msg.reply(msg.body()));
    eb.consumer("echo2", (Message<JsonObject> msg) -> {
        if ("send".equals(msg.body().getString("response_type"))) {
            eb.send("echo2_response", msg.body());
        } else {
            eb.publish("echo2_response", msg.body());
        }
    });
    TcpEventBusBridge bridge = TcpEventBusBridge.create(vertx, new BridgeOptions().addInboundPermitted(new PermittedOptions().setAddress("hello")).addInboundPermitted(new PermittedOptions().setAddress("echo")).addOutboundPermitted(new PermittedOptions().setAddress("echo")).addInboundPermitted(new PermittedOptions().setAddress("echo2")).addOutboundPermitted(new PermittedOptions().setAddress("echo2_response")));
    bridge.listen(7000, res -> System.out.println("Ready"));
}
Also used : Message(io.vertx.core.eventbus.Message) TcpEventBusBridge(io.vertx.ext.eventbus.bridge.tcp.TcpEventBusBridge) JsonObject(io.vertx.core.json.JsonObject) BridgeOptions(io.vertx.ext.bridge.BridgeOptions) EventBus(io.vertx.core.eventbus.EventBus) PermittedOptions(io.vertx.ext.bridge.PermittedOptions) Vertx(io.vertx.core.Vertx)

Aggregations

Message (io.vertx.core.eventbus.Message)53 JsonObject (io.vertx.core.json.JsonObject)37 Test (org.junit.Test)33 Async (io.vertx.ext.unit.Async)19 Vertx (io.vertx.core.Vertx)18 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)17 Before (org.junit.Before)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 After (org.junit.After)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