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);
}
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);
}
}
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);
}
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());
}
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;
}
Aggregations