Search in sources :

Example 31 with DeliveryOptions

use of io.vertx.core.eventbus.DeliveryOptions in project gateleen by swisspush.

the class EventBusWriter method flush.

@Override
public void flush() throws IOException {
    if (buffer != null) {
        DeliveryOptions options = new DeliveryOptions();
        if (deliveryOptionsHeaders != null) {
            options.setHeaders(deliveryOptionsHeaders);
        }
        if (TransmissionMode.send == transmissionMode) {
            eventBus.request(address, buffer.toString(), options, (Handler<AsyncResult<Message<JsonObject>>>) reply -> {
                if (reply.succeeded() && "ok".equals(reply.result().body().getString("status"))) {
                    log.debug("Successfully sent to (and got reply from) eventBus address {}", address);
                } else {
                    log.error("Failed to send (not publish) to the eventBus: {}", reply.cause());
                }
            });
        } else {
            eventBus.publish(address, buffer.toString(), options);
        }
        buffer = null;
    }
}
Also used : DeliveryOptions(io.vertx.core.eventbus.DeliveryOptions) EventBus(io.vertx.core.eventbus.EventBus) Logger(org.slf4j.Logger) MultiMap(io.vertx.core.MultiMap) LoggerFactory(org.slf4j.LoggerFactory) Writer(java.io.Writer) Message(io.vertx.core.eventbus.Message) JsonObject(io.vertx.core.json.JsonObject) IOException(java.io.IOException) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) JsonObject(io.vertx.core.json.JsonObject) DeliveryOptions(io.vertx.core.eventbus.DeliveryOptions) AsyncResult(io.vertx.core.AsyncResult)

Example 32 with DeliveryOptions

use of io.vertx.core.eventbus.DeliveryOptions in project gateleen by swisspush.

the class QueueProcessorTest method testInactiveQueueCircuitBreaker.

/**
 * Not active QueueCircuitBreakers should not execute any tasks. Therefore, the actual queue requests must always be made.
 * <p>This tests verifies the following conditions:</p>
 *
 * <ul>
 *     <li>{@link QueueCircuitBreaker#isCircuitCheckEnabled()} is called exactly once to check whether QueueCircuitBreaker is active</li>
 *     <li>{@link QueueCircuitBreaker#handleQueuedRequest(String, HttpRequest)} is never called</li>
 *     <li>{@link HttpClient#request(HttpMethod, String, Handler)} is called exactly once</li>
 * </ul>
 *
 * @param context the test context
 */
@Test
public void testInactiveQueueCircuitBreaker(TestContext context) {
    Async async = context.async();
    QueueCircuitBreaker circuitBreaker = Mockito.spy(new ConfigurableQueueCircuitBreaker(QueueCircuitState.OPEN, false, true));
    new QueueProcessor(vertx, httpClient, monitoringHandler, circuitBreaker);
    vertx.eventBus().request(Address.queueProcessorAddress(), buildQueueEventBusMessage("my_queue"), new DeliveryOptions().setSendTimeout(1000), event -> {
        verify(circuitBreaker, times(1)).isCircuitCheckEnabled();
        verify(circuitBreaker, never()).handleQueuedRequest(anyString(), any(HttpRequest.class));
        verify(httpClient, times(1)).request(any(HttpMethod.class), anyString());
        async.complete();
    });
}
Also used : HttpRequest(org.swisspush.gateleen.core.http.HttpRequest) QueueCircuitBreaker(org.swisspush.gateleen.queue.queuing.circuitbreaker.QueueCircuitBreaker) Async(io.vertx.ext.unit.Async) DeliveryOptions(io.vertx.core.eventbus.DeliveryOptions) HttpMethod(io.vertx.core.http.HttpMethod) Test(org.junit.Test)

Example 33 with DeliveryOptions

use of io.vertx.core.eventbus.DeliveryOptions in project commons by mosip.

the class VidPoolCheckerVerticle method start.

