/** * Starts up this service. * <ol> * <li>Registers an event bus consumer for {@linkplain #getEventBusAddress() * the service's event bus request address}.</li> * <li>Invokes {@link #doStart(Future)}.</li> * </ol> * * @param startFuture The future to complete on successful startup. */ @Override public final void start(final Future<Void> startFuture) { registerConsumer(); doStart(startFuture); }
/** * Unregisters the registration message consumer from the Vert.x event bus and then invokes {@link #doStop(Future)}. * * @param stopFuture the future to invoke once shutdown is complete. */ @Override public final void stop(final Future<Void> stopFuture) { if (requestConsumer != null) { requestConsumer.unregister(); log.info("unregistered Tenant API request consumer from event bus"); } doStop(stopFuture); }
private void registerConsumer() { requestConsumer = vertx.eventBus().consumer(getEventBusAddress()); requestConsumer.handler(this::processRequestMessage); log.info("listening on event bus [address: {}] for requests", getEventBusAddress()); }
/** * Verify that the method actually returns null when the type does not match. */ @Test public void testInvalidType() { final JsonObject device = new JsonObject().put("device-id", "someValue"); final String stringValue = EventBusService.getTypesafeValueForField(String.class, device, "device-id"); Assert.assertEquals("someValue", stringValue); final Integer intValue = EventBusService.getTypesafeValueForField(Integer.class, device, "device-id"); Assert.assertNull(intValue); }
/** * Verifies that a non-boolean value of a request's <em>enabled</em> * field is replaced with {@code true}. */ @Test public void getRequestPayloadHandlesNonBooleanEnabledField() { final JsonObject device = new JsonObject().put("device-id", "someValue").put("enabled", "notABoolean"); final JsonObject result = service.getRequestPayload(device); assertThat(result.getBoolean("enabled"), is(Boolean.TRUE)); }
private void processRequestMessage(final Message<JsonObject> msg) { if (log.isTraceEnabled()) { log.trace("received request message: {}", msg.body().encodePrettily()); } final EventBusMessage request = EventBusMessage.fromJson(msg.body()); processRequest(request).recover(t -> { log.debug("cannot process request [operation: {}]: {}", request.getOperation(), t.getMessage()); final int status = ServiceInvocationException.extractStatusCode(t); return Future.succeededFuture(request.getResponse(status)); }).map(response -> { if (response.getReplyToAddress() == null) { log.debug("sending response as direct reply to request [operation: {}]", request.getOperation()); msg.reply(response.toJson()); } else if (response.hasResponseProperties()) { log.debug("sending response [operation: {}, reply-to: {}]", request.getOperation(), request.getReplyToAddress()); vertx.eventBus().send(request.getReplyToAddress(), response.toJson()); } else { log.warn("discarding response lacking correlation ID or operation"); } return null; }); }
/** * Verify that a valid type works. */ @Test public void testValidType() { final JsonObject payload = new JsonObject() .put("booleanValue", true) .put("stringValue", "foo") .put("intValue", 42); final String stringValue = EventBusService.getTypesafeValueForField(String.class, payload, "stringValue"); Assert.assertEquals("foo", stringValue); final Integer intValue = EventBusService.getTypesafeValueForField(Integer.class, payload, "intValue"); Assert.assertEquals(Integer.valueOf(42), intValue); final Boolean booleanValue = EventBusService.getTypesafeValueForField(Boolean.class, payload, "booleanValue"); Assert.assertEquals(Boolean.TRUE, booleanValue); }
/** * Verifies that a default <em>enabled</em> field is added to a request. */ @Test public void getRequestPayloadHandlesMissingEnabledField() { final JsonObject device = new JsonObject().put("device-id", "someValue"); final JsonObject result = service.getRequestPayload(device); assertThat(result.getBoolean("enabled"), is(Boolean.TRUE)); }
private void processRequestMessage(final Message<JsonObject> msg) { if (log.isTraceEnabled()) { log.trace("received request message: {}", msg.body().encodePrettily()); } final EventBusMessage request = EventBusMessage.fromJson(msg.body()); final SpanContext spanContext = tracer.extract(Format.Builtin.TEXT_MAP, new MultiMapExtractAdapter(msg.headers())); request.setSpanContext(spanContext); processRequest(request).recover(t -> { log.debug("cannot process request [operation: {}]: {}", request.getOperation(), t.getMessage()); final int status = ServiceInvocationException.extractStatusCode(t); return Future.succeededFuture(request.getResponse(status)); }).map(response -> { if (response.getReplyToAddress() == null) { log.debug("sending response as direct reply to request [operation: {}]", request.getOperation()); msg.reply(response.toJson()); } else if (response.hasResponseProperties()) { log.debug("sending response [operation: {}, reply-to: {}]", request.getOperation(), request.getReplyToAddress()); vertx.eventBus().send(request.getReplyToAddress(), response.toJson()); } else { log.warn("discarding response lacking correlation ID or operation"); } return null; }); }
/** * Starts up this service. * <ol> * <li>Registers an event bus consumer for {@linkplain #getEventBusAddress() * the service's event bus request address}.</li> * <li>Invokes {@link #doStart(Future)}.</li> * </ol> * * @param startFuture The future to complete on successful startup. */ @Override public final void start(final Future<Void> startFuture) { registerConsumer(); doStart(startFuture); }
/** * Verify that the method returns null, when the value is null. */ @Test public void testGetNull() { final JsonObject device = new JsonObject().put("device-id", (String) null); final String value = EventBusService.getTypesafeValueForField(String.class, device, "device-id"); Assert.assertNull(value); }
private void registerConsumer() { requestConsumer = vertx.eventBus().consumer(getEventBusAddress()); requestConsumer.handler(this::processRequestMessage); log.info("listening on event bus [address: {}] for requests", getEventBusAddress()); }
/** * Unregisters the registration message consumer from the Vert.x event bus and then invokes {@link #doStop(Future)}. * * @param stopFuture the future to invoke once shutdown is complete. */ @Override public final void stop(final Future<Void> stopFuture) { if (requestConsumer != null) { requestConsumer.unregister(); log.info("unregistered Tenant API request consumer from event bus"); } doStop(stopFuture); }