use of io.vertx.core.eventbus.Message in project hono by eclipse.
the class LoraProtocolAdapter method handleCommand.
private void handleCommand(final CommandContext commandContext) {
Tags.COMPONENT.set(commandContext.getTracingSpan(), getTypeName());
final Sample timer = metrics.startTimer();
final Command command = commandContext.getCommand();
if (command.getGatewayId() == null) {
final String errorMsg = "no gateway defined for command";
LOG.debug("{} [{}]", errorMsg, command);
commandContext.release(new ServerErrorException(HttpURLConnection.HTTP_UNAVAILABLE, errorMsg));
return;
}
final String tenant = command.getTenant();
final String gatewayId = command.getGatewayId();
final LoraProvider loraProvider = Optional.ofNullable(commandSubscriptions.get(new SubscriptionKey(tenant, gatewayId))).map(Pair::two).orElse(null);
if (loraProvider == null) {
LOG.debug("received command for unknown gateway [{}] for tenant [{}]", gatewayId, tenant);
TracingHelper.logError(commandContext.getTracingSpan(), String.format("received command for unknown gateway [%s]", gatewayId));
commandContext.release(new ServerErrorException(HttpURLConnection.HTTP_UNAVAILABLE, "received command for unknown gateway"));
return;
}
final Future<TenantObject> tenantTracker = getTenantConfiguration(tenant, commandContext.getTracingContext());
tenantTracker.compose(tenantObject -> {
if (command.isValid()) {
return checkMessageLimit(tenantObject, command.getPayloadSize(), commandContext.getTracingContext());
} else {
return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST, "malformed command message"));
}
}).compose(success -> getRegistrationClient().assertRegistration(tenant, gatewayId, null, commandContext.getTracingContext())).compose(registrationAssertion -> sendCommandToGateway(commandContext, loraProvider, registrationAssertion.getCommandEndpoint())).onSuccess(aVoid -> {
addMicrometerSample(commandContext, timer);
commandContext.accept();
metrics.reportCommand(command.isOneWay() ? Direction.ONE_WAY : Direction.REQUEST, tenant, tenantTracker.result(), MetricsTags.ProcessingOutcome.FORWARDED, command.getPayloadSize(), timer);
}).onFailure(t -> {
LOG.debug("error sending command", t);
commandContext.release(t);
metrics.reportCommand(command.isOneWay() ? Direction.ONE_WAY : Direction.REQUEST, tenant, tenantTracker.result(), MetricsTags.ProcessingOutcome.from(t), command.getPayloadSize(), timer);
});
}
use of io.vertx.core.eventbus.Message in project hono by eclipse.
the class LoraProtocolAdapter method handleTenantTimeout.
private void handleTenantTimeout(final Message<String> msg) {
final String tenantId = msg.body();
log.debug("check command subscriptions on timeout of tenant [{}]", tenantId);
final Span span = TracingHelper.buildSpan(tracer, null, "check command subscriptions on tenant timeout", getClass().getSimpleName()).withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT).start();
TracingHelper.setDeviceTags(span, tenantId, null);
// check if tenant still exists
getTenantConfiguration(tenantId, span.context()).recover(thr -> {
if (thr instanceof TenantDisabledOrNotRegisteredException) {
log.debug("tenant [{}] disabled or removed, removing corresponding command consumers", tenantId);
span.log("tenant disabled or removed, corresponding command consumers will be closed");
@SuppressWarnings("rawtypes") final List<Future> consumerCloseFutures = new LinkedList<>();
for (final var iter = commandSubscriptions.entrySet().iterator(); iter.hasNext(); ) {
final var entry = iter.next();
if (entry.getKey().getTenant().equals(tenantId)) {
final CommandConsumer commandConsumer = entry.getValue().one();
consumerCloseFutures.add(commandConsumer.close(span.context()));
iter.remove();
}
}
return CompositeFuture.join(consumerCloseFutures).mapEmpty();
} else {
return Future.failedFuture(thr);
}
}).onFailure(thr -> TracingHelper.logError(span, thr)).onComplete(ar -> span.finish());
}
use of io.vertx.core.eventbus.Message in project vert.x by eclipse.
the class JSONEventBusTest method testChangesNotVisibleArray3.
@Test
public void testChangesNotVisibleArray3() {
List<Object> list = new ArrayList<>();
final JsonArray obj = new JsonArray(list);
eb.<JsonArray>consumer("foo").handler((Message<JsonArray> msg) -> {
vertx.setTimer(1000, id -> {
assertEquals(0, msg.body().size());
testComplete();
});
});
eb.send("foo", obj);
list.add("uhwqdiuh");
await();
}
use of io.vertx.core.eventbus.Message in project vert.x by eclipse.
the class JSONEventBusTest method testChangesNotVisibleObject2.
@Test
public void testChangesNotVisibleObject2() {
final JsonObject obj = new JsonObject();
eb.<JsonObject>consumer("foo").handler((Message<JsonObject> msg) -> {
msg.body().put("b", "uqwduihwqd");
});
eb.send("foo", obj);
vertx.setTimer(1000, id -> {
assertFalse(obj.containsKey("b"));
testComplete();
});
await();
}
use of io.vertx.core.eventbus.Message in project vert.x by eclipse.
the class JSONEventBusTest method testChangesNotVisibleArray1.
@Test
public void testChangesNotVisibleArray1() {
JsonArray obj = new JsonArray();
eb.<JsonArray>consumer("foo").handler((Message<JsonArray> msg) -> {
assertEquals(0, msg.body().size());
testComplete();
});
eb.send("foo", obj);
obj.add("blah");
await();
}
Aggregations