@Override
public void start(Future<Void> startFuture) {
    EventBus eventBus = vertx.eventBus();
    MessageConsumer<String> checkPoolConsumer = eventBus.consumer(EventType.CHECKPOOL);
    DeliveryOptions deliveryOptions = new DeliveryOptions();
    deliveryOptions.setSendTimeout(environment.getProperty("mosip.kernel.vid.pool-population-timeout", Long.class));
    checkPoolConsumer.handler(handler -> {
        long noOfFreeVids = vidService.fetchVidCount(VidLifecycleStatus.AVAILABLE);
        LOGGER.info("no of vid free present are {}", noOfFreeVids);
        if (noOfFreeVids < threshold && !locked.get()) {
            locked.set(true);
            eventBus.send(EventType.GENERATEPOOL, noOfFreeVids, deliveryOptions, replyHandler -> {
                if (replyHandler.succeeded()) {
                    locked.set(false);
                    LOGGER.info("population of pool done");
                } else if (replyHandler.failed()) {
                    locked.set(false);
                    LOGGER.error("population failed with cause ", replyHandler.cause());
                }
            });
        } else {
            LOGGER.info("event type is send {} eventBus{}", handler.isSend(), eventBus);
            LOGGER.info("locked generation");
        }
    });
    MessageConsumer<String> initPoolConsumer = eventBus.consumer(EventType.INITPOOL);
    initPoolConsumer.handler(initPoolHandler -> {
        long start = System.currentTimeMillis();
        long noOfFreeVids = vidService.fetchVidCount(VidLifecycleStatus.AVAILABLE);
        LOGGER.info("no of vid free present are {}", noOfFreeVids);
        LOGGER.info("value of threshold is {} and lock is {}", threshold, locked.get());
        boolean isEligibleForPool = noOfFreeVids < threshold && !locked.get();
        LOGGER.info("is eligible for pool {}", isEligibleForPool);
        if (isEligibleForPool) {
            locked.set(true);
            eventBus.send(EventType.GENERATEPOOL, noOfFreeVids, deliveryOptions, replyHandler -> {
                if (replyHandler.succeeded()) {
                    locked.set(false);
                    deployHttpVerticle(start);
                    LOGGER.info("population of init pool done");
                } else if (replyHandler.failed()) {
                    locked.set(false);
                    LOGGER.error("population failed with cause ", replyHandler.cause());
                    initPoolHandler.fail(100, replyHandler.cause().getMessage());
                }
            });
        } else {
            deployHttpVerticle(start);
        }
    });
}
Also used : EventBus(io.vertx.core.eventbus.EventBus) DeliveryOptions(io.vertx.core.eventbus.DeliveryOptions)

Example 34 with DeliveryOptions

use of io.vertx.core.eventbus.DeliveryOptions in project entcore by opendigitaleducation.

the class DefaultImportService method validate.

