@Override public void start() throws Exception { ProtonClient client = ProtonClient.create(vertx); client.connect("localhost", 5672, res -> { if(!res.succeeded()) { System.out.println("Connect failed: " + res.cause()); return; } ProtonConnection connection = res.result(); connection.open(); connection.createReceiver(address).handler((delivery, msg) -> { String content = (String) ((AmqpValue) msg.getBody()).getValue(); System.out.println("Received message with content: " + content); // By default, receivers automatically accept (and settle) the delivery // when the handler returns, if no other disposition has been applied. // To change this and always manage dispositions yourself, use the // setAutoAccept method on the receiver. }).open(); }); } }
private static void initConnection(Vertx vertx, ProtonConnection connection) { connection.openHandler(res -> { System.out.println("Client connection opened, container-id: " + connection.getRemoteContainer()); connection.open(); }); connection.closeHandler(c -> { System.out.println("Client closing connection, container-id: " + connection.getRemoteContainer()); connection.close(); connection.disconnect(); }); connection.disconnectHandler(c -> { System.out.println("Client socket disconnected, container-id: " + connection.getRemoteContainer()); connection.disconnect(); }); connection.sessionOpenHandler(session -> { session.closeHandler(x -> { session.close(); session.free(); }); session.open(); }); connection.senderOpenHandler(sender -> { initSender(vertx, connection, sender); }); connection.receiverOpenHandler(receiver -> { initReceiver(receiver); }); connection.open(); }
conn.open(); } else { log.info("Connection to " + endpoint.getHost() + ":" + endpoint.getPort() + " failed: " + connection.cause().getMessage());
connection.open();
private void connectHandler(ProtonConnection connection) { connection.setContainer(containerId); connection.openHandler(conn -> { log.info("[{}]: Connection opened", containerId); }).closeHandler(conn -> { connection.close(); connection.disconnect(); log.info("[{}]: Connection closed", containerId); }).disconnectHandler(protonConnection -> { connection.disconnect(); log.debug("Disconnected"); }).open(); connection.sessionOpenHandler(ProtonSession::open); connection.senderOpenHandler(sender -> senderOpenHandler(connection, sender)); connection.receiverOpenHandler(receiver -> receiverOpenHandler(connection, receiver)); }
/** * Handler for connection opened by remote * * @param ar async result with info on related Proton connection */ private void processOpenConnection(AsyncResult<ProtonConnection> ar) { if (ar.succeeded()) { log.info("Connection opened by {} {}", ar.result().getRemoteHostname(), ar.result().getRemoteContainer()); ProtonConnection connection = ar.result(); connection.open(); // new connection, preparing for hosting related sink/source endpoints if (!this.endpoints.containsKey(connection)) { this.endpoints.put(connection, new ConnectionEndpoint()); } } }
private void setRemoteConnectionOpenHandler(final ProtonConnection connection) { connection.sessionOpenHandler(remoteOpenSession -> handleSessionOpen(connection, remoteOpenSession)); connection.receiverOpenHandler(remoteOpenReceiver -> handleReceiverOpen(connection, remoteOpenReceiver)); connection.senderOpenHandler(remoteOpenSender -> handleSenderOpen(connection, remoteOpenSender)); connection.disconnectHandler(this::handleRemoteDisconnect); connection.closeHandler(remoteClose -> handleRemoteConnectionClose(connection, remoteClose)); connection.openHandler(remoteOpen -> { LOG.info("client [container: {}, user: {}] connected", connection.getRemoteContainer(), getUserFromConnection(connection)); connection.open(); // attach an ID so that we can later inform downstream components when connection is closed connection.attachments().set(Constants.KEY_CONNECTION_ID, String.class, UUID.randomUUID().toString()); }); }
/** * Open the endpoint, opening the connection */ public void open() { this.connection .sessionOpenHandler(session -> session.open()) .open(); }
/** * Process a connection request accepted by the Proton server or * open the connection if it's working as client * * @param connection Proton connection accepted instance */ private void processConnection(ProtonConnection connection) { connection .openHandler(this::processOpenConnection) .closeHandler(this::processCloseConnection) .disconnectHandler(this::processDisconnection) .sessionOpenHandler(this::processOpenSession) .receiverOpenHandler(receiver -> { this.processOpenReceiver(connection, receiver); }) .senderOpenHandler(sender -> { this.processOpenSender(connection, sender); }); if (this.bridgeConfigProperties.getEndpointConfigProperties().getMode() == AmqpMode.CLIENT) { connection.open(); } }
public void connect(String host, int port, ProtonClientOptions clientOptions, String address, CompletableFuture<Void> promise) { if (connection != null) { log.debug("Already connected"); promise.complete(null); return; } ProtonClient client = ProtonClient.create(vertx); log.debug("Connecting to {}:{}", host, port); client.connect(clientOptions, host, port, result -> { if (result.succeeded()) { log.debug("Connected to {}:{}", host, port); connection = result.result(); connection.setContainer(containerId); createSender(vertx, address, promise, 0); connection.open(); } else { log.info("Connection to {}:{} failed", host, port, result.cause()); promise.completeExceptionally(result.cause()); } }); }
final Future<ProtonConnection> con = Future.future(); unopenedConnection.openHandler(con); unopenedConnection.open(); return con; });
/** * Open the endpoint, opening the connection */ public void open() { if ((this.willHandler == null) || (this.disconnectionHandler == null)) { throw new IllegalStateException("Handlers for received will and disconnection must be set"); } this.connection .sessionOpenHandler(session -> session.open()) .receiverOpenHandler(this::receiverHandler) .open(); }
}); serverConnection.open(); });
public void send(String address, List<Message> messages, long timeout, TimeUnit timeUnit) throws InterruptedException { ProtonClient client = ProtonClient.create(vertx); CountDownLatch latch = new CountDownLatch(1); Queue<Message> messageQueue = new ArrayDeque<>(messages); client.connect(host, port, connectEvent -> { if (connectEvent.succeeded()) { ProtonConnection connection = connectEvent.result(); connection.open(); ProtonSender sender = connection.createSender(address); sender.openHandler(senderOpenEvent -> { if (senderOpenEvent.succeeded()) { sendNext(connection, sender, messageQueue, latch); } }); sender.open(); } }); boolean ok = latch.await(timeout, timeUnit); if (!ok) { throw new RuntimeException("Sending messages timed out, " + messageQueue.size() + " messages unsent"); } }
serverConnection.open(); });
/** * Processes a peer's AMQP <em>open</em> frame. * <p> * This default implementation * <ol> * <li>adds a unique connection identifier to the connection's attachments * under key {@link Constants#KEY_CONNECTION_ID}</li> * <li>invokes {@link #processDesiredCapabilities(ProtonConnection, Symbol[])}</li> * <li>sets a timer that closes the connection once the client's token * has expired</li> * <li>sends the AMQP <em>open</em> frame to the peer</li> * </ol> * * @param connection The connection to open. */ protected void processRemoteOpen(final ProtonConnection connection) { final HonoUser clientPrincipal = Constants.getClientPrincipal(connection); LOG.debug("client [container: {}, user: {}] connected", connection.getRemoteContainer(), clientPrincipal.getName()); // attach an ID so that we can later inform downstream components when connection is closed connection.attachments().set(Constants.KEY_CONNECTION_ID, String.class, UUID.randomUUID().toString()); processDesiredCapabilities(connection, connection.getRemoteDesiredCapabilities()); final Duration delay = Duration.between(Instant.now(), clientPrincipal.getExpirationTime()); final WeakReference<ProtonConnection> conRef = new WeakReference<>(connection); vertx.setTimer(delay.toMillis(), timerId -> { if (conRef.get() != null) { closeExpiredConnection(conRef.get()); } }); connection.open(); }
if (connectEvent.succeeded()) { ProtonConnection connection = connectEvent.result(); connection.open();
connection.open(); LOG.info("client connected [container: {}, user: {}, token valid until: {}]", connection.getRemoteContainer(), clientPrincipal.getName(), clientPrincipal.getExpirationTime().toString());
/** * Processes the AMQP <em>open</em> frame received from a peer. * <p> * Checks if the open frame contains a desired <em>ADDRESS_AUTHZ</em> capability and if so, * adds the authenticated clients' authorities to the properties of the open frame sent * to the peer in response. * * @param connection The connection opened by the peer. */ @Override protected void processRemoteOpen(final ProtonConnection connection) { final boolean isAddressAuthz = Arrays.stream(connection.getRemoteDesiredCapabilities()) .anyMatch(symbol -> symbol.equals(CAPABILITY_ADDRESS_AUTHZ)); if (isAddressAuthz) { LOG.debug("client [container: {}] requests transfer of authenticated user's authorities in open frame", connection.getRemoteContainer()); processAddressAuthzCapability(connection); } connection.open(); vertx.setTimer(5000, closeCon -> { if (!connection.isDisconnected()) { LOG.debug("connection with client [{}] timed out after 5 seconds, closing connection", connection.getRemoteContainer()); connection.setCondition(ProtonHelper.condition(Constants.AMQP_ERROR_INACTIVITY, "client must retrieve token within 5 secs after opening connection")).close(); } }); }
if (event.succeeded()) { ProtonConnection connection = event.result(); connection.open(); senderConnection = Optional.of(connection); ProtonSender sender = connection.createSender(address);