Search in sources :

Example 26 with DeliveryOptions

use of io.vertx.core.eventbus.DeliveryOptions in project mod-orders by folio-org.

the class CheckInOrderStatusChangeChangeHandlerTest method sendEvent.

private void sendEvent(JsonObject data, Handler<AsyncResult<Message<String>>> replyHandler) {
    // Add okapi url header
    DeliveryOptions deliveryOptions = new DeliveryOptions().addHeader(X_OKAPI_URL.getName(), X_OKAPI_URL.getValue());
    vertx.eventBus().request(MessageAddress.CHECKIN_ORDER_STATUS_UPDATE.address, data, deliveryOptions, replyHandler);
}
Also used : DeliveryOptions(io.vertx.core.eventbus.DeliveryOptions)

Example 27 with DeliveryOptions

use of io.vertx.core.eventbus.DeliveryOptions in project apicurio-registry by Apicurio.

the class EventsServiceImpl method triggerEvent.

@Override
public void triggerEvent(RegistryEventType type, Optional<String> artifactId, Object data) {
    if (configuredSinks && data != null) {
        Buffer buffer;
        try {
            buffer = Buffer.buffer(getMapper().writeValueAsBytes(data));
        } catch (JsonProcessingException e) {
            log.error("Error serializing event data", e);
            return;
        }
        DeliveryOptions opts = new DeliveryOptions().addHeader("type", type.cloudEventType());
        if (artifactId.isPresent()) {
            opts.addHeader("artifactId", artifactId.get());
        }
        eventBus.publish(INTERNAL_EVENTS_ADDRESS, buffer, opts);
    }
}
Also used : Buffer(io.vertx.core.buffer.Buffer) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) DeliveryOptions(io.vertx.core.eventbus.DeliveryOptions)

Example 28 with DeliveryOptions

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

the class Examples method example10.

public void example10(EventBus eventBus, MessageCodec myCodec) {
    eventBus.registerCodec(myCodec);
    DeliveryOptions options = new DeliveryOptions().setCodecName(myCodec.name());
    eventBus.send("orders", new MyPOJO(), options);
}
Also used : DeliveryOptions(io.vertx.core.eventbus.DeliveryOptions)

Example 29 with DeliveryOptions

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

the class MetricsTest method testReplyFailureRecipientFailure.

@Test
public void testReplyFailureRecipientFailure() throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    EventBus eb = vertx.eventBus();
    FakeEventBusMetrics metrics = FakeMetricsBase.getMetrics(eb);
    AtomicReference<String> replyAddress = new AtomicReference<>();
    CountDownLatch regLatch = new CountDownLatch(1);
    eb.consumer("foo", msg -> {
        replyAddress.set(msg.replyAddress());
        msg.fail(0, "whatever");
    }).completionHandler(onSuccess(v -> {
        regLatch.countDown();
    }));
    awaitLatch(regLatch);
    eb.request("foo", "bar", new DeliveryOptions(), ar -> {
        assertTrue(ar.failed());
        latch.countDown();
    });
    awaitLatch(latch);
    assertWaitUntil(() -> metrics.getReplyFailureAddresses().equals(Collections.singletonList("foo")));
    assertEquals(Collections.singletonList(ReplyFailure.RECIPIENT_FAILURE), metrics.getReplyFailures());
}
Also used : DeliveryOptions(io.vertx.core.eventbus.DeliveryOptions) java.util(java.util) io.vertx.core(io.vertx.core) DatagramSocket(io.vertx.core.datagram.DatagramSocket) ContextInternal(io.vertx.core.impl.ContextInternal) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) VertxTestBase(io.vertx.test.core.VertxTestBase) AtomicReference(java.util.concurrent.atomic.AtomicReference) EventBus(io.vertx.core.eventbus.EventBus) TestUtils(io.vertx.test.core.TestUtils) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BiConsumer(java.util.function.BiConsumer) Is.is(org.hamcrest.core.Is.is) ReplyFailure(io.vertx.core.eventbus.ReplyFailure) NetClient(io.vertx.core.net.NetClient) JdkSSLEngineOptions(io.vertx.core.net.JdkSSLEngineOptions) io.vertx.test.fakemetrics(io.vertx.test.fakemetrics) EventLoopGroup(io.netty.channel.EventLoopGroup) VertxInternal(io.vertx.core.impl.VertxInternal) Trust(io.vertx.test.tls.Trust) Test(org.junit.Test) EventLoop(io.netty.channel.EventLoop) Collectors(java.util.stream.Collectors) io.vertx.core.http(io.vertx.core.http) NetClientOptions(io.vertx.core.net.NetClientOptions) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) Buffer(io.vertx.core.buffer.Buffer) MetricsOptions(io.vertx.core.metrics.MetricsOptions) MessageConsumer(io.vertx.core.eventbus.MessageConsumer) NetSocket(io.vertx.core.net.NetSocket) AtomicReference(java.util.concurrent.atomic.AtomicReference) EventBus(io.vertx.core.eventbus.EventBus) CountDownLatch(java.util.concurrent.CountDownLatch) DeliveryOptions(io.vertx.core.eventbus.DeliveryOptions) Test(org.junit.Test)

Example 30 with DeliveryOptions

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

the class EventBusHandler method handle.

