Java Code Examples for io.vertx.proton.ProtonConnection#close()

The following examples show how to use io.vertx.proton.ProtonConnection#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AmqpServiceBase.java    From hono with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Closes an expired client connection.
 * <p>
 * A connection is considered expired if the {@link HonoUser#isExpired()} method
 * of the user principal attached to the connection returns {@code true}.
 *
 * @param con The client connection.
 */
protected final void closeExpiredConnection(final ProtonConnection con) {

    if (!con.isDisconnected()) {
        final HonoUser clientPrincipal = Constants.getClientPrincipal(con);
        if (clientPrincipal != null) {
            log.debug("client's [{}] access token has expired, closing connection", clientPrincipal.getName());
            con.disconnectHandler(null);
            con.closeHandler(null);
            con.setCondition(ProtonHelper.condition(AmqpError.UNAUTHORIZED_ACCESS, "access token expired"));
            con.close();
            con.disconnect();
            publishConnectionClosedEvent(con);
        }
    }
}
 
Example 2
Source File: AmqpBridge.java    From strimzi-kafka-bridge with Apache License 2.0 6 votes vote down vote up
/**
 * Close a connection endpoint and before that all the related sink/source endpoints
 *
 * @param connection connection for which closing related endpoint
 */
private void closeConnectionEndpoint(ProtonConnection connection) {

    // closing connection, but before closing all sink/source endpoints
    if (this.endpoints.containsKey(connection)) {
        ConnectionEndpoint endpoint = this.endpoints.get(connection);
        if (endpoint.getSource() != null) {
            endpoint.getSource().close();
        }
        if (!endpoint.getSinks().isEmpty()) {
            endpoint.getSinks().stream().forEach(sink -> sink.close());
        }
        connection.close();
        this.endpoints.remove(connection);
    }
}
 
Example 3
Source File: DeliveryPredicateSender.java    From enmasse with Apache License 2.0 6 votes vote down vote up
private void sendNext(ProtonConnection connection, ProtonSender sender) {

        Message message;
        if (messageQueue.hasNext() && (message = messageQueue.next()) != null) {
            sender.send(message, protonDelivery -> {
                if (predicate.test(protonDelivery)) {
                    resultPromise.complete(numSent.get());
                    connection.close();
                } else {
                    numSent.incrementAndGet();
                    vertx.runOnContext(id -> sendNext(connection, sender));
                }
            });
        } else {
            if (predicate.test(null)) {
                resultPromise.complete(numSent.get());
            } else {
                resultPromise.completeExceptionally(new RuntimeException("No more messages to send after + " + numSent.get() + " messages sent"));
                connectPromise.completeExceptionally(new RuntimeException("No more messages to send after + " + numSent.get() + " messages sent"));
            }
            connection.close();
        }
    }
 
Example 4
Source File: VertxBasedAmqpProtocolAdapter.java    From hono with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Invoked when a client closes the connection with this server.
 *
 * @param con The connection to close.
 * @param res The client's close frame.
 */
private void handleRemoteConnectionClose(final ProtonConnection con, final AsyncResult<ProtonConnection> res) {

    if (res.succeeded()) {
        log.debug("client [container: {}] closed connection", con.getRemoteContainer());
    } else {
        log.debug("client [container: {}] closed connection with error", con.getRemoteContainer(), res.cause());
    }
    con.disconnectHandler(null);
    con.close();
    con.disconnect();
}
 
Example 5
Source File: AmqpServiceBase.java    From hono with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Invoked when a client closes the connection with this server.
 * <p>
 * This implementation closes and disconnects the connection.
 *
 * @param con The connection to close.
 * @param res The client's close frame.
 */
protected void handleRemoteConnectionClose(final ProtonConnection con, final AsyncResult<ProtonConnection> res) {
    if (res.succeeded()) {
        log.debug("client [container: {}] closed connection", con.getRemoteContainer());
    } else {
        log.debug("client [container: {}] closed connection with error", con.getRemoteContainer(), res.cause());
    }
    con.close();
    con.disconnect();
    publishConnectionClosedEvent(con);
}
 
Example 6
Source File: ConnectionFactoryImpl.java    From hono with Eclipse Public License 2.0 5 votes vote down vote up
private void handleTimedOutOpenHandlerResult(final AsyncResult<ProtonConnection> openConnectionResult,
        final ProtonConnection downstreamConnection, final ProtonClientOptions clientOptions) {
    if (openConnectionResult.succeeded()) {
        logger.debug("ignoring received open frame from container [{}] at [{}://{}:{}, role: {}]: connection attempt already timed out",
                downstreamConnection.getRemoteContainer(),
                clientOptions.isSsl() ? "amqps" : "amqp",
                config.getHost(),
                config.getPort(),
                config.getServerRole());
        // close the connection again
        downstreamConnection.closeHandler(null);
        downstreamConnection.disconnectHandler(null);
        downstreamConnection.close();
    } else {
        final ErrorCondition error = downstreamConnection.getRemoteCondition();
        if (error == null) {
            logger.warn("ignoring failure to open connection to container [{}] at [{}://{}:{}, role: {}]: attempt already timed out",
                    downstreamConnection.getRemoteContainer(),
                    clientOptions.isSsl() ? "amqps" : "amqp",
                    config.getHost(),
                    config.getPort(),
                    config.getServerRole(),
                    openConnectionResult.cause());
        } else {
            logger.warn("ignoring failure to open connection to container [{}] at [{}://{}:{}, role: {}]: attempt already timed out; error: {} -{}",
                    downstreamConnection.getRemoteContainer(),
                    clientOptions.isSsl() ? "amqps" : "amqp",
                    config.getHost(),
                    config.getPort(),
                    config.getServerRole(),
                    error.getCondition(),
                    error.getDescription());
        }
    }
}
 
Example 7
Source File: Sender.java    From enmasse with Apache License 2.0 5 votes vote down vote up
private void sendNext(ProtonConnection connection, ProtonSender sender) {

        Message message;
        if (messageQueue.hasNext() && (message = messageQueue.next()) != null) {
            if (sender.getQoS().equals(ProtonQoS.AT_MOST_ONCE)) {
                sender.send(message);
                numSent.incrementAndGet();
                if (predicate.test(message)) {
                    resultPromise.complete(numSent.get());
                } else {
                    vertx.runOnContext(id -> sendNext(connection, sender));
                }
            } else {
                sender.send(message, protonDelivery -> {
                    if (protonDelivery.getRemoteState().equals(Accepted.getInstance())) {
                        numSent.incrementAndGet();
                        if (predicate.test(message)) {
                            resultPromise.complete(numSent.get());
                            connection.close();
                        } else {
                            sendNext(connection, sender);
                        }
                    } else {
                        resultPromise.completeExceptionally(new IllegalStateException("Message not accepted (remote state: " + protonDelivery.getRemoteState() + ") after " + numSent.get() + " messages sent"));
                        connectPromise.completeExceptionally(new IllegalStateException("Message not accepted (remote state: " + protonDelivery.getRemoteState() + ") after " + numSent.get() + " messages sent"));
                        connection.close();
                    }
                });
            }
        } else {
            if (predicate.test(null)) {
                resultPromise.complete(numSent.get());
            } else {
                resultPromise.completeExceptionally(new RuntimeException("No more messages to send after + " + numSent.get() + " messages sent"));
                connectPromise.completeExceptionally(new RuntimeException("No more messages to send after + " + numSent.get() + " messages sent"));
            }
            connection.close();
        }
    }
 
Example 8
Source File: ClientHandlerBase.java    From enmasse with Apache License 2.0 5 votes vote down vote up
protected void handleError(ProtonConnection connection, ErrorCondition error) {
    if (error == null || error.getCondition() == null) {
        log.info("{}: link closed without error", containerId);
    } else {
        log.info("{}: link closed with error {}", containerId, error);
        connection.close();
        if (unauthorizedAccess.equals(error.getCondition()) || error.getDescription().contains("not authorized")) {
            resultPromise.completeExceptionally(new UnauthorizedAccessException(error.getDescription()));
            connectPromise.completeExceptionally(new UnauthorizedAccessException(error.getDescription()));
        } else {
            resultPromise.completeExceptionally(new RuntimeException(error.getDescription()));
            connectPromise.completeExceptionally(new RuntimeException(error.getDescription()));
        }
    }
}
 
Example 9
Source File: BlockingClient.java    From enmasse with Apache License 2.0 5 votes vote down vote up
private void sendNext(ProtonConnection connection, ProtonSender sender, Queue<Message> messageQueue, CountDownLatch latch) {
    Message message = messageQueue.poll();

    if (message == null) {
        connection.close();
        latch.countDown();
    } else {
        sender.send(message, protonDelivery -> sendNext(connection, sender, messageQueue, latch));
    }
}
 
Example 10
Source File: AbstractSender.java    From enmasse with Apache License 2.0 4 votes vote down vote up
@Override
protected void connectionClosed(ProtonConnection conn) {
    conn.close();
    resultPromise.completeExceptionally(new RuntimeException("Connection closed" + getCurrentState()));
    connectPromise.completeExceptionally(new RuntimeException("Connection closed after " + getCurrentState()));
}
 
Example 11
Source File: AbstractSender.java    From enmasse with Apache License 2.0 4 votes vote down vote up
@Override
protected void connectionDisconnected(ProtonConnection conn) {
    conn.close();
    resultPromise.completeExceptionally(new RuntimeException("Connection disconnected after " + getCurrentState()));
    connectPromise.completeExceptionally(new RuntimeException("Connection disconnected after " + getCurrentState()));
}
 
Example 12
Source File: Receiver.java    From enmasse with Apache License 2.0 4 votes vote down vote up
@Override
protected void connectionClosed(ProtonConnection conn) {
    conn.close();
    resultPromise.completeExceptionally(new RuntimeException("Connection closed (" + messages.size() + " messages received"));
    connectPromise.completeExceptionally(new RuntimeException("Connection closed (" + messages.size() + " messages received"));
}
 
Example 13
Source File: Receiver.java    From enmasse with Apache License 2.0 4 votes vote down vote up
@Override
protected void connectionDisconnected(ProtonConnection conn) {
    conn.close();
    resultPromise.completeExceptionally(new RuntimeException("Connection disconnected (" + messages.size() + " messages received"));
    connectPromise.completeExceptionally(new RuntimeException("Connection disconnected (" + messages.size() + " messages received"));
}