use of org.eclipse.hono.client.ServerErrorException in project hono by eclipse.
the class HonoClientImpl method getOrCreateSender.
Future<MessageSender> getOrCreateSender(final String key, final Supplier<Future<MessageSender>> newSenderSupplier) {
final Future<MessageSender> result = Future.future();
context.runOnContext(get -> {
final MessageSender sender = activeSenders.get(key);
if (sender != null && sender.isOpen()) {
LOG.debug("reusing existing message sender [target: {}, credit: {}]", key, sender.getCredit());
result.complete(sender);
} else if (!creationLocks.computeIfAbsent(key, k -> Boolean.FALSE)) {
// register a handler to be notified if the underlying connection to the server fails
// so that we can fail the result handler passed in
final Handler<Void> connectionFailureHandler = connectionLost -> {
// remove lock so that next attempt to open a sender doesn't fail
creationLocks.remove(key);
result.tryFail(new ServerErrorException(HttpURLConnection.HTTP_UNAVAILABLE, "no connection to service"));
};
creationRequests.add(connectionFailureHandler);
creationLocks.put(key, Boolean.TRUE);
LOG.debug("creating new message sender for {}", key);
newSenderSupplier.get().setHandler(creationAttempt -> {
creationLocks.remove(key);
creationRequests.remove(connectionFailureHandler);
if (creationAttempt.succeeded()) {
MessageSender newSender = creationAttempt.result();
LOG.debug("successfully created new message sender for {}", key);
activeSenders.put(key, newSender);
result.tryComplete(newSender);
} else {
LOG.debug("failed to create new message sender for {}", key, creationAttempt.cause());
activeSenders.remove(key);
result.tryFail(creationAttempt.cause());
}
});
} else {
LOG.debug("already trying to create a message sender for {}", key);
result.fail(new ServerErrorException(HttpURLConnection.HTTP_UNAVAILABLE, "no connection to service"));
}
});
return result;
}
use of org.eclipse.hono.client.ServerErrorException in project hono by eclipse.
the class HonoClientImpl method isConnected.
/**
* {@inheritDoc}
*/
@Override
public final Future<Void> isConnected() {
final Future<Void> result = Future.future();
context.runOnContext(check -> {
if (isConnectedInternal()) {
result.complete();
} else {
result.fail(new ServerErrorException(HttpURLConnection.HTTP_UNAVAILABLE));
}
});
return result;
}
use of org.eclipse.hono.client.ServerErrorException in project hono by eclipse.
the class AbstractRequestResponseClientTest method testCancelRequestFailsResponseHandler.
/**
* Verifies that the client cancels and fails a request for which no response
* has been received after a certain amount of time. The request is then
* failed with a {@link ServerErrorException}.
*
* @param ctx The vert.x test context.
*/
@SuppressWarnings("unchecked")
@Test
public void testCancelRequestFailsResponseHandler(final TestContext ctx) {
// GIVEN a request-response client which times out requests after 200 ms
client.setRequestTimeout(200);
// WHEN no response is received for a request sent to the peer
doAnswer(invocation -> {
// do not wait 200ms before running the timeout task but instead
// run it immediately
Handler<Long> task = invocation.getArgument(1);
task.handle(1L);
return null;
}).when(vertx).setTimer(anyLong(), any(Handler.class));
final Async requestFailure = ctx.async();
client.createAndSendRequest("request", null, (JsonObject) null, ctx.asyncAssertFailure(t -> {
ctx.assertTrue(ServerErrorException.class.isInstance(t));
requestFailure.complete();
}));
// THEN the request handler is failed
requestFailure.await();
}
use of org.eclipse.hono.client.ServerErrorException in project hono by eclipse.
the class HonoClientImplTest method testConnectFailsAfterMaxConnectionAttempts.
/**
* Verifies that the client tries to connect a limited
* number of times only.
*
* @param ctx The vert.x test client.
*/
@Test
public void testConnectFailsAfterMaxConnectionAttempts(final TestContext ctx) {
// GIVEN a client that is configured to connect
// to a peer that is not listening
props.setHost(InetAddress.getLoopbackAddress().getHostAddress());
props.setPort(45000);
client = new HonoClientImpl(vertx, props);
final ProtonClientOptions options = new ProtonClientOptions().setConnectTimeout(50).setReconnectAttempts(3).setReconnectInterval(50);
// WHEN the client tries to connect
client.connect(options).setHandler(ctx.asyncAssertFailure(t -> {
// THEN the connection attempt fails
ctx.assertEquals(HttpURLConnection.HTTP_UNAVAILABLE, ((ServerErrorException) t).getErrorCode());
}));
}
Aggregations