public boolean handle(final HttpServerRequest request) {
    final Logger requestLog = RequestLoggerFactory.getLogger(EventBusHandler.class, request);
    if (request.uri().startsWith(apiPath)) {
        requestLog.debug("Handling {}", request.uri());
        Matcher matcher = adressPathPattern.matcher(request.uri());
        if (matcher.matches()) {
            final String address = addressPrefix + matcher.group(1);
            final JsonObject message = new JsonObject().put(URI, request.uri()).put(METHOD, request.method().name()).put(HEADERS, JsonMultiMap.toJson(request.headers()));
            requestLog.debug("Preparing message for address {}", address);
            request.bodyHandler(buffer -> {
                String contentType = request.headers().get(CONTENT_TYPE);
                if (contentType == null) {
                    contentType = APPLICATION_JSON;
                }
                if (buffer != null && buffer.length() > 0) {
                    if (contentType.contains(APPLICATION_JSON)) {
                        try {
                            message.put(PAYLOAD, new JsonObject(buffer.toString()));
                        } catch (DecodeException e) {
                            request.response().setStatusCode(BAD_REQUEST);
                            request.response().end(e.getMessage());
                            return;
                        }
                    } else if (contentType.contains(TEXT)) {
                        message.put(PAYLOAD, buffer.toString());
                    } else {
                        message.put(PAYLOAD, buffer.getBytes());
                    }
                }
                requestLog.debug("Request content type is {}", contentType);
                if (HttpMethod.GET == request.method() || Boolean.TRUE.toString().equals(request.headers().get(SYNC))) {
                    requestLog.debug("This is a synchronous request");
                    vertx.eventBus().request(address, message, new DeliveryOptions().setSendTimeout(TIMEOUT), (Handler<AsyncResult<Message<JsonObject>>>) reply -> {
                        if (reply.succeeded()) {
                            requestLog.debug("Got response");
                            JsonObject response = reply.result().body();
                            MultiMap headers = null;
                            try {
                                if (response.fieldNames().contains(HEADERS)) {
                                    headers = JsonMultiMap.fromJson(response.getJsonArray(HEADERS));
                                    request.response().headers().setAll(headers);
                                }
                            } catch (DecodeException e) {
                                requestLog.warn("Wrong headers in reply", e);
                            }
                            if (response.fieldNames().contains(PAYLOAD)) {
                                String responseContentType;
                                if (headers != null) {
                                    responseContentType = headers.get(CONTENT_TYPE);
                                } else {
                                    responseContentType = APPLICATION_JSON;
                                }
                                requestLog.debug("Response content type is {}", responseContentType);
                                try {
                                    request.response().setChunked(true);
                                    if (responseContentType != null && responseContentType.contains(APPLICATION_JSON)) {
                                        request.response().end(response.getJsonObject(PAYLOAD).encode());
                                    } else if (responseContentType != null && responseContentType.contains(TEXT)) {
                                        request.response().end(response.getString(PAYLOAD));
                                    } else {
                                        request.response().end(Buffer.buffer(response.getBinary(PAYLOAD)));
                                    }
                                } catch (DecodeException e) {
                                    requestLog.warn("Wrong payload in reply for content-type " + responseContentType, e);
                                    request.response().setStatusCode(500);
                                    request.response().end("Wrong payload in reply for content-type " + responseContentType + ": ", e.getMessage());
                                }
                            } else {
                                requestLog.debug("No payload in response");
                                request.response().end();
                            }
                        } else {
                            requestLog.debug("Timeout");
                            request.response().setStatusCode(GATEWAY_TIMEOUT);
                            request.response().setChunked(true);
                            request.response().end("Gateway Timeout");
                        }
                    });
                } else {
                    requestLog.debug("This is an asynchronous request");
                    vertx.eventBus().publish(address, message);
                    request.response().setStatusCode(ACCEPTED);
                    request.response().end();
                }
            });
            return true;
        }
    }
    return false;
}
Also used : DeliveryOptions(io.vertx.core.eventbus.DeliveryOptions) HttpServerRequest(io.vertx.core.http.HttpServerRequest) DecodeException(io.vertx.core.json.DecodeException) JsonMultiMap(org.swisspush.gateleen.core.json.JsonMultiMap) MultiMap(io.vertx.core.MultiMap) LoggerFactory(org.slf4j.LoggerFactory) Router(io.vertx.ext.web.Router) RequestLoggerFactory(org.swisspush.gateleen.core.http.RequestLoggerFactory) ConfigurationResourceManager(org.swisspush.gateleen.core.configuration.ConfigurationResourceManager) SockJSHandler(io.vertx.ext.web.handler.sockjs.SockJSHandler) Matcher(java.util.regex.Matcher) BridgeEventType(io.vertx.ext.bridge.BridgeEventType) JsonObject(io.vertx.core.json.JsonObject) SockJSBridgeOptions(io.vertx.ext.web.handler.sockjs.SockJSBridgeOptions) AsyncResult(io.vertx.core.AsyncResult) SockJSHandlerOptions(io.vertx.ext.web.handler.sockjs.SockJSHandlerOptions) Logger(org.slf4j.Logger) Vertx(io.vertx.core.Vertx) Message(io.vertx.core.eventbus.Message) Buffer(io.vertx.core.buffer.Buffer) HttpMethod(io.vertx.core.http.HttpMethod) ConfigurationResourceConsumer(org.swisspush.gateleen.core.configuration.ConfigurationResourceConsumer) Pattern(java.util.regex.Pattern) Handler(io.vertx.core.Handler) PermittedOptions(io.vertx.ext.bridge.PermittedOptions) JsonMultiMap(org.swisspush.gateleen.core.json.JsonMultiMap) MultiMap(io.vertx.core.MultiMap) Matcher(java.util.regex.Matcher) JsonObject(io.vertx.core.json.JsonObject) Logger(org.slf4j.Logger) DecodeException(io.vertx.core.json.DecodeException) DeliveryOptions(io.vertx.core.eventbus.DeliveryOptions) AsyncResult(io.vertx.core.AsyncResult)

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