Search in sources :

Example 1 with DeliveryOptions

use of io.vertx.core.eventbus.DeliveryOptions in project vert.x by eclipse.

the class MetricsTest method testReplyFailureTimeout2.

@Test
public void testReplyFailureTimeout2() throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    EventBus eb = vertx.eventBus();
    eb.consumer(ADDRESS1, msg -> {
        msg.reply("juu", new DeliveryOptions().setSendTimeout(10), ar -> {
            assertTrue(ar.failed());
            latch.countDown();
        });
    });
    eb.send(ADDRESS1, "bar", ar -> {
    });
    awaitLatch(latch);
    FakeEventBusMetrics metrics = FakeMetricsBase.getMetrics(eb);
    assertEquals(1, metrics.getReplyFailureAddresses().size());
    assertEquals(Collections.singletonList(ReplyFailure.TIMEOUT), metrics.getReplyFailures());
}
Also used : EventBus(io.vertx.core.eventbus.EventBus) CountDownLatch(java.util.concurrent.CountDownLatch) DeliveryOptions(io.vertx.core.eventbus.DeliveryOptions) Test(org.junit.Test)

Example 2 with DeliveryOptions

use of io.vertx.core.eventbus.DeliveryOptions in project vert.x by eclipse.

the class EventBusFlowControlTest method testFlowControlWithOptions.