@Override
public void validate(ImportInfos importInfos, UserInfos user, final Handler<Either<JsonObject, JsonObject>> handler) {
    try {
        final AdmlValidate admlValidate = new AdmlValidate(user, handler).invoke();
        if (admlValidate.is())
            return;
        final JsonObject action = new JsonObject(mapper.writeValueAsString(importInfos)).put("action", "validate").put("adml-structures", admlValidate.getAdmlStructures());
        eb.send(Directory.FEEDER, action, new DeliveryOptions().setSendTimeout(TIMEOUT), handlerToAsyncHandler(new Handler<Message<JsonObject>>() {

            @Override
            public void handle(Message<JsonObject> res) {
                if ("ok".equals(res.body().getString("status"))) {
                    JsonObject r = res.body().getJsonObject("result", new JsonObject());
                    if (r.getJsonObject("errors", new JsonObject()).size() > 0) {
                        handler.handle(new Either.Left<JsonObject, JsonObject>(r.getJsonObject("errors")));
                    } else {
                        JsonObject f = r.getJsonObject("files");
                        if (r.getJsonObject("softErrors") != null) {
                            f.put("softErrors", r.getJsonObject("softErrors"));
                        }
                        if (isNotEmpty(r.getString("_id"))) {
                            f.put("importId", r.getString("_id"));
                        }
                        handler.handle(new Either.Right<JsonObject, JsonObject>(f));
                    }
                } else {
                    handler.handle(new Either.Left<JsonObject, JsonObject>(new JsonObject().put("global", new fr.wseduc.webutils.collections.JsonArray().add(res.body().getString("message", "")))));
                }
            }
        }));
    } catch (JsonProcessingException e) {
        handler.handle(new Either.Left<JsonObject, JsonObject>(new JsonObject().put("global", new fr.wseduc.webutils.collections.JsonArray().add("unexpected.error"))));
        log.error(e.getMessage(), e);
    }
}
Also used : Message(io.vertx.core.eventbus.Message) JsonObject(io.vertx.core.json.JsonObject) Utils.handlerToAsyncHandler(fr.wseduc.webutils.Utils.handlerToAsyncHandler) Handler(io.vertx.core.Handler) JsonArray(io.vertx.core.json.JsonArray) Either(fr.wseduc.webutils.Either) DeliveryOptions(io.vertx.core.eventbus.DeliveryOptions) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 35 with DeliveryOptions

use of io.vertx.core.eventbus.DeliveryOptions in project entcore by opendigitaleducation.

the class DefaultImportService method sendCommand.

protected void sendCommand(final Handler<Either<JsonObject, JsonObject>> handler, JsonObject action) {
    eb.send("entcore.feeder", action, new DeliveryOptions().setSendTimeout(600000L), handlerToAsyncHandler(new Handler<Message<JsonObject>>() {

        @Override
        public void handle(Message<JsonObject> event) {
            if ("ok".equals(event.body().getString("status"))) {
                JsonObject r = event.body().getJsonObject("result", new JsonObject());
                r.remove("status");
                if (r.getJsonObject("errors", new JsonObject()).size() > 0) {
                    handler.handle(new Either.Left<JsonObject, JsonObject>(r));
                } else {
                    handler.handle(new Either.Right<JsonObject, JsonObject>(r));
                }
            } else {
                handler.handle(new Either.Left<JsonObject, JsonObject>(new JsonObject().put("global", new JsonArray().add(event.body().getString("message", "")))));
            }
        }
    }));
}
Also used : JsonArray(io.vertx.core.json.JsonArray) Message(io.vertx.core.eventbus.Message) Utils.handlerToAsyncHandler(fr.wseduc.webutils.Utils.handlerToAsyncHandler) Handler(io.vertx.core.Handler) JsonObject(io.vertx.core.json.JsonObject) Either(fr.wseduc.webutils.Either) DeliveryOptions(io.vertx.core.eventbus.DeliveryOptions)

Aggregations

DeliveryOptions (io.vertx.core.eventbus.DeliveryOptions)96 JsonObject (io.vertx.core.json.JsonObject)39 Message (io.vertx.core.eventbus.Message)23 EventBus (io.vertx.core.eventbus.EventBus)21 Test (org.junit.Test)20 JsonArray (io.vertx.core.json.JsonArray)17 CountDownLatch (java.util.concurrent.CountDownLatch)16 Handler (io.vertx.core.Handler)15 AsyncResult (io.vertx.core.AsyncResult)14 Utils.handlerToAsyncHandler (fr.wseduc.webutils.Utils.handlerToAsyncHandler)10 Vertx (io.vertx.core.Vertx)10 Either (fr.wseduc.webutils.Either)9 Context (io.vertx.core.Context)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 Buffer (io.vertx.core.buffer.Buffer)8 MultiMap (io.vertx.core.MultiMap)7 Span (io.vertx.test.faketracer.Span)6 java.util (java.util)6 io.vertx.core (io.vertx.core)5 Future (io.vertx.core.Future)5