use of org.eclipse.hono.application.client.amqp.AmqpApplicationClient in project hono by eclipse.
the class Receiver method start.
/**
* Starts this component.
* <p>
*
* @return A future indicating the outcome of the startup process.
*/
@PostConstruct
Future<CompositeFuture> start() {
final Future<Void> startFuture;
if (client instanceof AmqpApplicationClient) {
final AmqpApplicationClient amqpClient = (AmqpApplicationClient) client;
startFuture = amqpClient.connect().onComplete(con -> amqpClient.addReconnectListener(client -> createConsumer())).mapEmpty();
} else {
startFuture = Future.succeededFuture();
}
return startFuture.compose(c -> createConsumer()).onComplete(this::handleCreateConsumerStatus);
}
use of org.eclipse.hono.application.client.amqp.AmqpApplicationClient in project hono by eclipse.
the class ReceiverTest method clientFactoryVariants.
private static Stream<ApplicationClient<? extends MessageContext>> clientFactoryVariants() {
final AmqpApplicationClient amqpApplicationClientFactory = mock(AmqpApplicationClient.class);
when(amqpApplicationClientFactory.connect()).thenReturn(Future.succeededFuture());
when(amqpApplicationClientFactory.createTelemetryConsumer(anyString(), VertxMockSupport.anyHandler(), VertxMockSupport.anyHandler())).thenReturn(Future.succeededFuture(mock(MessageConsumer.class)));
when(amqpApplicationClientFactory.createEventConsumer(anyString(), VertxMockSupport.anyHandler(), VertxMockSupport.anyHandler())).thenReturn(Future.succeededFuture(mock(MessageConsumer.class)));
final KafkaApplicationClient kafkaApplicationClientFactory = mock(KafkaApplicationClient.class);
when(kafkaApplicationClientFactory.createTelemetryConsumer(anyString(), VertxMockSupport.anyHandler(), VertxMockSupport.anyHandler())).thenReturn(Future.succeededFuture(mock(MessageConsumer.class)));
when(kafkaApplicationClientFactory.createEventConsumer(anyString(), VertxMockSupport.anyHandler(), VertxMockSupport.anyHandler())).thenReturn(Future.succeededFuture(mock(MessageConsumer.class)));
return Stream.of(amqpApplicationClientFactory, kafkaApplicationClientFactory);
}
use of org.eclipse.hono.application.client.amqp.AmqpApplicationClient in project hono by eclipse.
the class TelemetryHttpIT method testUploadQos1MessageFailsIfDeliveryStateNotUpdated.
/**
* Verifies that the upload of a QoS 1 telemetry message fails with a 503 status code
* when the consumer doesn't update the message delivery state and the
* <em>sendMessageTimeout</em> has elapsed.
*
* @param vertx The vert.x instance.
* @param ctx The test context
* @throws InterruptedException if test is interrupted while running.
*/
@Test
@AssumeMessagingSystem(type = MessagingType.amqp)
public void testUploadQos1MessageFailsIfDeliveryStateNotUpdated(final Vertx vertx, final VertxTestContext ctx) throws InterruptedException {
final AmqpApplicationClient amqpApplicationClient = (AmqpApplicationClient) helper.applicationClient;
// GIVEN a device and a north bound message consumer that doesn't update the message delivery state
final Tenant tenant = new Tenant();
final Checkpoint messageReceived = ctx.checkpoint();
final Checkpoint deliveryStateCheckDone = ctx.checkpoint();
final Checkpoint httpResponseReceived = ctx.checkpoint();
final VertxTestContext setup = new VertxTestContext();
final AtomicReference<ProtonDelivery> deliveryRef = new AtomicReference<>();
helper.registry.addDeviceForTenant(tenantId, tenant, deviceId, PWD).compose(ok -> amqpApplicationClient.createTelemetryConsumer(tenantId, msg -> {
final Promise<Void> result = Promise.promise();
final var delivery = msg.getMessageContext().getDelivery();
deliveryRef.set(delivery);
logger.debug("received message: {}", msg.getMessageContext().getRawMessage());
ctx.verify(() -> {
assertThat(delivery.remotelySettled()).isFalse();
assertThat(delivery.getRemoteState()).isNull();
});
messageReceived.flag();
// don't update the delivery state here
return result.future();
}, remoteClose -> {
})).onComplete(setup.succeedingThenComplete());
assertThat(setup.awaitCompletion(IntegrationTestSupport.getTestSetupTimeout(), TimeUnit.SECONDS)).isTrue();
if (setup.failed()) {
ctx.failNow(setup.causeOfFailure());
return;
}
// WHEN the device tries to upload a telemetry message
final MultiMap requestHeaders = MultiMap.caseInsensitiveMultiMap().add(HttpHeaders.CONTENT_TYPE, "binary/octet-stream").add(HttpHeaders.AUTHORIZATION, authorization).add(HttpHeaders.ORIGIN, ORIGIN_URI).add(Constants.HEADER_QOS_LEVEL, "1");
final Future<HttpResponse<Buffer>> httpResponseFuture = httpClient.create(getEndpointUri(), Buffer.buffer("hello"), requestHeaders, // THEN the message gets rejected by the HTTP adapter with a 503
ResponsePredicate.status(HttpURLConnection.HTTP_UNAVAILABLE));
httpResponseFuture.onComplete(ctx.succeeding(response -> {
ctx.verify(() -> {
final var body = response.bodyAsJsonObject();
assertThat(body.getString(RequestResponseApiConstants.FIELD_ERROR)).isEqualTo(ServiceInvocationException.getLocalizedMessage(SendMessageTimeoutException.CLIENT_FACING_MESSAGE_KEY));
});
httpResponseReceived.flag();
// verify that the telemetry message delivery is remotely settled via the timeout handling in the adapter
vertx.setTimer(50, tid -> {
ctx.verify(() -> {
final ProtonDelivery delivery = deliveryRef.get();
assertThat(delivery).isNotNull();
assertThat(delivery.remotelySettled()).isTrue();
assertThat(delivery.getRemoteState()).isNotNull();
assertThat(delivery.getRemoteState().getType()).isEqualTo(DeliveryState.DeliveryStateType.Released);
});
deliveryStateCheckDone.flag();
});
}));
}
use of org.eclipse.hono.application.client.amqp.AmqpApplicationClient in project hono by eclipse.
the class HonoExampleApplicationBase method consumeData.
/**
* Start the application client and set the message handling method to treat data that is received.
*/
protected void consumeData() {
final CompletableFuture<ApplicationClient<? extends MessageContext>> startup = new CompletableFuture<>();
if (client instanceof AmqpApplicationClient) {
final AmqpApplicationClient ac = (AmqpApplicationClient) client;
ac.addDisconnectListener(c -> LOG.info("lost connection to Hono, trying to reconnect ..."));
ac.addReconnectListener(c -> LOG.info("reconnected to Hono"));
}
client.start().compose(v -> CompositeFuture.all(createEventConsumer(), createTelemetryConsumer())).onSuccess(ok -> startup.complete(client)).onFailure(startup::completeExceptionally);
try {
startup.join();
LOG.info("Consumer ready for telemetry and event messages");
System.in.read();
} catch (final CompletionException e) {
LOG.error("{} consumer failed to start [{}:{}]", USE_KAFKA ? "Kafka" : "AMQP", HonoExampleConstants.HONO_MESSAGING_HOST, port, e.getCause());
} catch (final IOException e) {
// nothing we can do
}
final CompletableFuture<ApplicationClient<? extends MessageContext>> shutDown = new CompletableFuture<>();
@SuppressWarnings("rawtypes") final List<Future> closeFutures = new ArrayList<>();
Optional.ofNullable(eventConsumer).map(MessageConsumer::close).ifPresent(closeFutures::add);
Optional.ofNullable(telemetryConsumer).map(MessageConsumer::close).ifPresent(closeFutures::add);
Optional.ofNullable(client).map(Lifecycle::stop).ifPresent(closeFutures::add);
CompositeFuture.join(closeFutures).compose(ok -> vertx.close()).recover(t -> vertx.close()).onComplete(ar -> shutDown.complete(client));
// wait for clients to be closed
shutDown.join();
LOG.info("Consumer has been shut down");
}
Aggregations