@Test
public void testFlowControlWithOptions() {
    MessageProducer<String> prod = eb.sender("some-address");
    prod.deliveryOptions(new DeliveryOptions().addHeader("foo", "bar"));
    int numBatches = 1000;
    int wqms = 2000;
    prod.setWriteQueueMaxSize(wqms);
    MessageConsumer<String> consumer = eb.consumer("some-address");
    AtomicInteger cnt = new AtomicInteger();
    consumer.handler(msg -> {
        int c = cnt.incrementAndGet();
        if (c == numBatches * wqms) {
            testComplete();
        }
    });
    vertx.runOnContext(v -> {
        sendBatch(prod, wqms, numBatches, 0);
    });
    await(10, TimeUnit.SECONDS);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DeliveryOptions(io.vertx.core.eventbus.DeliveryOptions) Test(org.junit.Test)

Example 3 with DeliveryOptions

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

the class TcpEventBusBridgeImpl method doSendOrPub.

private void doSendOrPub(boolean send, NetSocket socket, String address, JsonObject msg, Map<String, MessageConsumer<?>> registry, Map<String, Message<JsonObject>> replies) {
    final JsonObject body = msg.getJsonObject("body");
    final JsonObject headers = msg.getJsonObject("headers");
    // default to message
    final String type = msg.getString("type", "message");
    DeliveryOptions deliveryOptions = parseMsgHeaders(new DeliveryOptions(), headers);
    switch(type) {
        case "send":
            if (checkMatches(true, address, replies)) {
                final String replyAddress = msg.getString("replyAddress");
                if (replyAddress != null) {
                    eb.send(address, body, deliveryOptions, (AsyncResult<Message<JsonObject>> res1) -> {
                        if (res1.failed()) {
                            sendErrFrame(address, replyAddress, (ReplyException) res1.cause(), socket);
                        } else {
                            final Message<JsonObject> response = res1.result();
                            final JsonObject responseHeaders = new JsonObject();
                            // clone the headers from / to
                            for (Map.Entry<String, String> entry : response.headers()) {
                                responseHeaders.put(entry.getKey(), entry.getValue());
                            }
                            if (response.replyAddress() != null) {
                                replies.put(response.replyAddress(), response);
                            }
                            sendFrame("message", replyAddress, response.replyAddress(), responseHeaders, true, response.body(), socket);
                        }
                    });
                } else {
                    if (replies.containsKey(address)) {
                        replies.get(address).reply(body, deliveryOptions);
                    } else {
                        eb.send(address, body, deliveryOptions);
                    }
                }
                // replies are a one time off operation
                replies.remove(address);
            } else {
                sendErrFrame("access_denied", socket);
            }
            break;
        case "publish":
            if (checkMatches(true, address)) {
                eb.publish(address, body, deliveryOptions);
            } else {
                sendErrFrame("access_denied", socket);
            }
            break;
        case "register":
            if (checkMatches(false, address)) {
                registry.put(address, eb.consumer(address, (Message<JsonObject> res1) -> {
                    // save a reference to the message so tcp bridged messages can be replied properly
                    if (res1.replyAddress() != null) {
                        replies.put(res1.replyAddress(), res1);
                    }
                    final JsonObject responseHeaders = new JsonObject();
                    // clone the headers from / to
                    for (Map.Entry<String, String> entry : res1.headers()) {
                        responseHeaders.put(entry.getKey(), entry.getValue());
                    }
                    sendFrame("message", res1.address(), res1.replyAddress(), responseHeaders, res1.isSend(), res1.body(), socket);
                }));
            } else {
                sendErrFrame("access_denied", socket);
            }
            break;
        case "unregister":
            if (checkMatches(false, address)) {
                MessageConsumer<?> consumer = registry.remove(address);
                if (consumer != null) {
                    consumer.unregister();
                } else {
                    sendErrFrame("unknown_address", socket);
                }
            } else {
                sendErrFrame("access_denied", socket);
            }
            break;
        default:
            sendErrFrame("unknown_type", socket);
            break;
    }
}
Also used : JsonObject(io.vertx.core.json.JsonObject) DeliveryOptions(io.vertx.core.eventbus.DeliveryOptions) AsyncResult(io.vertx.core.AsyncResult) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 4 with DeliveryOptions

use of io.vertx.core.eventbus.DeliveryOptions in project vertx-examples by vert-x3.

the class ProcessorServiceVertxEBProxy method process.

@Override
public void process(JsonObject document, Handler<AsyncResult<JsonObject>> resultHandler) {
    if (closed) {
        resultHandler.handle(Future.failedFuture(new IllegalStateException("Proxy is closed")));
        return;
    }
    JsonObject _json = new JsonObject();
    _json.put("document", document);
    DeliveryOptions _deliveryOptions = (_options != null) ? new DeliveryOptions(_options) : new DeliveryOptions();
    _deliveryOptions.addHeader("action", "process");
    _vertx.eventBus().<JsonObject>send(_address, _json, _deliveryOptions, res -> {
        if (res.failed()) {
            resultHandler.handle(Future.failedFuture(res.cause()));
        } else {
            resultHandler.handle(Future.succeededFuture(res.result().body()));
        }
    });
}
Also used : JsonObject(io.vertx.core.json.JsonObject) DeliveryOptions(io.vertx.core.eventbus.DeliveryOptions)

Example 5 with DeliveryOptions

use of io.vertx.core.eventbus.DeliveryOptions in project vertx-examples by vert-x3.

the class SomeDatabaseServiceVertxEBProxy method getDataById.

@Override
public SomeDatabaseService getDataById(int id, Handler<AsyncResult<JsonObject>> resultHandler) {
    if (closed) {
        resultHandler.handle(Future.failedFuture(new IllegalStateException("Proxy is closed")));
        return this;
    }
    JsonObject _json = new JsonObject();
    _json.put("id", id);
    DeliveryOptions _deliveryOptions = (_options != null) ? new DeliveryOptions(_options) : new DeliveryOptions();
    _deliveryOptions.addHeader("action", "getDataById");
    _vertx.eventBus().<JsonObject>send(_address, _json, _deliveryOptions, res -> {
        if (res.failed()) {
            resultHandler.handle(Future.failedFuture(res.cause()));
        } else {
            resultHandler.handle(Future.succeededFuture(res.result().body()));
        }
    });
    return this;
}
Also used : JsonObject(io.vertx.core.json.JsonObject) DeliveryOptions(io.vertx.core.eventbus.DeliveryOptions)

Aggregations

DeliveryOptions (io.vertx.core.eventbus.DeliveryOptions)35 Test (org.junit.Test)14 CountDownLatch (java.util.concurrent.CountDownLatch)10 EventBus (io.vertx.core.eventbus.EventBus)8 JsonObject (io.vertx.core.json.JsonObject)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 Context (io.vertx.core.Context)4 ReplyException (io.vertx.core.eventbus.ReplyException)3 ReplyFailure (io.vertx.core.eventbus.ReplyFailure)3 Map (java.util.Map)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 Is.is (org.hamcrest.core.Is.is)3 FileParser (com.codingchili.Model.FileParser)2 ParserException (com.codingchili.Model.ParserException)2 io.vertx.core (io.vertx.core)2 Vertx (io.vertx.core.Vertx)2 Buffer (io.vertx.core.buffer.Buffer)2 DatagramSocket (io.vertx.core.datagram.DatagramSocket)2 MessageConsumer (io.vertx.core.eventbus.MessageConsumer)2 FileSystem (io.vertx.core.file.FileSystem)2